Skip to content

Commit

Permalink
Introduce ValidHoldingNode
Browse files Browse the repository at this point in the history
... for algorithms that are only applicable to Nodes that hold exactly
one value: documents, array items, and map items.
  • Loading branch information
jtigger committed Jul 8, 2021
1 parent 2dfc0d5 commit 4dc320c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
14 changes: 6 additions & 8 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -120,17 +120,15 @@ 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
}

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)
Expand All @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/yamlmeta/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pkg/yamlmeta/value_holding_node.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4dc320c

Please sign in to comment.