Skip to content

Commit 5fb6764

Browse files
committed
Add cases for non-compliant struct tag
Add test cases to decode_test.go and encode_test.go that cover an edge case described in issue #157 where a struct field uses an incorrect tag syntax (a bare string like `bar` without the `yaml:` prefix) alongside a correct `yaml:"foo"` tag. The new tests assert that the library continues to accept and round-trip values when one field uses the supported-but-nonstandard tag form. This clarifies expected behavior and prevents regressions for legacy struct tags that some users rely on.
1 parent 779357c commit 5fb6764

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

decode_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,19 @@ ba?r: a?bc
960960
"ba?r": "a?bc",
961961
},
962962
},
963+
964+
// issue #157
965+
{
966+
`foo: foo
967+
bar: bar`,
968+
struct {
969+
F string `yaml:"foo"` // correct tag, because it has yaml prefix
970+
B string `bar` //nolint:govet // incorrect tag, but supported
971+
}{
972+
F: "foo",
973+
B: "bar",
974+
},
975+
},
963976
}
964977

965978
type M map[string]any

encode_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,18 @@ var marshalTests = []struct {
582582
},
583583
"foo:\n bar: a?bc\n",
584584
},
585+
586+
// issue $157
587+
{
588+
struct {
589+
F string `yaml:"foo"` // the correct tag
590+
B string `bar` // the incorrect tag, but supported
591+
}{
592+
F: "foo",
593+
B: "bar",
594+
},
595+
"foo: foo\nbar: bar\n",
596+
},
585597
}
586598

587599
func TestMarshal(t *testing.T) {

0 commit comments

Comments
 (0)