From b61fe18b8c7a6adbb1ad4a6294c5e68a3da9a706 Mon Sep 17 00:00:00 2001 From: Inteon <42113979+inteon@users.noreply.github.com> Date: Sun, 31 Oct 2021 11:49:19 +0100 Subject: [PATCH 1/2] go fmt Signed-off-by: Inteon <42113979+inteon@users.noreply.github.com> --- apic.go | 8 ++++---- decode_test.go | 26 ++++++++++++++------------ emitterc.go | 2 +- go.mod | 8 ++++---- go.sum | 2 ++ node_test.go | 36 ++++++++++++++++++------------------ readerc.go | 8 ++++---- scannerc.go | 8 ++++---- writerc.go | 8 ++++---- yaml.go | 5 ++--- yamlh.go | 1 - yamlprivateh.go | 20 ++++++++++---------- 12 files changed, 67 insertions(+), 65 deletions(-) create mode 100644 go.sum diff --git a/apic.go b/apic.go index ae7d049f..05fd305d 100644 --- a/apic.go +++ b/apic.go @@ -1,17 +1,17 @@ -// +// // Copyright (c) 2011-2019 Canonical Ltd // Copyright (c) 2006-2010 Kirill Simonov -// +// // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE diff --git a/decode_test.go b/decode_test.go index 5ade5500..a5dfadca 100644 --- a/decode_test.go +++ b/decode_test.go @@ -767,7 +767,7 @@ var unmarshalTests = []struct { M{"a": 123456e1}, }, { "a: 123456E1\n", - M{"a": 123456E1}, + M{"a": 123456e1}, }, // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes { @@ -802,7 +802,6 @@ var unmarshalTests = []struct { "c": []interface{}{"d", "e"}, }, }, - } type M map[string]interface{} @@ -950,14 +949,14 @@ var unmarshalErrorTests = []struct { {"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"}, { "a: &a [00,00,00,00,00,00,00,00,00]\n" + - "b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" + - "c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" + - "d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" + - "e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" + - "f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" + - "g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" + - "h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" + - "i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n", + "b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" + + "c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" + + "d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" + + "e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" + + "f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" + + "g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" + + "h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" + + "i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n", "yaml: document contains excessive aliasing", }, } @@ -1436,7 +1435,10 @@ func (s *S) TestMergeStruct(c *C) { } } -var unmarshalNullTests = []struct{ input string; pristine, expected func() interface{} }{{ +var unmarshalNullTests = []struct { + input string + pristine, expected func() interface{} +}{{ "null", func() interface{} { var v interface{}; v = "v"; return &v }, func() interface{} { var v interface{}; v = nil; return &v }, @@ -1487,7 +1489,7 @@ func (s *S) TestUnmarshalNull(c *C) { func (s *S) TestUnmarshalPreservesData(c *C) { var v struct { A, B int - C int `yaml:"-"` + C int `yaml:"-"` } v.A = 42 v.C = 88 diff --git a/emitterc.go b/emitterc.go index 0f47c9ca..f6e50b5b 100644 --- a/emitterc.go +++ b/emitterc.go @@ -241,7 +241,7 @@ func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool emitter.indent += 2 } else { // Everything else aligns to the chosen indentation. - emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent) + emitter.indent = emitter.best_indent * ((emitter.indent + emitter.best_indent) / emitter.best_indent) } } return true diff --git a/go.mod b/go.mod index f407ea32..40805254 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ -module "gopkg.in/yaml.v3" +module gopkg.in/yaml.v3 -require ( - "gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405 -) +go 1.17 + +require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..d277f45c --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/node_test.go b/node_test.go index b7d0c9a3..f15042b1 100644 --- a/node_test.go +++ b/node_test.go @@ -688,17 +688,17 @@ var nodeTests = []struct { Line: 3, Column: 4, }, { - Kind: yaml.ScalarNode, - Tag: "!!str", - Value: "c", + Kind: yaml.ScalarNode, + Tag: "!!str", + Value: "c", LineComment: "# IC", - Line: 5, - Column: 1, + Line: 5, + Column: 1, }, { - Kind: yaml.SequenceNode, - Tag: "!!seq", - Line: 6, - Column: 3, + Kind: yaml.SequenceNode, + Tag: "!!seq", + Line: 6, + Column: 3, Content: []*yaml.Node{{ Kind: yaml.ScalarNode, Tag: "!!str", @@ -707,17 +707,17 @@ var nodeTests = []struct { Column: 5, }}, }, { - Kind: yaml.ScalarNode, - Tag: "!!str", - Value: "d", + Kind: yaml.ScalarNode, + Tag: "!!str", + Value: "d", LineComment: "# ID", - Line: 7, - Column: 1, + Line: 7, + Column: 1, }, { - Kind: yaml.MappingNode, - Tag: "!!map", - Line: 8, - Column: 3, + Kind: yaml.MappingNode, + Tag: "!!map", + Line: 8, + Column: 3, Content: []*yaml.Node{{ Kind: yaml.ScalarNode, Tag: "!!str", diff --git a/readerc.go b/readerc.go index b7de0a89..56af2453 100644 --- a/readerc.go +++ b/readerc.go @@ -1,17 +1,17 @@ -// +// // Copyright (c) 2011-2019 Canonical Ltd // Copyright (c) 2006-2010 Kirill Simonov -// +// // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE diff --git a/scannerc.go b/scannerc.go index ca007010..037fcd53 100644 --- a/scannerc.go +++ b/scannerc.go @@ -2847,7 +2847,7 @@ func yaml_parser_scan_line_comment(parser *yaml_parser_t, token_mark yaml_mark_t continue } if parser.buffer[parser.buffer_pos+peek] == '#' { - seen := parser.mark.index+peek + seen := parser.mark.index + peek for { if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { return false @@ -2876,7 +2876,7 @@ func yaml_parser_scan_line_comment(parser *yaml_parser_t, token_mark yaml_mark_t parser.comments = append(parser.comments, yaml_comment_t{ token_mark: token_mark, start_mark: start_mark, - line: text, + line: text, }) } return true @@ -2910,7 +2910,7 @@ func yaml_parser_scan_comments(parser *yaml_parser_t, scan_mark yaml_mark_t) boo // the foot is the line below it. var foot_line = -1 if scan_mark.line > 0 { - foot_line = parser.mark.line-parser.newlines+1 + foot_line = parser.mark.line - parser.newlines + 1 if parser.newlines == 0 && parser.mark.column > 1 { foot_line++ } @@ -2996,7 +2996,7 @@ func yaml_parser_scan_comments(parser *yaml_parser_t, scan_mark yaml_mark_t) boo recent_empty = false // Consume until after the consumed comment line. - seen := parser.mark.index+peek + seen := parser.mark.index + peek for { if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { return false diff --git a/writerc.go b/writerc.go index b8a116bf..266d0b09 100644 --- a/writerc.go +++ b/writerc.go @@ -1,17 +1,17 @@ -// +// // Copyright (c) 2011-2019 Canonical Ltd // Copyright (c) 2006-2010 Kirill Simonov -// +// // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE diff --git a/yaml.go b/yaml.go index 8cec6da4..3ba221c2 100644 --- a/yaml.go +++ b/yaml.go @@ -363,7 +363,7 @@ const ( // Address yaml.Node // } // err := yaml.Unmarshal(data, &person) -// +// // Or by itself: // // var person Node @@ -373,7 +373,7 @@ type Node struct { // Kind defines whether the node is a document, a mapping, a sequence, // a scalar value, or an alias to another node. The specific data type of // scalar nodes may be obtained via the ShortTag and LongTag methods. - Kind Kind + Kind Kind // Style allows customizing the apperance of the node in the tree. Style Style @@ -421,7 +421,6 @@ func (n *Node) IsZero() bool { n.HeadComment == "" && n.LineComment == "" && n.FootComment == "" && n.Line == 0 && n.Column == 0 } - // LongTag returns the long form of the tag that indicates the data type for // the node. If the Tag field isn't explicitly defined, one will be computed // based on the node properties. diff --git a/yamlh.go b/yamlh.go index 7c6d0077..9e9afc9e 100644 --- a/yamlh.go +++ b/yamlh.go @@ -639,7 +639,6 @@ type yaml_parser_t struct { } type yaml_comment_t struct { - scan_mark yaml_mark_t // Position where scanning for comments started token_mark yaml_mark_t // Position after which tokens will be associated with this comment start_mark yaml_mark_t // Position of '#' comment mark diff --git a/yamlprivateh.go b/yamlprivateh.go index e88f9c54..dea1ba96 100644 --- a/yamlprivateh.go +++ b/yamlprivateh.go @@ -1,17 +1,17 @@ -// +// // Copyright (c) 2011-2019 Canonical Ltd // Copyright (c) 2006-2010 Kirill Simonov -// +// // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -137,8 +137,8 @@ func is_crlf(b []byte, i int) bool { func is_breakz(b []byte, i int) bool { //return is_break(b, i) || is_z(b, i) return ( - // is_break: - b[i] == '\r' || // CR (#xD) + // is_break: + b[i] == '\r' || // CR (#xD) b[i] == '\n' || // LF (#xA) b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) @@ -151,8 +151,8 @@ func is_breakz(b []byte, i int) bool { func is_spacez(b []byte, i int) bool { //return is_space(b, i) || is_breakz(b, i) return ( - // is_space: - b[i] == ' ' || + // is_space: + b[i] == ' ' || // is_breakz: b[i] == '\r' || // CR (#xD) b[i] == '\n' || // LF (#xA) @@ -166,8 +166,8 @@ func is_spacez(b []byte, i int) bool { func is_blankz(b []byte, i int) bool { //return is_blank(b, i) || is_breakz(b, i) return ( - // is_blank: - b[i] == ' ' || b[i] == '\t' || + // is_blank: + b[i] == ' ' || b[i] == '\t' || // is_breakz: b[i] == '\r' || // CR (#xD) b[i] == '\n' || // LF (#xA) From baaeb5da4f5bd2b20fb8cd9fe331512840148946 Mon Sep 17 00:00:00 2001 From: Inteon <42113979+inteon@users.noreply.github.com> Date: Sun, 31 Oct 2021 12:03:46 +0100 Subject: [PATCH 2/2] consider 'unknown field' errors to be strict errors Signed-off-by: Inteon <42113979+inteon@users.noreply.github.com> --- decode.go | 13 ++++++++----- decode_test.go | 12 ++++++++---- yaml.go | 17 +++++++++-------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/decode.go b/decode.go index df36e3a3..8ccf115d 100644 --- a/decode.go +++ b/decode.go @@ -308,9 +308,10 @@ func (p *parser) mapping() *Node { // Decoder, unmarshals a node into a provided value. type decoder struct { - doc *Node - aliases map[*Node]bool - terrors []string + doc *Node + aliases map[*Node]bool + terrors []string + strictErrors []string stringMapType reflect.Type generalMapType reflect.Type @@ -361,6 +362,7 @@ func (d *decoder) callUnmarshaler(n *Node, u Unmarshaler) (good bool) { err := u.UnmarshalYAML(n) if e, ok := err.(*TypeError); ok { d.terrors = append(d.terrors, e.Errors...) + d.strictErrors = append(d.strictErrors, e.StrictErrors...) return false } if err != nil { @@ -377,12 +379,13 @@ func (d *decoder) callObsoleteUnmarshaler(n *Node, u obsoleteUnmarshaler) (good if len(d.terrors) > terrlen { issues := d.terrors[terrlen:] d.terrors = d.terrors[:terrlen] - return &TypeError{issues} + return &TypeError{issues, nil} } return nil }) if e, ok := err.(*TypeError); ok { d.terrors = append(d.terrors, e.Errors...) + d.strictErrors = append(d.strictErrors, e.StrictErrors...) return false } if err != nil { @@ -908,7 +911,7 @@ func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) { d.unmarshal(n.Content[i+1], value) inlineMap.SetMapIndex(name, value) } else if d.knownFields { - d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.Line, name.String(), out.Type())) + d.strictErrors = append(d.strictErrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.Line, name.String(), out.Type())) } } return true diff --git a/decode_test.go b/decode_test.go index a5dfadca..5142d13b 100644 --- a/decode_test.go +++ b/decode_test.go @@ -1110,8 +1110,8 @@ func (s *S) TestUnmarshalerWholeDocument(c *C) { } func (s *S) TestUnmarshalerTypeError(c *C) { - unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}} - unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}} + unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}, nil} + unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}, nil} defer func() { delete(unmarshalerResult, 2) delete(unmarshalerResult, 4) @@ -1141,8 +1141,8 @@ func (s *S) TestUnmarshalerTypeError(c *C) { } func (s *S) TestObsoleteUnmarshalerTypeError(c *C) { - unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}} - unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}} + unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}, nil} + unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}, nil} defer func() { delete(unmarshalerResult, 2) delete(unmarshalerResult, 4) @@ -1605,6 +1605,10 @@ func (s *S) TestUnmarshalKnownFields(c *C) { dec.KnownFields(item.known) err := dec.Decode(value.Interface()) c.Assert(err, ErrorMatches, item.error) + if !item.unique { + // If a strict error occurs, the decoding should still be successful + c.Assert(value.Elem().Interface(), DeepEquals, item.value) + } } } diff --git a/yaml.go b/yaml.go index 3ba221c2..0156aa61 100644 --- a/yaml.go +++ b/yaml.go @@ -129,8 +129,8 @@ func (dec *Decoder) Decode(v interface{}) (err error) { out = out.Elem() } d.unmarshal(node, out) - if len(d.terrors) > 0 { - return &TypeError{d.terrors} + if len(d.terrors) > 0 || len(d.strictErrors) > 0 { + return &TypeError{d.terrors, d.strictErrors} } return nil } @@ -147,8 +147,8 @@ func (n *Node) Decode(v interface{}) (err error) { out = out.Elem() } d.unmarshal(n, out) - if len(d.terrors) > 0 { - return &TypeError{d.terrors} + if len(d.terrors) > 0 || len(d.strictErrors) > 0 { + return &TypeError{d.terrors, d.strictErrors} } return nil } @@ -166,8 +166,8 @@ func unmarshal(in []byte, out interface{}, strict bool) (err error) { } d.unmarshal(node, v) } - if len(d.terrors) > 0 { - return &TypeError{d.terrors} + if len(d.terrors) > 0 || len(d.strictErrors) > 0 { + return &TypeError{d.terrors, d.strictErrors} } return nil } @@ -313,11 +313,12 @@ func failf(format string, args ...interface{}) { // types. When this error is returned, the value is still // unmarshaled partially. type TypeError struct { - Errors []string + Errors []string + StrictErrors []string } func (e *TypeError) Error() string { - return fmt.Sprintf("yaml: unmarshal errors:\n %s", strings.Join(e.Errors, "\n ")) + return fmt.Sprintf("yaml: unmarshal errors:\n %s", strings.Join(append(e.Errors, e.StrictErrors...), "\n ")) } type Kind uint32