Skip to content

Commit

Permalink
Handle array items with a custom marshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs committed Apr 5, 2017
1 parent 3157125 commit bba6acb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func isPrimitive(mtype reflect.Type) bool {
case reflect.String:
return true
case reflect.Struct:
return mtype == timeType
return mtype == timeType || isCustomMarshaler(mtype)
default:
return false
}
Expand Down
15 changes: 10 additions & 5 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,8 @@ func TestNestedUnmarshal(t *testing.T) {
}

type customMarshalerParent struct {
Person customMarshaler `toml:"name"`
Self customMarshaler `toml:"me"`
Friends []customMarshaler `toml:"friends"`
}

type customMarshaler struct {
Expand All @@ -551,8 +552,12 @@ func (c customMarshaler) MarshalTOML() ([]byte, error) {

var customMarshalerData = customMarshaler{FirsName: "Sally", LastName: "Fields"}
var customMarshalerToml = []byte(`Sally Fields`)
var nestedCustomMarshalerData = customMarshalerParent{Person: customMarshalerData}
var nestedCustomMarshalerToml = []byte(`name = "Sally Fields"
var nestedCustomMarshalerData = customMarshalerParent{
Self: customMarshaler{FirsName: "Maiku", LastName: "Suteda"},
Friends: []customMarshaler{customMarshalerData},
}
var nestedCustomMarshalerToml = []byte(`friends = ["Sally Fields"]
me = "Maiku Suteda"
`)

func TestCustomMarshaler(t *testing.T) {
Expand All @@ -562,7 +567,7 @@ func TestCustomMarshaler(t *testing.T) {
}
expected := customMarshalerToml
if !bytes.Equal(result, expected) {
t.Errorf("Bad custom marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result)
t.Errorf("Bad custom marshaler: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result)
}
}

Expand All @@ -573,6 +578,6 @@ func TestNestedCustomMarshaler(t *testing.T) {
}
expected := nestedCustomMarshalerToml
if !bytes.Equal(result, expected) {
t.Errorf("Bad nested custom marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result)
t.Errorf("Bad nested custom marshaler: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result)
}
}

0 comments on commit bba6acb

Please sign in to comment.