Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KnownFields() #1008

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"fmt"
"os"
"reflect"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -2811,6 +2812,82 @@ func (s *S) TestNodeEncodeDecode(c *C) {
}
}

var nodeDecodeKnownFieldsTests = []struct {
knownFields bool
node yaml.Node
value interface{}
error string
}{{
false,
yaml.Node{
Kind: yaml.MappingNode,
Tag: "!!map",
Content: []*yaml.Node{{
Kind: yaml.ScalarNode,
Value: "a",
Tag: "!!str",
}, {
Kind: yaml.ScalarNode,
Value: "b",
Tag: "!!str",
}, {
Kind: yaml.ScalarNode,
Value: "c",
Tag: "!!str",
}, {
Kind: yaml.ScalarNode,
Value: "d",
Tag: "!!str",
}},
},
struct{A string}{},
"",
}, {
true,
yaml.Node{
Kind: yaml.MappingNode,
Tag: "!!map",
Content: []*yaml.Node{{
Kind: yaml.ScalarNode,
Value: "a",
Tag: "!!str",
}, {
Kind: yaml.ScalarNode,
Value: "b",
Tag: "!!str",
}, {
Kind: yaml.ScalarNode,
Value: "c",
Tag: "!!str",
}, {
Kind: yaml.ScalarNode,
Value: "d",
Tag: "!!str",
}},
},
struct{A string}{},
"yaml: unmarshal errors:\n line 0: field c not found in type struct { A string }",
}}

func (s *S) TestNodeDecodeKnownFIelds(c *C) {
for i, item := range nodeDecodeKnownFieldsTests {
c.Logf("Decode KnownFields test value #%d: %#v", i, item.knownFields)

if item.knownFields {
item.node.KnownFields(true)
}

t := reflect.ValueOf(item.value).Type()
value := reflect.New(t)
err := item.node.Decode(value.Interface())
if item.error != "" {
c.Assert(err, ErrorMatches, item.error)
} else {
c.Assert(err, IsNil)
}
}
}

func (s *S) TestNodeZeroEncodeDecode(c *C) {
// Zero node value behaves as nil when encoding...
var n yaml.Node
Expand Down
9 changes: 9 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (dec *Decoder) Decode(v interface{}) (err error) {
// conversion of YAML into a Go value.
func (n *Node) Decode(v interface{}) (err error) {
d := newDecoder()
d.knownFields = n.knownFields
defer handleErr(&err)
out := reflect.ValueOf(v)
if out.Kind() == reflect.Ptr && !out.IsNil() {
Expand Down Expand Up @@ -413,6 +414,8 @@ type Node struct {
// These fields are not respected when encoding the node.
Line int
Column int

knownFields bool
}

// IsZero returns whether the node has all of its fields unset.
Expand Down Expand Up @@ -482,6 +485,12 @@ func (n *Node) SetString(s string) {
}
}

// KnownFields ensures that the keys in decoded mappings to
// exist as fields in the struct being decoded into.
func (n *Node) KnownFields(enable bool) {
n.knownFields = enable
}

// --------------------------------------------------------------------------
// Maintain a mapping of keys to structure field indexes

Expand Down