Skip to content

Commit

Permalink
Fixed the broken json marshalling.
Browse files Browse the repository at this point in the history
  • Loading branch information
zond committed May 16, 2020
1 parent f148d1a commit edf7ee4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion godip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
45 changes: 45 additions & 0 deletions godip_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit edf7ee4

Please sign in to comment.