From 0590b225c762e157aaf53c782415f43c8e84b38c Mon Sep 17 00:00:00 2001 From: jregan Date: Sun, 16 Aug 2020 07:35:11 -0700 Subject: [PATCH 1/2] Copy types.go to rnode.go. --- kyaml/yaml/{types.go => rnode.go} | 0 kyaml/yaml/{types_test.go => rnode_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename kyaml/yaml/{types.go => rnode.go} (100%) rename kyaml/yaml/{types_test.go => rnode_test.go} (100%) diff --git a/kyaml/yaml/types.go b/kyaml/yaml/rnode.go similarity index 100% rename from kyaml/yaml/types.go rename to kyaml/yaml/rnode.go diff --git a/kyaml/yaml/types_test.go b/kyaml/yaml/rnode_test.go similarity index 100% rename from kyaml/yaml/types_test.go rename to kyaml/yaml/rnode_test.go From c4c8decb744ac8a66299af80b274929d84bbbb59 Mon Sep 17 00:00:00 2001 From: jregan Date: Sun, 16 Aug 2020 07:35:11 -0700 Subject: [PATCH 2/2] Delete non-rnode stuff from rnode.go. --- kyaml/yaml/rnode.go | 213 --------------------------------------- kyaml/yaml/rnode_test.go | 98 ------------------ 2 files changed, 311 deletions(-) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 3cf2bb2cf..4a99ebda1 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -4,15 +4,12 @@ package yaml import ( - "bytes" "encoding/json" "fmt" "io/ioutil" - "strings" "gopkg.in/yaml.v3" "sigs.k8s.io/kustomize/kyaml/errors" - "sigs.k8s.io/kustomize/kyaml/sets" ) const ( @@ -45,31 +42,6 @@ func IsEmptyMap(node *RNode) bool { return IsMissingOrNull(node) || IsYNodeEmptyMap(node.YNode()) } -// IsYNodeTaggedNull returns true if the node is explicitly tagged Null. -func IsYNodeTaggedNull(n *yaml.Node) bool { - return n != nil && n.Tag == NodeTagNull -} - -// IsYNodeEmptyMap is true if the Node is a non-nil empty map. -func IsYNodeEmptyMap(n *yaml.Node) bool { - return n != nil && n.Kind == yaml.MappingNode && len(n.Content) == 0 -} - -// IsYNodeEmptyMap is true if the Node is a non-nil empty sequence. -func IsYNodeEmptySeq(n *yaml.Node) bool { - return n != nil && n.Kind == yaml.SequenceNode && len(n.Content) == 0 -} - -// IsYNodeEmptyDoc is true if the node is a Document with no content. -// E.g.: "---\n---" -func IsYNodeEmptyDoc(n *yaml.Node) bool { - return n.Kind == yaml.DocumentNode && n.Content[0].Tag == NodeTagNull -} - -func IsYNodeString(n *yaml.Node) bool { - return n.Kind == yaml.ScalarNode && n.Tag == NodeTagString -} - // GetValue returns underlying yaml.Node Value field func GetValue(node *RNode) string { if IsMissingOrNull(node) { @@ -78,18 +50,6 @@ func GetValue(node *RNode) string { return node.YNode().Value } -// Parser parses values into configuration. -type Parser struct { - Kind string `yaml:"kind,omitempty"` - Value string `yaml:"value,omitempty"` -} - -func (p Parser) Filter(_ *RNode) (*RNode, error) { - d := yaml.NewDecoder(bytes.NewBuffer([]byte(p.Value))) - o := &RNode{value: &yaml.Node{}} - return o, d.Decode(o.value) -} - // Parse parses a yaml string into an *RNode func Parse(value string) (*RNode, error) { return Parser{Value: value}.Filter(nil) @@ -131,28 +91,6 @@ func UpdateFile(filter Filter, path string) error { return WriteFile(y, path) } -// TODO(pwittrock): test this -func GetStyle(styles ...string) Style { - var style Style - for _, s := range styles { - switch s { - case "TaggedStyle": - style |= TaggedStyle - case "DoubleQuotedStyle": - style |= DoubleQuotedStyle - case "SingleQuotedStyle": - style |= SingleQuotedStyle - case "LiteralStyle": - style |= LiteralStyle - case "FoldedStyle": - style |= FoldedStyle - case "FlowStyle": - style |= FlowStyle - } - } - return style -} - // MustParse parses a yaml string into an *RNode and panics if there is an error func MustParse(value string) *RNode { v, err := Parser{Value: value}.Filter(nil) @@ -210,23 +148,6 @@ func NewRNode(value *yaml.Node) *RNode { return &RNode{value: value} } -// Filter defines a function to manipulate an individual RNode such as by changing -// its values, or returning a field. -// -// When possible, Filters should be serializable to yaml so that they can be described -// declaratively as data. -// -// Analogous to http://www.linfo.org/filters.html -type Filter interface { - Filter(object *RNode) (*RNode, error) -} - -type FilterFunc func(object *RNode) (*RNode, error) - -func (f FilterFunc) Filter(object *RNode) (*RNode, error) { - return f(object) -} - // RNode provides functions for manipulating Kubernetes Resources // Objects unmarshalled into *yaml.Nodes type RNode struct { @@ -247,109 +168,6 @@ type RNode struct { Match []string } -// MapNode wraps a field key and value. -type MapNode struct { - Key *RNode - Value *RNode -} - -// IsNilOrEmpty returns true if the MapNode is nil, -// has no value, or has a value that appears empty. -func (mn *MapNode) IsNilOrEmpty() bool { - return mn == nil || mn.Value.IsNilOrEmpty() -} - -type MapNodeSlice []*MapNode - -func (m MapNodeSlice) Keys() []*RNode { - var keys []*RNode - for i := range m { - if m[i] != nil { - keys = append(keys, m[i].Key) - } - } - return keys -} - -func (m MapNodeSlice) Values() []*RNode { - var values []*RNode - for i := range m { - if m[i] != nil { - values = append(values, m[i].Value) - } else { - values = append(values, nil) - } - } - return values -} - -type TypeMeta struct { - Kind string - APIVersion string -} - -// ResourceMeta contains the metadata for a both Resource Type and Resource. -type ResourceMeta struct { - // APIVersion is the apiVersion field of a Resource - APIVersion string `yaml:"apiVersion,omitempty"` - // Kind is the kind field of a Resource - Kind string `yaml:"kind,omitempty"` - // ObjectMeta is the metadata field of a Resource - ObjectMeta `yaml:"metadata,omitempty"` -} - -// ObjectMeta contains metadata about a Resource -type ObjectMeta struct { - // Name is the metadata.name field of a Resource - Name string `yaml:"name,omitempty"` - // Namespace is the metadata.namespace field of a Resource - Namespace string `yaml:"namespace,omitempty"` - // Labels is the metadata.labels field of a Resource - Labels map[string]string `yaml:"labels,omitempty"` - // Annotations is the metadata.annotations field of a Resource. - Annotations map[string]string `yaml:"annotations,omitempty"` -} - -// GetIdentifier returns a ResourceIdentifier that includes -// the information needed to uniquely identify a resource in a cluster. -func (m *ResourceMeta) GetIdentifier() ResourceIdentifier { - return ResourceIdentifier{ - Name: m.Name, - Namespace: m.Namespace, - APIVersion: m.APIVersion, - Kind: m.Kind, - } -} - -// ResourceIdentifier contains the information needed to uniquely -// identify a resource in a cluster. -type ResourceIdentifier struct { - // Name is the name of the resource as set in metadata.name - Name string `yaml:"name,omitempty"` - // Namespace is the namespace of the resource as set in metadata.namespace - Namespace string `yaml:"namespace,omitempty"` - // ApiVersion is the apiVersion of the resource - APIVersion string `yaml:"apiVersion,omitempty"` - // Kind is the kind of the resource - Kind string `yaml:"kind,omitempty"` -} - -func (r *ResourceIdentifier) GetName() string { - return r.Name -} - -func (r *ResourceIdentifier) GetNamespace() string { - return r.Namespace -} - -func (r *ResourceIdentifier) GetAPIVersion() string { - return r.APIVersion -} - -func (r *ResourceIdentifier) GetKind() string { - return r.Kind -} - var ErrMissingMetadata = fmt.Errorf("missing Resource metadata") // Field names @@ -528,37 +346,6 @@ func (rn *RNode) FieldPath() []string { return rn.fieldPath } -const ( - Trim = "Trim" - Flow = "Flow" -) - -// String returns a string value for a Node, applying the supplied formatting options -func String(node *yaml.Node, opts ...string) (string, error) { - if node == nil { - return "", nil - } - optsSet := sets.String{} - optsSet.Insert(opts...) - if optsSet.Has(Flow) { - oldStyle := node.Style - defer func() { - node.Style = oldStyle - }() - node.Style = yaml.FlowStyle - } - - b := &bytes.Buffer{} - e := NewEncoder(b) - err := e.Encode(node) - e.Close() - val := b.String() - if optsSet.Has(Trim) { - val = strings.TrimSpace(val) - } - return val, errors.Wrap(err) -} - // String returns string representation of the RNode func (rn *RNode) String() (string, error) { if rn == nil { diff --git a/kyaml/yaml/rnode_test.go b/kyaml/yaml/rnode_test.go index 7b13065d2..8906357d2 100644 --- a/kyaml/yaml/rnode_test.go +++ b/kyaml/yaml/rnode_test.go @@ -167,61 +167,6 @@ type: string assert.Equal(t, expected, actual) } -func TestIsYNodeTaggedNull(t *testing.T) { - if IsYNodeTaggedNull(nil) { - t.Fatalf("nil cannot be tagged null") - } - if IsYNodeTaggedNull(&Node{}) { - t.Fatalf("untagged node is not tagged") - } - if IsYNodeTaggedNull(&Node{Tag: NodeTagFloat}) { - t.Fatalf("float tagged node is not tagged") - } - if !IsYNodeTaggedNull(&Node{Tag: NodeTagNull}) { - t.Fatalf("tagged node is tagged") - } -} - -func TestIsYNodeEmptyMap(t *testing.T) { - if IsYNodeEmptyMap(nil) { - t.Fatalf("nil cannot be a map") - } - if IsYNodeEmptyMap(&Node{}) { - t.Fatalf("raw node is not a map") - } - if IsYNodeEmptyMap(&Node{Kind: SequenceNode}) { - t.Fatalf("seq node is not a map") - } - n := &Node{Kind: MappingNode} - if !IsYNodeEmptyMap(n) { - t.Fatalf("empty mapping node is an empty mapping node") - } - n.Content = append(n.Content, &Node{Kind: SequenceNode}) - if IsYNodeEmptyMap(n) { - t.Fatalf("a node with content isn't empty") - } -} - -func TestIsYNodeEmptySeq(t *testing.T) { - if IsYNodeEmptySeq(nil) { - t.Fatalf("nil cannot be a map") - } - if IsYNodeEmptySeq(&Node{}) { - t.Fatalf("raw node is not a map") - } - if IsYNodeEmptySeq(&Node{Kind: MappingNode}) { - t.Fatalf("map node is not a sequence") - } - n := &Node{Kind: SequenceNode} - if !IsYNodeEmptySeq(n) { - t.Fatalf("empty sequence node is an empty sequence node") - } - n.Content = append(n.Content, &Node{Kind: MappingNode}) - if IsYNodeEmptySeq(n) { - t.Fatalf("a node with content isn't empty") - } -} - func TestIsMissingOrNull(t *testing.T) { if !IsMissingOrNull(nil) { t.Fatalf("input: nil") @@ -356,46 +301,3 @@ func TestRNodeIsNilOrEmpty(t *testing.T) { t.Fatalf("non-empty list should not be empty") } } - -func TestMapNodeIsNilOrEmpty(t *testing.T) { - var mn *MapNode - - if !mn.IsNilOrEmpty() { - t.Fatalf("nil should be empty") - } - - mn = &MapNode{Key: MakeNullNode()} - if !mn.IsNilOrEmpty() { - t.Fatalf("missing value should be empty") - } - - mn.Value = NewRNode(nil) - if !mn.IsNilOrEmpty() { - t.Fatalf("missing value YNode should be empty") - } - - mn.Value = MakeNullNode() - if !mn.IsNilOrEmpty() { - t.Fatalf("value tagged null should be empty") - } - - mn.Value = NewMapRNode(nil) - if !mn.IsNilOrEmpty() { - t.Fatalf("empty map should be empty") - } - - mn.Value = NewMapRNode(&map[string]string{"foo": "bar"}) - if mn.IsNilOrEmpty() { - t.Fatalf("non-empty map should not be empty") - } - - mn.Value = NewListRNode() - if !mn.IsNilOrEmpty() { - t.Fatalf("empty list should be empty") - } - - mn.Value = NewListRNode("foo") - if mn.IsNilOrEmpty() { - t.Fatalf("non-empty list should not be empty") - } -}