-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix dredd test #76
Merged
Merged
Fix dredd test #76
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f92d9ac
Fix swagger format error.
zemyblue 4f40069
Fix dredd skip paths.
zemyblue bf5134d
Fix `/genesis` dredd test
zemyblue 322c603
Fix dredd fail case.
zemyblue dca2b0e
Fix dredd test case(2)
zemyblue 28d3ea4
Add fixed dredd test to circleCI
zemyblue 8b3d266
Add changelog
zemyblue 4491bc0
Remove unused RemoveProperty of unmarshal.go
zemyblue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package unmarshaler | ||
|
||
import ( | ||
"encoding/json" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type UnmarshalledArbitraryObject struct { | ||
Body interface{} | ||
} | ||
|
||
func (obj *UnmarshalledArbitraryObject) GetProperty(keys ...string) interface{} { | ||
body := obj.Body | ||
for _, key := range keys { | ||
body = body.(map[string]interface{})[key] | ||
} | ||
return body | ||
} | ||
|
||
func (obj *UnmarshalledArbitraryObject) SetProperty(keys []string, value interface{}) { | ||
prevKeys := keys[:len(keys)-1] | ||
lastKey := keys[len(keys)-1] | ||
|
||
body := obj.Body | ||
for _, key := range prevKeys { | ||
body = body.(map[string]interface{})[key] | ||
} | ||
body.(map[string]interface{})[lastKey] = value | ||
} | ||
|
||
func (obj *UnmarshalledArbitraryObject) DeleteProperty(keys ...string) { | ||
prevKeys := keys[:len(keys)-1] | ||
lastKey := keys[len(keys)-1] | ||
|
||
body := obj.Body | ||
for _, key := range prevKeys { | ||
body = body.(map[string]interface{})[key] | ||
} | ||
delete(body.(map[string]interface{}), lastKey) | ||
} | ||
|
||
func UnmarshalJSON(str *string) UnmarshalledArbitraryObject { | ||
return UnmarshalledArbitraryObject{unmarshalArbitraryFormat(json.Unmarshal, str)} | ||
} | ||
|
||
func UnmarshalYAML(str *string) UnmarshalledArbitraryObject { | ||
return UnmarshalledArbitraryObject{unmarshalArbitraryFormat(yaml.Unmarshal, str)} | ||
} | ||
|
||
func unmarshalArbitraryFormat(unmarshal func([]byte, interface{}) error, str *string) interface{} { | ||
var body interface{} | ||
err := unmarshal([]byte(*str), &body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return body | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package unmarshaler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnmarshalElementJSON(t *testing.T) { | ||
var unmarshalJSONTests = []struct { | ||
json string | ||
expected interface{} | ||
}{ | ||
{ | ||
`"30"`, | ||
"30", | ||
}, | ||
{ | ||
"30", | ||
float64(30), | ||
}, | ||
{ | ||
"null", | ||
nil, | ||
}, | ||
{ | ||
"[]", | ||
[]interface{}{}, | ||
}, | ||
{ | ||
`["a", "b", "c"]`, | ||
[]interface{}{"a", "b", "c"}, | ||
}, | ||
{ | ||
"{}", | ||
map[string]interface{}{}, | ||
}, | ||
{ | ||
`{"key1":"value1", "key2":"value2"}`, | ||
map[string]interface{}{"key1": "value1", "key2": "value2"}, | ||
}, | ||
{ | ||
testJSON1, | ||
map[string]interface{}{ | ||
"key1": float64(119), | ||
"sub1": map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": "value3"}}, | ||
"sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, | ||
}, | ||
{ | ||
testJSON2, | ||
map[string]interface{}{ | ||
"key1": float64(119), | ||
"sub1": []interface{}{map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": []interface{}{"value2"}}}}, | ||
"sub3": map[string]interface{}{"key4": "value2", "key5": "value2"}}, | ||
}, | ||
} | ||
|
||
for _, tt := range unmarshalJSONTests { | ||
t.Logf("unmarshal json test %s", tt.json) | ||
{ | ||
unmarshaledJSON := UnmarshalJSON(&tt.json) | ||
require.Equal(t, tt.expected, unmarshaledJSON.Body) | ||
} | ||
} | ||
} | ||
|
||
func TestUnmarshalElementYAML(t *testing.T) { | ||
var unmarshalJSONTests = []struct { | ||
yaml string | ||
expected interface{} | ||
}{ | ||
{ | ||
`"30"`, | ||
"30", | ||
}, | ||
{ | ||
"30", | ||
30, | ||
}, | ||
{ | ||
"null", | ||
nil, | ||
}, | ||
{ | ||
"[]", | ||
[]interface{}{}, | ||
}, | ||
{ | ||
`["a", "b", "c"]`, | ||
[]interface{}{"a", "b", "c"}, | ||
}, | ||
{ | ||
"{}", | ||
map[string]interface{}{}, | ||
}, | ||
{ | ||
"key1: value1\nkey2: value2", | ||
map[string]interface{}{"key1": "value1", "key2": "value2"}, | ||
}, | ||
{ | ||
testYAML1, | ||
map[string]interface{}{ | ||
"key1": 119, | ||
"sub1": map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": "value3"}}, | ||
"sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, | ||
}, | ||
{ | ||
testYAML2, | ||
map[string]interface{}{ | ||
"key1": 119, | ||
"sub1": []interface{}{map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": []interface{}{"value3"}}}}, | ||
"sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, | ||
}, | ||
} | ||
|
||
for _, tt := range unmarshalJSONTests { | ||
t.Logf("unmarshal yaml test %s", tt.yaml) | ||
{ | ||
unmarshaledYAML := UnmarshalYAML(&tt.yaml) | ||
require.Equal(t, tt.expected, unmarshaledYAML.Body) | ||
} | ||
} | ||
} | ||
|
||
func TestGetAndSetProperty(t *testing.T) { | ||
testJSON := testJSON1 | ||
unmarshaledJSON := UnmarshalJSON(&testJSON) | ||
require.Equal(t, float64(119), unmarshaledJSON.GetProperty("key1")) | ||
require.Equal(t, "value2", unmarshaledJSON.GetProperty("sub1", "key2")) | ||
require.Equal(t, "value3", unmarshaledJSON.GetProperty("sub1", "sub2", "key3")) | ||
require.Equal(t, "value4", unmarshaledJSON.GetProperty("sub3", "key4")) | ||
require.Equal(t, "value5", unmarshaledJSON.GetProperty("sub3", "key5")) | ||
|
||
unmarshaledJSON.SetProperty([]string{"key1"}, "newValue1") | ||
unmarshaledJSON.SetProperty([]string{"sub1", "sub2", "key3"}, "newValue2") | ||
|
||
require.Equal(t, "newValue1", unmarshaledJSON.GetProperty("key1")) | ||
require.Equal(t, "newValue2", unmarshaledJSON.GetProperty("sub1", "sub2", "key3")) | ||
} | ||
|
||
func TestDeleteProposer(t *testing.T) { | ||
testJSON := testJSON1 | ||
unmarshaledJSON := UnmarshalJSON(&testJSON) | ||
|
||
unmarshaledJSON.DeleteProperty("sub3", "key5") | ||
require.Nil(t, unmarshaledJSON.GetProperty("sub3", "key5")) | ||
} | ||
|
||
const ( | ||
testJSON1 = `{"key1":119, "sub1":{"key2":"value2", "sub2":{"key3":"value3"}}, "sub3":{"key4":"value4", "key5":"value5"}}` | ||
testJSON2 = `{"key1":119, "sub1":[{"key2":"value2", "sub2":{"key3":["value2"]}}], "sub3":{"key4":"value2", "key5":"value2"}}` | ||
testYAML1 = ` | ||
key1: 119 | ||
sub1: | ||
key2: value2 | ||
sub2: | ||
key3: value3 | ||
sub3: | ||
key4: value4 | ||
key5: value5 | ||
` | ||
testYAML2 = ` | ||
key1: 119 | ||
sub1: | ||
- key2: value2 | ||
sub2: | ||
key3: [value3] | ||
sub3: | ||
key4: value4 | ||
key5: value5 | ||
` | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to doing same thing with
RemoveProperty()
. Is it right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's right.
I update the function and didn't remove it.
I'll remove it. Thank you.