map_element.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apply
  14. // MapElement contains the recorded, local and remote values for a field
  15. // of type map
  16. type MapElement struct {
  17. // FieldMetaImpl contains metadata about the field from openapi
  18. FieldMetaImpl
  19. // MapElementData contains the value a field was set to
  20. MapElementData
  21. // Values contains the combined recorded-local-remote value of each item in the map
  22. // Values contains the values in mapElement. Element must contain
  23. // a Name matching its key in Values
  24. Values map[string]Element
  25. }
  26. // Merge implements Element.Merge
  27. func (e MapElement) Merge(v Strategy) (Result, error) {
  28. return v.MergeMap(e)
  29. }
  30. // GetValues implements Element.GetValues
  31. func (e MapElement) GetValues() map[string]Element {
  32. return e.Values
  33. }
  34. var _ Element = &MapElement{}
  35. // MapElementData contains the recorded, local and remote data for a map or type
  36. type MapElementData struct {
  37. RawElementData
  38. }
  39. // GetRecordedMap returns the Recorded value as a map
  40. func (e MapElementData) GetRecordedMap() map[string]interface{} {
  41. return mapCast(e.recorded)
  42. }
  43. // GetLocalMap returns the Local value as a map
  44. func (e MapElementData) GetLocalMap() map[string]interface{} {
  45. return mapCast(e.local)
  46. }
  47. // GetRemoteMap returns the Remote value as a map
  48. func (e MapElementData) GetRemoteMap() map[string]interface{} {
  49. return mapCast(e.remote)
  50. }
  51. // mapCast casts i to a map if it is non-nil, otherwise returns nil
  52. func mapCast(i interface{}) map[string]interface{} {
  53. if i == nil {
  54. return nil
  55. }
  56. return i.(map[string]interface{})
  57. }
  58. // HasConflict returns ConflictError if some elements in map conflict.
  59. func (e MapElement) HasConflict() error {
  60. for _, item := range e.GetValues() {
  61. if item, ok := item.(ConflictDetector); ok {
  62. if err := item.HasConflict(); err != nil {
  63. return err
  64. }
  65. }
  66. }
  67. return nil
  68. }
  69. var _ ConflictDetector = &MapElement{}