From e4fdfd39de3f56163377db40ef8541cefc6e17c9 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 16 Apr 2019 14:43:26 -0700 Subject: [PATCH 1/3] Allow unmarshaling to top level maps --- marshal.go | 14 +++++++++++--- marshal_test.go | 24 +++++++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/marshal.go b/marshal.go index 2571448a..8dba8465 100644 --- a/marshal.go +++ b/marshal.go @@ -514,11 +514,19 @@ func (d *Decoder) SetTagName(v string) *Decoder { func (d *Decoder) unmarshal(v interface{}) error { mtype := reflect.TypeOf(v) - if mtype.Kind() != reflect.Ptr || mtype.Elem().Kind() != reflect.Struct { - return errors.New("Only a pointer to struct can be unmarshaled from TOML") + if mtype.Kind() != reflect.Ptr { + return errors.New("Only a pointer to struct or map can be unmarshaled from TOML") } - sval, err := d.valueFromTree(mtype.Elem(), d.tval) + elem := mtype.Elem() + + switch elem.Kind() { + case reflect.Struct, reflect.Map: + default: + return errors.New("Only a pointer to struct or map can be unmarshaled from TOML") + } + + sval, err := d.valueFromTree(elem, d.tval) if err != nil { return err } diff --git a/marshal_test.go b/marshal_test.go index 712433ad..deac4926 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -1103,12 +1103,26 @@ func TestUnmarshalCustomTag(t *testing.T) { } func TestUnmarshalMap(t *testing.T) { - m := make(map[string]int) - m["a"] = 1 + testToml := []byte(` + a = 1 + b = 2 + c = 3 + `) + var result map[string]int + err := Unmarshal(testToml, &result) + if err != nil { + t.Errorf("Received unexpected error: %s", err) + return + } - err := Unmarshal(basicTestToml, m) - if err.Error() != "Only a pointer to struct can be unmarshaled from TOML" { - t.Fail() + expected := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + } + + if !reflect.DeepEqual(result, expected) { + t.Errorf("Bad unmarshal: expected %v, got %v", expected, result) } } From 690990074af81f8a8daf09157706f3b1e0646360 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Wed, 24 Apr 2019 23:02:44 -0700 Subject: [PATCH 2/3] Improve unmarshal input types test coverage --- marshal.go | 4 ++-- marshal_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/marshal.go b/marshal.go index 8dba8465..2e16a384 100644 --- a/marshal.go +++ b/marshal.go @@ -515,7 +515,7 @@ func (d *Decoder) SetTagName(v string) *Decoder { func (d *Decoder) unmarshal(v interface{}) error { mtype := reflect.TypeOf(v) if mtype.Kind() != reflect.Ptr { - return errors.New("Only a pointer to struct or map can be unmarshaled from TOML") + return errors.New("only a pointer to struct or map can be unmarshaled from TOML") } elem := mtype.Elem() @@ -523,7 +523,7 @@ func (d *Decoder) unmarshal(v interface{}) error { switch elem.Kind() { case reflect.Struct, reflect.Map: default: - return errors.New("Only a pointer to struct or map can be unmarshaled from TOML") + return errors.New("only a pointer to struct or map can be unmarshaled from TOML") } sval, err := d.valueFromTree(elem, d.tval) diff --git a/marshal_test.go b/marshal_test.go index deac4926..b489c74d 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -1126,6 +1126,23 @@ func TestUnmarshalMap(t *testing.T) { } } +func TestUnmarshalNonPointer(t *testing.T) { + a := 1 + err := Unmarshal([]byte{}, a) + if err == nil { + t.Fatal("unmarshal should err when given a non pointer") + } +} + + +func TestUnmarshalInvalidPointerKind(t *testing.T) { + a := 1 + err := Unmarshal([]byte{}, &a) + if err == nil { + t.Fatal("unmarshal should err when given an invalid pointer type") + } +} + func TestMarshalSlice(t *testing.T) { m := make([]int, 1) m[0] = 1 From 8cc41e520e1a58fc731db334ba8dfa0777144287 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Wed, 24 Apr 2019 23:09:33 -0700 Subject: [PATCH 3/3] Remove extra blank line --- marshal_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/marshal_test.go b/marshal_test.go index b489c74d..999235fa 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -1134,7 +1134,6 @@ func TestUnmarshalNonPointer(t *testing.T) { } } - func TestUnmarshalInvalidPointerKind(t *testing.T) { a := 1 err := Unmarshal([]byte{}, &a)