Revert "Marshal: Be more lenient when unmarshal a struct (#2975)" (#2977)

This reverts commit b64f1d5dc9.

Reason: It is breaking samples/go/photo-index

Reopens: #2971
This commit is contained in:
Erik Arvidsson
2016-12-21 19:14:59 -08:00
committed by GitHub
parent b64f1d5dc9
commit 6bc82b03dd
2 changed files with 18 additions and 13 deletions

View File

@@ -20,7 +20,10 @@ import (
//
// To unmarshal a Noms struct into a Go struct, Unmarshal matches incoming
// object fields to the fields used by Marshal (either the struct field name or
// its tag). Unmarshal will only set exported fields of the struct. Go struct
// its tag). Unmarshal will only set exported fields of the struct. The name
// of the Go struct must match (ignoring case) the name of the Noms struct. All
// exported fields on the Go struct must be present in the Noms struct, unless
// the field on the Go struct is marked with the "omitempty" tag. Go struct
// fields also support the "original" tag which causes the Go field to receive
// the entire original unmarshaled Noms struct.
//
@@ -231,10 +234,11 @@ func (c *decoderCacheT) set(t reflect.Type, d decoderFunc) {
}
type decField struct {
name string
decoder decoderFunc
index int
original bool
name string
decoder decoderFunc
index int
omitEmpty bool
original bool
}
func structDecoder(t reflect.Type) decoderFunc {
@@ -258,10 +262,11 @@ func structDecoder(t reflect.Type) decoderFunc {
validateField(f, t)
fields = append(fields, decField{
name: tags.name,
decoder: typeDecoder(f.Type, tags),
index: i,
original: tags.original,
name: tags.name,
decoder: typeDecoder(f.Type, tags),
index: i,
omitEmpty: tags.omitEmpty,
original: tags.original,
})
}
@@ -283,6 +288,8 @@ func structDecoder(t reflect.Type) decoderFunc {
fv, ok := s.MaybeGet(f.name)
if ok {
f.decoder(fv, sf)
} else if !f.omitEmpty {
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ", missing field \"" + f.name + "\""})
}
}
}

View File

@@ -309,11 +309,9 @@ func TestDecodeMissingField(t *testing.T) {
B bool
}
var s S
Unmarshal(types.NewStruct("S", types.StructData{
assertDecodeErrorMessage(t, types.NewStruct("S", types.StructData{
"a": types.Number(42),
}), &s)
assert.Equal(t, int32(42), s.A)
assert.False(t, s.B)
}), &s, "Cannot unmarshal struct S {\n a: Number,\n} into Go value of type marshal.S, missing field \"b\"")
}
func TestDecodeEmbeddedStruct(tt *testing.T) {