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

Make yaml.Node.Decode customizable with decoder options #1017

Open
wants to merge 1 commit 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
29 changes: 29 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,35 @@ func (s *S) TestUnmarshalKnownFields(c *C) {
}
}

type decoderOptionsTest struct {
Foo int
Bar string
}

// contrived example only for the sake of showcasing the
// custom unmarshalling logic
func (i *decoderOptionsTest) UnmarshalYAML(data *yaml.Node) error {
repr := struct {
Foo int
Bar string
}{}
if err := data.Decode(&repr, yaml.NodeDecoderOptions.KnownFields); err != nil {
return err
}
i.Foo = repr.Foo
i.Bar = repr.Bar
return nil
}

func (s *S) TestDecoderOptions(c *C) {
testData := "foo: 42\nbar: \"bar\"\nsome_random_field: 666"
out := decoderOptionsTest{}
dec := yaml.NewDecoder(bytes.NewBuffer([]byte(testData)))
dec.KnownFields(true)
err := dec.Decode(&out)
c.Assert(err, ErrorMatches, "yaml: unmarshal errors:\n line 3: field some_random_field not found in type struct { Foo int; Bar string }")
}

type textUnmarshaler struct {
S string
}
Expand Down
19 changes: 18 additions & 1 deletion yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,29 @@ func (dec *Decoder) Decode(v interface{}) (err error) {
return nil
}

// NodeDecoderOption allows configuring decoder with custom options.
//
// It is especially useful when implementing UnmarshalYAML for custom data types.
type NodeDecoderOption func(d *decoder)

// NodeDecoderOptions stores all currently available decoder configuration options.
var NodeDecoderOptions = struct {
KnownFields NodeDecoderOption
}{
KnownFields: func(d *decoder) {
d.knownFields = true
},
}

// Decode decodes the node and stores its data into the value pointed to by v.
//
// See the documentation for Unmarshal for details about the
// conversion of YAML into a Go value.
func (n *Node) Decode(v interface{}) (err error) {
func (n *Node) Decode(v interface{}, nodeDecoderOptions ...NodeDecoderOption) (err error) {
d := newDecoder()
for _, opt := range nodeDecoderOptions {
opt(d)
}
defer handleErr(&err)
out := reflect.ValueOf(v)
if out.Kind() == reflect.Ptr && !out.IsNil() {
Expand Down