From edf7ee44a8c4c4f394a0c82fa8e27486438c98c6 Mon Sep 17 00:00:00 2001 From: Martin Bruse Date: Sat, 16 May 2020 02:16:01 +0200 Subject: [PATCH] Fixed the broken json marshalling. --- godip.go | 2 +- godip_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/godip.go b/godip.go index 375742ed..2e3e41d5 100644 --- a/godip.go +++ b/godip.go @@ -378,7 +378,7 @@ func (self Options) MarshalJSON() ([]byte, error) { filter := "" if kVal.Type() == reflect.TypeOf(FilteredOptionValue{}) { filter = kVal.FieldByName("Filter").String() - kVal = kVal.FieldByName("Value") + kVal = reflect.ValueOf(kVal.FieldByName("Value").Interface()) } val := map[string]interface{}{ "Type": kVal.Type().Name(), diff --git a/godip_test.go b/godip_test.go index 807825e5..74fa9c27 100644 --- a/godip_test.go +++ b/godip_test.go @@ -1,11 +1,56 @@ package godip import ( + "encoding/json" "reflect" "sort" "testing" ) +func TestOptionsMarshal(t *testing.T) { + opts := Options{ + FilteredOptionValue{ + Filter: "MAX:Build:1", + Value: Province("spa"), + }: Options{ + OrderType("Build"): Options{ + UnitType("Army"): Options{ + SrcProvince("spa"): Options{}, + }, + }, + }, + } + b, err := json.MarshalIndent(opts, " ", " ") + if err != nil { + t.Fatal(err) + } + wanted := `{ + "spa": { + "Filter": "MAX:Build:1", + "Next": { + "Build": { + "Next": { + "Army": { + "Next": { + "spa": { + "Next": {}, + "Type": "SrcProvince" + } + }, + "Type": "UnitType" + } + }, + "Type": "OrderType" + } + }, + "Type": "Province" + } + }` + if string(b) != wanted { + t.Errorf("Bad json marshal, got %v, wanted %v", string(b), wanted) + } +} + func TestOptionsBubbleFilters(t *testing.T) { for _, tc := range []struct { opts Options