list_element.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // ListElement contains the recorded, local and remote values for a field
  15. // of type list
  16. type ListElement struct {
  17. // FieldMetaImpl contains metadata about the field from openapi
  18. FieldMetaImpl
  19. ListElementData
  20. // Values contains the combined recorded-local-remote value of each item in the list
  21. // Present for lists that can be merged only. Contains the items
  22. // from each of the 3 lists merged into single Elements using
  23. // the merge-key.
  24. Values []Element
  25. }
  26. // Merge implements Element.Merge
  27. func (e ListElement) Merge(v Strategy) (Result, error) {
  28. return v.MergeList(e)
  29. }
  30. var _ Element = &ListElement{}
  31. // ListElementData contains the recorded, local and remote data for a list
  32. type ListElementData struct {
  33. RawElementData
  34. }
  35. // GetRecordedList returns the Recorded value as a list
  36. func (e ListElementData) GetRecordedList() []interface{} {
  37. return sliceCast(e.recorded)
  38. }
  39. // GetLocalList returns the Local value as a list
  40. func (e ListElementData) GetLocalList() []interface{} {
  41. return sliceCast(e.local)
  42. }
  43. // GetRemoteList returns the Remote value as a list
  44. func (e ListElementData) GetRemoteList() []interface{} {
  45. return sliceCast(e.remote)
  46. }
  47. // sliceCast casts i to a slice if it is non-nil, otherwise returns nil
  48. func sliceCast(i interface{}) []interface{} {
  49. if i == nil {
  50. return nil
  51. }
  52. return i.([]interface{})
  53. }
  54. // HasConflict returns ConflictError if fields in recorded and remote of ListElement conflict
  55. func (e ListElement) HasConflict() error {
  56. for _, item := range e.Values {
  57. if item, ok := item.(ConflictDetector); ok {
  58. if err := item.HasConflict(); err != nil {
  59. return err
  60. }
  61. }
  62. }
  63. return nil
  64. }
  65. var _ ConflictDetector = &ListElement{}