visitor.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 parse
  14. import (
  15. "k8s.io/kubernetes/pkg/kubectl/apply"
  16. "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
  17. )
  18. // ItemVisitor provides an interface for Items to Accept and call
  19. // the Visit function that corresponds to its actual type.
  20. type ItemVisitor interface {
  21. // CreatePrimitiveElement builds an Element for a primitiveItem
  22. CreatePrimitiveElement(*primitiveItem) (apply.Element, error)
  23. // CreateListElement builds an Element for a listItem
  24. CreateListElement(*listItem) (apply.Element, error)
  25. // CreateMapElement builds an Element for a mapItem
  26. CreateMapElement(*mapItem) (apply.Element, error)
  27. // CreateTypeElement builds an Element for a typeItem
  28. CreateTypeElement(*typeItem) (apply.Element, error)
  29. }
  30. // ElementBuildingVisitor creates an Elements from Items
  31. // An Element combines the values from the Item with the field metadata.
  32. type ElementBuildingVisitor struct {
  33. resources openapi.Resources
  34. }
  35. // CreatePrimitiveElement creates a primitiveElement
  36. func (v ElementBuildingVisitor) CreatePrimitiveElement(item *primitiveItem) (apply.Element, error) {
  37. return v.primitiveElement(item)
  38. }
  39. // CreateListElement creates a ListElement
  40. func (v ElementBuildingVisitor) CreateListElement(item *listItem) (apply.Element, error) {
  41. meta, err := getFieldMeta(item.GetMeta(), item.Name)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if meta.GetFieldMergeType() == apply.MergeStrategy {
  46. return v.mergeListElement(meta, item)
  47. }
  48. return v.replaceListElement(meta, item)
  49. }
  50. // CreateMapElement creates a mapElement
  51. func (v ElementBuildingVisitor) CreateMapElement(item *mapItem) (apply.Element, error) {
  52. meta, err := getFieldMeta(item.GetMeta(), item.Name)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return v.mapElement(meta, item)
  57. }
  58. // CreateTypeElement creates a typeElement
  59. func (v ElementBuildingVisitor) CreateTypeElement(item *typeItem) (apply.Element, error) {
  60. meta, err := getFieldMeta(item.GetMeta(), item.Name)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return v.typeElement(meta, item)
  65. }