forked from optimizely/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizely_json.go
119 lines (97 loc) · 3.49 KB
/
optimizely_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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, error) {
var data map[string]interface{}
err := json.Unmarshal([]byte(payload), &data)
if err != nil {
return nil, err
}
return &OptimizelyJSON{payload: payload, data: data}, err
}
// 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{} {
return optlyJson.data
}
// 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
if optlyJson.payload != "" {
return json.Unmarshal([]byte(optlyJson.payload), schema)
}
return populateSchema(optlyJson.data)
}
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
}