From 525e760d5cabbea5e87494fd18791890b790c284 Mon Sep 17 00:00:00 2001 From: Steve Ellis Date: Mon, 2 Jul 2018 15:16:16 -0700 Subject: [PATCH] add function for normalizing app specific JSON Signed-off-by: John Barker --- store/models/job_spec.go | 14 ++++++++++++++ store/models/job_spec_test.go | 24 ++++++++++++++++++++++++ utils/json_digest.go | 10 ++++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/store/models/job_spec.go b/store/models/job_spec.go index 9f56d52ac09..02b6b63f9b7 100644 --- a/store/models/job_spec.go +++ b/store/models/job_spec.go @@ -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) +} diff --git a/store/models/job_spec_test.go b/store/models/job_spec_test.go index a193bed456c..fcbb6631a01 100644 --- a/store/models/job_spec_test.go +++ b/store/models/job_spec_test.go @@ -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) + }) + } +} diff --git a/utils/json_digest.go b/utils/json_digest.go index d7068d3e89d..f9721565aef 100644 --- a/utils/json_digest.go +++ b/utils/json_digest.go @@ -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 } @@ -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()