Skip to content

Commit

Permalink
add function for normalizing app specific JSON
Browse files Browse the repository at this point in the history
Signed-off-by: John Barker <jebarker@gmail.com>
  • Loading branch information
se3000 committed Jul 13, 2018
1 parent 00ed2c6 commit 525e760
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
14 changes: 14 additions & 0 deletions store/models/job_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,17 @@ func (bt *BridgeType) UnmarshalJSON(input []byte) error {
bt.DefaultConfirmations = aux.DefaultConfirmations
return nil
}

func NormalizeSpecJSON(s string) (string, error) {
var jsr JobSpecRequest
if err := json.Unmarshal([]byte(s), &jsr); err != nil {
return "", err
}

js, err := json.Marshal(&jsr)
if err != nil {
return "", err
}

return utils.NormalizedJSONString(js)
}
24 changes: 24 additions & 0 deletions store/models/job_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,27 @@ func TestTask_UnmarshalJSON(t *testing.T) {
})
}
}

func TestNormalizeSpecJSON(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input string
want string
}{
{"identitiy", `{"endAt":"2018-07-02T21:51:36Z"}`, `{"endAt":"2018-07-02T21:51:36Z","initiators":null,"startAt":null,"tasks":null}`},
{"ordering of keys", `{"startAt":"2018-07-02T21:51:36Z","endAt":"2018-07-02T21:51:36Z"}`, `{"endAt":"2018-07-02T21:51:36Z","initiators":null,"startAt":"2018-07-02T21:51:36Z","tasks":null}`},
{"task type normalization", `{"tasks":[{"type":"noOp"}]}`, `{"endAt":null,"initiators":null,"startAt":null,"tasks":[{"confirmations":0.000000e+00,"type":"noop"}]}`},
{"initiator type normalization", `{"initiators":[{"type":"runLog"}]}`, `{"endAt":null,"initiators":[{"address":"0x0000000000000000000000000000000000000000","id":0.000000e+00,"jobId":"","time":"0001-01-01T00:00:00Z","type":"runlog"}],"startAt":null,"tasks":null}`},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := models.NormalizeSpecJSON(test.input)

assert.Equal(t, test.want, result)
assert.NoError(t, err)
})
}
}
10 changes: 6 additions & 4 deletions utils/json_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ func NormalizedJSON(object interface{}) (string, error) {
return "", err
}

return NormalizedJSONString(jsonBytes)
}

func NormalizedJSONString(val []byte) (string, error) {
// Unmarshal into a generic interface{}
var data interface{}
err = json.Unmarshal(jsonBytes, &data)
if err != nil {
if err := json.Unmarshal(val, &data); err != nil {
return "", err
}

Expand All @@ -60,8 +63,7 @@ func NormalizedJSON(object interface{}) (string, error) {
defer wc.Close()

// Now marshal the generic interface
err = marshal(wc, data)
if err != nil {
if err := marshal(wc, data); err != nil {
return "", err
}
wc.Close()
Expand Down

0 comments on commit 525e760

Please sign in to comment.