Add option to disallow unknown fields
This commit is contained in:
@ -370,12 +370,12 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to decode tag %q: %w", tn, err)
|
||||
}
|
||||
} else {
|
||||
if err := d.rawRead(tt); err != nil {
|
||||
} else if d.disallowUnknownFields {
|
||||
return fmt.Errorf("unknown field %q", tn)
|
||||
} else if err := d.rawRead(tt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
case reflect.Map:
|
||||
if val.Type().Key().Kind() != reflect.String {
|
||||
return errors.New("cannot parse TagCompound as " + val.Type().String())
|
||||
|
@ -3,10 +3,12 @@ package nbt
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -428,3 +430,21 @@ func TestDecoder_Decode_textUnmarshaler(t *testing.T) {
|
||||
t.Errorf("b should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecoder_Decode_ErrorUnknownField(t *testing.T) {
|
||||
data := []byte{
|
||||
TagCompound, 0, 1, 'S',
|
||||
TagByte, 0, 1, 'A', 1,
|
||||
TagByte, 0, 1, 'B', 2,
|
||||
TagEnd,
|
||||
}
|
||||
var v struct {
|
||||
A byte `nbt:"a"`
|
||||
}
|
||||
d := NewDecoder(bytes.NewReader(data))
|
||||
d.DisallowUnknownFields()
|
||||
if _, err := d.Decode(&v); err == nil || !strings.Contains(err.Error(), "unknown field") {
|
||||
t.Errorf("should return an error unmarshalling unknown field")
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ type DecoderReader = interface {
|
||||
}
|
||||
type Decoder struct {
|
||||
r DecoderReader
|
||||
disallowUnknownFields bool
|
||||
}
|
||||
|
||||
func NewDecoder(r io.Reader) *Decoder {
|
||||
@ -46,6 +47,12 @@ func NewDecoder(r io.Reader) *Decoder {
|
||||
return d
|
||||
}
|
||||
|
||||
// DisallowUnknownFields makes the decoder return an error when unmarshalling a compound
|
||||
// tag item that has a tag name not present in the destination struct.
|
||||
func (d *Decoder) DisallowUnknownFields() {
|
||||
d.disallowUnknownFields = true
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
io.Reader
|
||||
}
|
||||
|
Reference in New Issue
Block a user