-
Notifications
You must be signed in to change notification settings - Fork 19
feat: implementing OptimizelyJSON #250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
262b61f
feat: implementing OptimizelyJSON
pawels-optimizely 79f1074
Merge branch 'master' into pawel/OASIS-6225
pawels-optimizely fe90bf4
addressing PR comments
pawels-optimizely 57eacb5
having sting payload as underlying data
pawels-optimizely b4e092d
Merge branch 'master' into pawel/OASIS-6225
pawels-optimizely 544e383
added another constructor that takes map as the parameter
pawels-optimizely 6ec55d8
Merge branch 'pawel/OASIS-6225' of github.com:optimizely/go-sdk into …
pawels-optimizely 8f7aa73
checking errors too
pawels-optimizely 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 hidden or 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,119 @@ | ||
| /**************************************************************************** | ||
| * Copyright 2020, Optimizely, Inc. and contributors * | ||
| * * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| * you may not use this file except in compliance with the License. * | ||
| * You may obtain a copy of the License at * | ||
| * * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| * * | ||
| * Unless required by applicable law or agreed to in writing, software * | ||
| * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| * See the License for the specific language governing permissions and * | ||
| * limitations under the License. * | ||
| ***************************************************************************/ | ||
|
|
||
| // Package optimizelyjson // | ||
| package optimizelyjson | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // OptimizelyJSON holds the underlying structure of the object | ||
| type OptimizelyJSON struct { | ||
| payload string | ||
|
|
||
| data map[string]interface{} | ||
| } | ||
|
|
||
| // NewOptimizelyJSONfromString constructs the object out of string payload | ||
| func NewOptimizelyJSONfromString(payload string) *OptimizelyJSON { | ||
| return &OptimizelyJSON{payload: payload} | ||
| } | ||
|
|
||
| // NewOptimizelyJSONfromMap constructs the object | ||
| func NewOptimizelyJSONfromMap(data map[string]interface{}) *OptimizelyJSON { | ||
| return &OptimizelyJSON{data: data} | ||
| } | ||
|
|
||
| // ToString returns the string representation of json | ||
| func (optlyJson *OptimizelyJSON) ToString() (string, error) { | ||
| if optlyJson.payload == "" { | ||
| jsonBytes, err := json.Marshal(optlyJson.data) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| optlyJson.payload = string(jsonBytes) | ||
|
|
||
| } | ||
| return optlyJson.payload, nil | ||
| } | ||
|
|
||
| // ToMap returns the native representation of json (map of interface) | ||
| func (optlyJson *OptimizelyJSON) ToMap() (map[string]interface{}, error) { | ||
| var err error | ||
| if optlyJson.data == nil { | ||
| err = json.Unmarshal([]byte(optlyJson.payload), &optlyJson.data) | ||
| } | ||
| return optlyJson.data, err | ||
| } | ||
|
|
||
| // GetValue populates the schema passed by the user - it takes primitive types and complex struct type | ||
| func (optlyJson *OptimizelyJSON) GetValue(jsonPath string, schema interface{}) error { | ||
|
|
||
| populateSchema := func(v interface{}) error { | ||
| jsonBytes, err := json.Marshal(v) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| err = json.Unmarshal(jsonBytes, schema) | ||
| return err | ||
| } | ||
|
|
||
| if jsonPath == "" { // populate the whole schema | ||
| return json.Unmarshal([]byte(optlyJson.payload), schema) | ||
| } | ||
|
|
||
| if optlyJson.data == nil { | ||
| err := json.Unmarshal([]byte(optlyJson.payload), &optlyJson.data) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| splitJSONPath := strings.Split(jsonPath, ".") | ||
| lastIndex := len(splitJSONPath) - 1 | ||
|
|
||
| internalMap := optlyJson.data | ||
|
|
||
| for i := 0; i < len(splitJSONPath); i++ { | ||
|
|
||
| if splitJSONPath[i] == "" { | ||
| return errors.New("json key cannot be empty") | ||
| } | ||
|
|
||
| if item, ok := internalMap[splitJSONPath[i]]; ok { | ||
| switch v := item.(type) { | ||
|
|
||
| case map[string]interface{}: | ||
| internalMap = v | ||
| if i == lastIndex { | ||
| return populateSchema(v) | ||
| } | ||
|
|
||
| default: | ||
| if i == lastIndex { | ||
| return populateSchema(v) | ||
| } | ||
| } | ||
| } else { | ||
| return fmt.Errorf(`json key "%s" not found`, splitJSONPath[i]) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,235 @@ | ||
| package optimizelyjson | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/suite" | ||
| "testing" | ||
| ) | ||
|
|
||
| type OptimizelyJsonTestSuite struct { | ||
| suite.Suite | ||
| data map[string]interface{} | ||
| dynamicList []interface{} | ||
| optimizelyJson *OptimizelyJSON | ||
| payload string | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) SetupTest() { | ||
|
|
||
| suite.dynamicList = []interface{}{"1", "2", 3.01, 4.23, true} | ||
| suite.payload = `{"field1":1,"field2":2.5,"field3":"three","field4":{"inner_field1":3,"inner_field2":["1","2",3.01,4.23,true]},"field5":true,"field6":null}` | ||
|
|
||
| suite.data = map[string]interface{}{ | ||
| "field1": 1.0, | ||
| "field2": 2.5, | ||
| "field3": "three", | ||
| "field4": map[string]interface{}{"inner_field1": 3.0, "inner_field2": suite.dynamicList}, | ||
| "field5": true, | ||
| "field6": nil, | ||
| } | ||
| suite.optimizelyJson = NewOptimizelyJSONfromString(suite.payload) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestConstructors() { | ||
|
|
||
| object1 := NewOptimizelyJSONfromString(suite.payload) | ||
| object2 := NewOptimizelyJSONfromMap(suite.data) | ||
|
|
||
| object1ToString, err1Str := object1.ToString() | ||
| object2ToString, err2Str := object2.ToString() | ||
| suite.NoError(err1Str) | ||
| suite.NoError(err2Str) | ||
| suite.Equal(object1ToString, object2ToString) | ||
|
|
||
| object1ToMap, err1Map := object1.ToMap() | ||
| object2ToMap, err2Map := object2.ToMap() | ||
| suite.NoError(err1Map) | ||
| suite.NoError(err2Map) | ||
| suite.Equal(object1ToMap, object2ToMap) | ||
|
|
||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestToMap() { | ||
|
|
||
| returnValue, err := suite.optimizelyJson.ToMap() | ||
| suite.NoError(err) | ||
| suite.Equal(suite.data, returnValue) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestToString() { | ||
|
|
||
| returnValue, err := suite.optimizelyJson.ToString() | ||
| suite.NoError(err) | ||
| expected := `{"field1":1,"field2":2.5,"field3":"three","field4":{"inner_field1":3,"inner_field2":["1","2",3.01,4.23,true]},"field5":true,"field6":null}` | ||
| suite.Equal(expected, returnValue) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueInvalidJsonKeyEmptySchema() { | ||
| emptyStruct := struct{}{} | ||
| err := suite.optimizelyJson.GetValue("some_key", &emptyStruct) | ||
| suite.Error(err) | ||
| suite.Equal(`json key "some_key" not found`, err.Error()) | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueInvalidJsonMultipleKeyEmptySchema() { | ||
| emptyStruct := struct{}{} | ||
| err := suite.optimizelyJson.GetValue("field3.some_key", &emptyStruct) | ||
| suite.Error(err) | ||
| suite.Equal(`json key "some_key" not found`, err.Error()) | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyEmptySchema() { | ||
| emptyStruct := struct{}{} | ||
pawels-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| err := suite.optimizelyJson.GetValue("field4", &emptyStruct) | ||
| suite.NoError(err) | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonMultipleKeyWrongSchema() { | ||
| emptyStruct := struct{}{} | ||
| err := suite.optimizelyJson.GetValue("field4.inner_field1", &emptyStruct) | ||
| suite.Error(err) // cannot unmarshal number into a struct | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonMultipleKeyValidSchema() { | ||
| var intValue int | ||
| err := suite.optimizelyJson.GetValue("field4.inner_field1", &intValue) | ||
| suite.NoError(err) | ||
| suite.Equal(3, intValue) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonMultipleKeyValidGenericSchema() { | ||
| var value interface{} | ||
| err := suite.optimizelyJson.GetValue("field4.inner_field2", &value) | ||
| suite.NoError(err) | ||
| suite.Equal(suite.dynamicList, value) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyIntValue() { | ||
| var intValue int | ||
| err := suite.optimizelyJson.GetValue("field1", &intValue) | ||
| suite.NoError(err) | ||
| suite.Equal(1, intValue) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyDoubleValue() { | ||
| var doubleValue float64 | ||
| err := suite.optimizelyJson.GetValue("field2", &doubleValue) | ||
| suite.NoError(err) | ||
| suite.Equal(2.5, doubleValue) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyStringValue() { | ||
| var stringValue string | ||
| err := suite.optimizelyJson.GetValue("field3", &stringValue) | ||
| suite.NoError(err) | ||
| suite.Equal("three", stringValue) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyBoolValue() { | ||
| var boolValue bool | ||
| err := suite.optimizelyJson.GetValue("field5", &boolValue) | ||
| suite.NoError(err) | ||
| suite.Equal(true, boolValue) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyNullValue() { | ||
| emptyStruct := struct{}{} | ||
| err := suite.optimizelyJson.GetValue("field6", &emptyStruct) | ||
| suite.NoError(err) | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueInValidJsonKey() { | ||
| emptyStruct := struct{}{} | ||
| err := suite.optimizelyJson.GetValue("field4.", &emptyStruct) | ||
| suite.Error(err) | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueEmptyJsonKeyEmptySchema() { | ||
| emptyStruct := struct{}{} | ||
| err := suite.optimizelyJson.GetValue("", &emptyStruct) | ||
| suite.NoError(err) | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueEmptyJsonMultipleKeyEmptySchema() { | ||
| emptyStruct := struct{}{} | ||
| err := suite.optimizelyJson.GetValue("field4..some_field", &emptyStruct) | ||
| suite.Error(err) | ||
| suite.Equal(struct{}{}, emptyStruct) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueEmptyJsonKeyWholeSchema() { | ||
|
|
||
| type field4Struct struct { | ||
| InnerField1 int `json:"inner_field1"` | ||
| InnerField2 []interface{} `json:"inner_field2"` | ||
| } | ||
|
|
||
| type schema struct { | ||
| Field1 int | ||
| Field2 float64 | ||
| Field3 string | ||
| Field4 field4Struct | ||
| Field5 bool | ||
| Field6 interface{} | ||
| } | ||
| sc := schema{} | ||
| err := suite.optimizelyJson.GetValue("", &sc) | ||
| suite.NoError(err) | ||
|
|
||
| expected := schema{ | ||
| Field1: 1, | ||
| Field2: 2.5, | ||
| Field3: "three", | ||
| Field4: field4Struct{InnerField1: 3, InnerField2: suite.dynamicList}, | ||
| Field5: true, | ||
| Field6: nil, | ||
| } | ||
| suite.Equal(expected, sc) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyPartialSchema() { | ||
|
|
||
| type schema struct { | ||
| InnerField1 int `json:"inner_field1"` | ||
| InnerField2 []interface{} `json:"inner_field2"` | ||
| } | ||
|
|
||
| sc := schema{} | ||
| err := suite.optimizelyJson.GetValue("field4", &sc) | ||
| suite.NoError(err) | ||
|
|
||
| expected := schema{ | ||
| InnerField1: 3, | ||
| InnerField2: suite.dynamicList, | ||
| } | ||
| suite.Equal(expected, sc) | ||
|
|
||
| // check if it does not destroy original object | ||
| err = suite.optimizelyJson.GetValue("field4", &sc) | ||
| suite.NoError(err) | ||
| suite.Equal(expected, sc) | ||
| } | ||
|
|
||
| func (suite *OptimizelyJsonTestSuite) TestGetValueValidJsonKeyArraySchema() { | ||
|
|
||
| var array []interface{} | ||
|
|
||
| err := suite.optimizelyJson.GetValue("field4.inner_field2", &array) | ||
| suite.NoError(err) | ||
|
|
||
| suite.Equal("1", array[0]) | ||
| suite.Equal("2", array[1]) | ||
| suite.Equal(3.01, array[2]) | ||
| suite.Equal(4.23, array[3]) | ||
|
|
||
| } | ||
|
|
||
| func TestOptimizelyJsonTestSuite(t *testing.T) { | ||
| suite.Run(t, new(OptimizelyJsonTestSuite)) | ||
| } | ||
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.
I am still not sure about it's name. It should have better name. since it's not returning anything.
may be
UnMarshalwould be a better name.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.
it's a suggestion, shouldn't block anything.