From 4dc320c55f50ded0d09540f3eef1ecdce76aaef4 Mon Sep 17 00:00:00 2001 From: John Ryan Date: Thu, 8 Jul 2021 12:12:30 -0700 Subject: [PATCH] Introduce ValidHoldingNode ... for algorithms that are only applicable to Nodes that hold exactly one value: documents, array items, and map items. --- pkg/schema/schema.go | 14 ++++++-------- pkg/yamlmeta/ast.go | 9 +++++++-- pkg/yamlmeta/value_holding_node.go | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 pkg/yamlmeta/value_holding_node.go diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 0721e7ee..338dcdd6 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -70,7 +70,7 @@ func NewNullSchema() *DocumentSchema { } func NewDocumentType(doc *yamlmeta.Document) (*DocumentType, error) { - typeOfValue, err := getTypeFromValueHoldingNode(doc) + typeOfValue, err := getType(doc) if err != nil { return nil, err } @@ -93,7 +93,7 @@ func NewMapType(m *yamlmeta.Map) (*MapType, error) { } func NewMapItemType(item *yamlmeta.MapItem) (*MapItemType, error) { - typeOfValue, err := getTypeFromValueHoldingNode(item) + typeOfValue, err := getType(item) if err != nil { return nil, err } @@ -120,7 +120,7 @@ func NewArrayType(a *yamlmeta.Array) (*ArrayType, error) { } func NewArrayItemType(item *yamlmeta.ArrayItem) (*ArrayItemType, error) { - typeOfValue, err := getTypeFromValueHoldingNode(item) + typeOfValue, err := getType(item) if err != nil { return nil, err } @@ -128,9 +128,7 @@ func NewArrayItemType(item *yamlmeta.ArrayItem) (*ArrayItemType, error) { return &ArrayItemType{ValueType: typeOfValue, defaultValue: typeOfValue.GetDefaultValue(), Position: item.GetPosition()}, nil } -// getTypeFromValueHoldingNode derives the yamlmeta.Type from the given `node`. -// a "value-holding node" is a node that holds an actual value: Document, ArrayItem, or MapItem -func getTypeFromValueHoldingNode(node yamlmeta.Node) (yamlmeta.Type, error) { +func getType(node yamlmeta.ValueHoldingNode) (yamlmeta.Type, error) { var typeOfValue yamlmeta.Type anns, err := collectAnnotations(node) @@ -140,13 +138,13 @@ func getTypeFromValueHoldingNode(node yamlmeta.Node) (yamlmeta.Type, error) { typeOfValue = getTypeFromAnnotations(anns) if typeOfValue == nil { - typeOfValue, err = inferTypeFromValue(node.GetValues()[0], node.GetPosition()) + typeOfValue, err = inferTypeFromValue(node.Val(), node.GetPosition()) if err != nil { return nil, err } } - err = valueTypeAllowsItemValue(typeOfValue, node.GetValues()[0], node.GetPosition()) + err = valueTypeAllowsItemValue(typeOfValue, node.Val(), node.GetPosition()) if err != nil { return nil, err } diff --git a/pkg/yamlmeta/ast.go b/pkg/yamlmeta/ast.go index 60f464a4..a8371b8e 100644 --- a/pkg/yamlmeta/ast.go +++ b/pkg/yamlmeta/ast.go @@ -30,8 +30,13 @@ type Node interface { sealed() // limit the concrete types of Node to map directly only to types allowed in YAML spec. } -// Ensure: all types are — in fact — assignable to Node -var _ = []Node{&DocumentSet{}, &Document{}, &Map{}, &MapItem{}, &Array{}, &ArrayItem{}} +type ValueHoldingNode interface { + Node + Val() interface{} +} + +var _ = []Node{&DocumentSet{}, &Map{}, &Array{}} +var _ = []ValueHoldingNode{&Document{}, &MapItem{}, &ArrayItem{}} type DocumentSet struct { Metas []*Meta diff --git a/pkg/yamlmeta/value_holding_node.go b/pkg/yamlmeta/value_holding_node.go new file mode 100644 index 00000000..760fb2c2 --- /dev/null +++ b/pkg/yamlmeta/value_holding_node.go @@ -0,0 +1,14 @@ +// Copyright 2020 VMware, Inc. +// SPDX-License-Identifier: Apache-2.0 + +package yamlmeta + +func (d *Document) Val() interface{} { + return d.Value +} +func (mi *MapItem) Val() interface{} { + return mi.Value +} +func (ai *ArrayItem) Val() interface{} { + return ai.Value +}