1/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package parse
18
19import (
20	"k8s.io/kube-openapi/pkg/util/proto"
21	"k8s.io/kubectl/pkg/apply"
22)
23
24// Item wraps values from 3 sources (recorded, local, remote).
25// The values are not collated
26type Item interface {
27	// CreateElement merges the values in the item into a combined Element
28	CreateElement(ItemVisitor) (apply.Element, error)
29}
30
31// primitiveItem contains a recorded, local, and remote value
32type primitiveItem struct {
33	Name      string
34	Primitive *proto.Primitive
35
36	apply.RawElementData
37}
38
39func (i *primitiveItem) CreateElement(v ItemVisitor) (apply.Element, error) {
40	return v.CreatePrimitiveElement(i)
41}
42
43func (i *primitiveItem) GetMeta() proto.Schema {
44	// https://golang.org/doc/faq#nil_error
45	if i.Primitive != nil {
46		return i.Primitive
47	}
48	return nil
49}
50
51// listItem contains a recorded, local, and remote list
52type listItem struct {
53	Name  string
54	Array *proto.Array
55
56	apply.ListElementData
57}
58
59func (i *listItem) CreateElement(v ItemVisitor) (apply.Element, error) {
60	return v.CreateListElement(i)
61}
62
63func (i *listItem) GetMeta() proto.Schema {
64	// https://golang.org/doc/faq#nil_error
65	if i.Array != nil {
66		return i.Array
67	}
68	return nil
69}
70
71// mapItem contains a recorded, local, and remote map
72type mapItem struct {
73	Name string
74	Map  *proto.Map
75
76	apply.MapElementData
77}
78
79func (i *mapItem) CreateElement(v ItemVisitor) (apply.Element, error) {
80	return v.CreateMapElement(i)
81}
82
83func (i *mapItem) GetMeta() proto.Schema {
84	// https://golang.org/doc/faq#nil_error
85	if i.Map != nil {
86		return i.Map
87	}
88	return nil
89}
90
91// mapItem contains a recorded, local, and remote map
92type typeItem struct {
93	Name string
94	Type *proto.Kind
95
96	apply.MapElementData
97}
98
99func (i *typeItem) GetMeta() proto.Schema {
100	// https://golang.org/doc/faq#nil_error
101	if i.Type != nil {
102		return i.Type
103	}
104	return nil
105}
106
107func (i *typeItem) CreateElement(v ItemVisitor) (apply.Element, error) {
108	return v.CreateTypeElement(i)
109}
110
111// emptyItem contains no values
112type emptyItem struct {
113	Name string
114}
115
116func (i *emptyItem) CreateElement(v ItemVisitor) (apply.Element, error) {
117	e := &apply.EmptyElement{}
118	e.Name = i.Name
119	return e, nil
120}
121