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

Merge Go 1.10+ files #118

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
7 changes: 7 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,10 @@ func jsonToYAMLValue(j interface{}) interface{} {
}
return j
}

// DisallowUnknownFields configures the JSON decoder to error out if unknown
// fields come along, instead of dropping them by default.
func DisallowUnknownFields(d *json.Decoder) *json.Decoder {
d.DisallowUnknownFields()
return d
}
31 changes: 0 additions & 31 deletions yaml_go110.go

This file was deleted.

63 changes: 0 additions & 63 deletions yaml_go110_test.go

This file was deleted.

38 changes: 38 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,3 +949,41 @@ spec:
t.Fatalf("expected\n%s\ngot\n%s", string(v2output), v3output)
}
}

func TestUnmarshalWithTags(t *testing.T) {
type WithTaggedField struct {
Field string `json:"field"`
}

t.Run("Known tagged field", func(t *testing.T) {
y := []byte(`field: "hello"`)
v := WithTaggedField{}
if err := Unmarshal(y, &v, DisallowUnknownFields); err != nil {
t.Errorf("unexpected error: %v", err)
}
if v.Field != "hello" {
t.Errorf("v.Field=%v, want 'hello'", v.Field)
}

})
t.Run("With unknown tagged field", func(t *testing.T) {
y := []byte(`unknown: "hello"`)
v := WithTaggedField{}
err := Unmarshal(y, &v, DisallowUnknownFields)
if err == nil {
t.Errorf("want error because of unknown field, got <nil>: v=%#v", v)
}
})

}

func exampleUnknown() {
type WithTaggedField struct {
Field string `json:"field"`
}
y := []byte(`unknown: "hello"`)
v := WithTaggedField{}
fmt.Printf("%v\n", Unmarshal(y, &v, DisallowUnknownFields))
// Ouptut:
// unmarshaling JSON: while decoding JSON: json: unknown field "unknown"
}
Loading