Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions pkg/optimizelyjson/optimizely_json.go
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 {
Copy link
Contributor

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 UnMarshal would be a better name.

Copy link
Contributor

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.


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
}
235 changes: 235 additions & 0 deletions pkg/optimizelyjson/optimizely_json_test.go
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{}{}
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))
}