2020-04-11 02:56:38 +00:00
|
|
|
package jsonpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-04-14 23:20:00 +00:00
|
|
|
"strconv"
|
2020-04-11 02:56:38 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-15 00:31:30 +00:00
|
|
|
// Eval is a half-baked json path implementation that needs some love
|
2020-08-13 01:42:13 +00:00
|
|
|
func Eval(path string, b []byte) (string, int, error) {
|
2020-04-14 23:20:00 +00:00
|
|
|
var object interface{}
|
2020-04-11 02:56:38 +00:00
|
|
|
err := json.Unmarshal(b, &object)
|
|
|
|
if err != nil {
|
2020-04-15 00:31:30 +00:00
|
|
|
// Try to unmarshal it into an array instead
|
2020-08-13 01:42:13 +00:00
|
|
|
return "", 0, err
|
2020-04-11 02:56:38 +00:00
|
|
|
}
|
|
|
|
return walk(path, object)
|
|
|
|
}
|
|
|
|
|
2020-08-13 01:42:13 +00:00
|
|
|
func walk(path string, object interface{}) (string, int, error) {
|
2022-10-19 19:52:20 +00:00
|
|
|
var keys []string
|
|
|
|
startOfCurrentKey, bracketDepth := 0, 0
|
|
|
|
for i := range path {
|
|
|
|
if path[i] == '[' {
|
|
|
|
bracketDepth++
|
|
|
|
} else if path[i] == ']' {
|
|
|
|
bracketDepth--
|
|
|
|
}
|
|
|
|
// If we encounter a dot, we've reached the end of a key unless we're inside a bracket
|
|
|
|
if path[i] == '.' && bracketDepth == 0 {
|
|
|
|
keys = append(keys, path[startOfCurrentKey:i])
|
|
|
|
startOfCurrentKey = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if startOfCurrentKey <= len(path) {
|
|
|
|
keys = append(keys, path[startOfCurrentKey:])
|
|
|
|
}
|
2020-04-14 23:20:00 +00:00
|
|
|
currentKey := keys[0]
|
2020-04-15 00:31:30 +00:00
|
|
|
switch value := extractValue(currentKey, object).(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
return walk(strings.Replace(path, fmt.Sprintf("%s.", currentKey), "", 1), value)
|
2020-08-13 01:42:13 +00:00
|
|
|
case string:
|
2021-06-05 22:41:42 +00:00
|
|
|
if len(keys) > 1 {
|
|
|
|
return "", 0, fmt.Errorf("couldn't walk through '%s', because '%s' was a string instead of an object", keys[1], currentKey)
|
|
|
|
}
|
2020-08-13 01:42:13 +00:00
|
|
|
return value, len(value), nil
|
|
|
|
case []interface{}:
|
|
|
|
return fmt.Sprintf("%v", value), len(value), nil
|
2020-04-15 00:31:30 +00:00
|
|
|
case interface{}:
|
2020-08-13 01:42:13 +00:00
|
|
|
return fmt.Sprintf("%v", value), 1, nil
|
2020-04-15 00:31:30 +00:00
|
|
|
default:
|
2020-08-13 01:42:13 +00:00
|
|
|
return "", 0, fmt.Errorf("couldn't walk through '%s' because type was '%T', but expected 'map[string]interface{}'", currentKey, value)
|
2020-04-14 23:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractValue(currentKey string, value interface{}) interface{} {
|
|
|
|
// Check if the current key ends with [#]
|
|
|
|
if strings.HasSuffix(currentKey, "]") && strings.Contains(currentKey, "[") {
|
2022-10-19 19:52:20 +00:00
|
|
|
var isNestedArray bool
|
|
|
|
var index string
|
|
|
|
startOfBracket, endOfBracket, bracketDepth := 0, 0, 0
|
|
|
|
for i := range currentKey {
|
|
|
|
if currentKey[i] == '[' {
|
|
|
|
startOfBracket = i
|
|
|
|
bracketDepth++
|
|
|
|
} else if currentKey[i] == ']' && bracketDepth == 1 {
|
|
|
|
bracketDepth--
|
|
|
|
endOfBracket = i
|
|
|
|
index = currentKey[startOfBracket+1 : i]
|
|
|
|
if len(currentKey) > i+1 && currentKey[i+1] == '[' {
|
|
|
|
isNestedArray = true // there's more keys.
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arrayIndex, err := strconv.Atoi(index)
|
2020-04-14 23:20:00 +00:00
|
|
|
if err != nil {
|
2021-05-16 02:38:13 +00:00
|
|
|
return nil
|
2020-04-14 23:20:00 +00:00
|
|
|
}
|
2022-10-19 19:52:20 +00:00
|
|
|
currentKeyWithoutIndex := currentKey[:startOfBracket]
|
|
|
|
// if currentKeyWithoutIndex contains only an index (i.e. [0] or 0)
|
|
|
|
if len(currentKeyWithoutIndex) == 0 {
|
2020-04-14 23:20:00 +00:00
|
|
|
array := value.([]interface{})
|
|
|
|
if len(array) > arrayIndex {
|
2022-10-19 19:52:20 +00:00
|
|
|
if isNestedArray {
|
|
|
|
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
2020-04-14 23:20:00 +00:00
|
|
|
}
|
|
|
|
return array[arrayIndex]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-19 19:52:20 +00:00
|
|
|
if value == nil || value.(map[string]interface{})[currentKeyWithoutIndex] == nil {
|
2020-07-24 17:20:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-19 19:52:20 +00:00
|
|
|
// if currentKeyWithoutIndex contains both a key and an index (i.e. data[0])
|
|
|
|
array := value.(map[string]interface{})[currentKeyWithoutIndex].([]interface{})
|
2020-04-14 23:20:00 +00:00
|
|
|
if len(array) > arrayIndex {
|
2022-10-19 19:52:20 +00:00
|
|
|
if isNestedArray {
|
|
|
|
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
2020-04-14 23:20:00 +00:00
|
|
|
}
|
|
|
|
return array[arrayIndex]
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-11 02:56:38 +00:00
|
|
|
}
|
2020-04-14 23:20:00 +00:00
|
|
|
return value.(map[string]interface{})[currentKey]
|
2020-04-11 02:56:38 +00:00
|
|
|
}
|