Add option to disallow unknown fields

This commit is contained in:
Jack Lee
2023-02-21 02:02:04 -05:00
parent 7fa4b32dd0
commit e717b9c6db
3 changed files with 32 additions and 5 deletions

View File

@ -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)
}
}