Fix unmarshaler error when a custom marshaler function is defined (#383)

Fixes #382
This commit is contained in:
x-hgg-x
2020-05-04 21:05:45 +02:00
committed by GitHub
parent e7d1a179ae
commit 9ccd9bbc7a
2 changed files with 75 additions and 4 deletions
+48 -1
View File
@@ -911,6 +911,9 @@ var nestedCustomMarshalerData = customMarshalerParent{
var nestedCustomMarshalerToml = []byte(`friends = ["Sally Fields"]
me = "Maiku Suteda"
`)
var nestedCustomMarshalerTomlForUnmarshal = []byte(`[friends]
FirstName = "Sally"
LastName = "Fields"`)
func TestCustomMarshaler(t *testing.T) {
result, err := Marshal(customMarshalerData)
@@ -946,6 +949,26 @@ func TestTextMarshaler(t *testing.T) {
}
}
func TestUnmarshalTextMarshaler(t *testing.T) {
var nested = struct {
Friends textMarshaler `toml:"friends"`
}{}
var expected = struct {
Friends textMarshaler `toml:"friends"`
}{
Friends: textMarshaler{FirstName: "Sally", LastName: "Fields"},
}
err := Unmarshal(nestedCustomMarshalerTomlForUnmarshal, &nested)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(nested, expected) {
t.Errorf("Bad unmarshal: expected %v, got %v", expected, nested)
}
}
func TestNestedTextMarshaler(t *testing.T) {
var parent = struct {
Self textMarshaler `toml:"me"`
@@ -1002,7 +1025,7 @@ type customPointerMarshaler struct {
LastName string
}
func (m *customPointerMarshaler) MarshalText() ([]byte, error) {
func (m *customPointerMarshaler) MarshalTOML() ([]byte, error) {
return []byte("hidden"), nil
}
@@ -1048,6 +1071,30 @@ stranger = "hidden"
}
}
func TestPointerCustomMarshalerSequence(t *testing.T) {
var customPointerMarshalerSlice *[]*customPointerMarshaler
var customPointerMarshalerArray *[2]*customPointerMarshaler
if !isCustomMarshalerSequence(reflect.TypeOf(customPointerMarshalerSlice)) {
t.Errorf("error: should be a sequence of custom marshaler interfaces")
}
if !isCustomMarshalerSequence(reflect.TypeOf(customPointerMarshalerArray)) {
t.Errorf("error: should be a sequence of custom marshaler interfaces")
}
}
func TestPointerTextMarshalerSequence(t *testing.T) {
var textPointerMarshalerSlice *[]*textPointerMarshaler
var textPointerMarshalerArray *[2]*textPointerMarshaler
if !isTextMarshalerSequence(reflect.TypeOf(textPointerMarshalerSlice)) {
t.Errorf("error: should be a sequence of text marshaler interfaces")
}
if !isTextMarshalerSequence(reflect.TypeOf(textPointerMarshalerArray)) {
t.Errorf("error: should be a sequence of text marshaler interfaces")
}
}
var commentTestToml = []byte(`
# it's a comment on type
[postgres]