Skip to content

Commit

Permalink
Merge Go 1.10+ files
Browse files Browse the repository at this point in the history
go.mod requires Go 1.12, separate 1.10+ files are no longer needed.
This merges their contents with the main files.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Oct 8, 2024
1 parent c3772b5 commit 02c8e8f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 94 deletions.
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"
}

0 comments on commit 02c8e8f

Please sign in to comment.