-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a30e20d
commit 241f290
Showing
3 changed files
with
190 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package sirius | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type jsonNode map[string]interface{} | ||
|
||
func getJSONPathValue(path string, node jsonNode) interface{} { | ||
var currentNode = node | ||
var value interface{} | ||
keys := strings.Split(path, ".") | ||
for _, key := range keys { | ||
tempNode := getNodeValue(key, -1, currentNode) | ||
if tempNode == nil { | ||
value = currentNode[key] | ||
continue | ||
} | ||
currentNode = tempNode | ||
value = currentNode | ||
} | ||
return value | ||
} | ||
|
||
func getNodeValue(key string, arrayIndex int, node jsonNode) jsonNode { | ||
tempNode := getNode(node[key], arrayIndex) | ||
if tempNode == nil { | ||
tempKey, index, err := formatKey(key) | ||
if err != nil { | ||
return nil | ||
} | ||
return getNodeValue(tempKey, index, node) | ||
} | ||
return tempNode | ||
} | ||
|
||
func getNode(v interface{}, i int) jsonNode { | ||
switch node := v.(type) { | ||
case map[string]interface{}: | ||
configNode := jsonNode(node) | ||
return configNode | ||
case []interface{}: | ||
if len(node) <= i { | ||
return nil | ||
} | ||
if i == -1 { | ||
return nil | ||
} | ||
configNode := jsonNode(getNode(node[i], 0)) | ||
return configNode | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func formatKey(key string) (string, int, error) { | ||
i := strings.Index(key, "[") | ||
k := strings.Index(key, "]") | ||
if i == -1 { | ||
return "", 0, fmt.Errorf("Not format key for index") | ||
} | ||
formatKey := key[:i] | ||
if formatKey == "" { | ||
return "", 0, fmt.Errorf("Not format key") | ||
} | ||
index, err := strconv.Atoi(key[i+1 : k]) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
return formatKey, index, nil | ||
} |
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,3 @@ | ||
module github.com/jgolang/sirius | ||
|
||
go 1.15 |
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,114 @@ | ||
package sirius | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// SJson doc ... | ||
type SJson struct { | ||
Buf []byte | ||
node jsonNode | ||
} | ||
|
||
// Open doc ... | ||
func Open(name string) (*SJson, error) { | ||
jsonFile, err := os.Open(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer jsonFile.Close() | ||
|
||
buffer, err := ioutil.ReadAll(jsonFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var node jsonNode | ||
err = json.Unmarshal([]byte(buffer), &node) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sjson := SJson{ | ||
Buf: buffer, | ||
node: node, | ||
} | ||
return &sjson, nil | ||
} | ||
|
||
// Get doc .. | ||
func (g SJson) Get(key string) interface{} { | ||
return getJSONPathValue(key, g.node) | ||
} | ||
|
||
// GetString doc .. | ||
func (g SJson) GetString(key string) string { | ||
v := getJSONPathValue(key, g.node) | ||
str, ok := v.(string) | ||
if !ok { | ||
return "" | ||
} | ||
return str | ||
} | ||
|
||
// GetInt doc .. | ||
func (g SJson) GetInt(key string) int { | ||
v := getJSONPathValue(key, g.node) | ||
str := fmt.Sprintf("%v", v) | ||
valueInt, err := strconv.Atoi(str) | ||
if err != nil { | ||
return 0 | ||
} | ||
return valueInt | ||
} | ||
|
||
// GetInt64 doc .. | ||
func (g SJson) GetInt64(key string) int64 { | ||
v := getJSONPathValue(key, g.node) | ||
str := fmt.Sprintf("%v", v) | ||
valueInt64, err := strconv.ParseInt(str, 10, 64) | ||
if err != nil { | ||
return 0 | ||
} | ||
return valueInt64 | ||
} | ||
|
||
// GetFloat64 doc .. | ||
func (g SJson) GetFloat64(key string) float64 { | ||
v := getJSONPathValue(key, g.node) | ||
valueFloat64, ok := v.(float64) | ||
if !ok { | ||
return 0.0 | ||
} | ||
return valueFloat64 | ||
} | ||
|
||
// GetBool doc .. | ||
func (g SJson) GetBool(key string) bool { | ||
v := getJSONPathValue(key, g.node) | ||
valueBool, ok := v.(bool) | ||
if !ok { | ||
return false | ||
} | ||
return valueBool | ||
} | ||
|
||
// GetArrayMaps doc .. | ||
func (g SJson) GetArrayMaps(key string) []map[string]interface{} { | ||
v := getJSONPathValue(key, g.node) | ||
value, ok := v.([]interface{}) | ||
if !ok { | ||
return nil | ||
} | ||
var a []map[string]interface{} | ||
for _, m := range value { | ||
nm, ok := m.(map[string]interface{}) | ||
if !ok { | ||
return nil | ||
} | ||
a = append(a, nm) | ||
} | ||
return a | ||
} |