2020-04-11 04:56:38 +02:00
|
|
|
package jsonpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-04-15 01:20:00 +02:00
|
|
|
"strconv"
|
2020-04-11 04:56:38 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-15 02:31:30 +02:00
|
|
|
// Eval is a half-baked json path implementation that needs some love
|
2020-08-13 03:42:13 +02:00
|
|
|
func Eval(path string, b []byte) (string, int, error) {
|
2022-11-04 01:46:35 +01:00
|
|
|
if len(path) == 0 && !(len(b) != 0 && b[0] == '[' && b[len(b)-1] == ']') {
|
|
|
|
// if there's no path AND the value is not a JSON array, then there's nothing to walk
|
|
|
|
return string(b), len(b), nil
|
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
var object interface{}
|
2022-11-04 01:46:35 +01:00
|
|
|
if err := json.Unmarshal(b, &object); err != nil {
|
2020-08-13 03:42:13 +02:00
|
|
|
return "", 0, err
|
2020-04-11 04:56:38 +02:00
|
|
|
}
|
|
|
|
return walk(path, object)
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:25:40 +01:00
|
|
|
// walk traverses the object and returns the value as a string as well as its length
|
2020-08-13 03:42:13 +02:00
|
|
|
func walk(path string, object interface{}) (string, int, error) {
|
2022-10-19 21:52:20 +02: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-15 01:20:00 +02:00
|
|
|
currentKey := keys[0]
|
2020-04-15 02:31:30 +02:00
|
|
|
switch value := extractValue(currentKey, object).(type) {
|
|
|
|
case map[string]interface{}:
|
2022-11-19 23:25:40 +01:00
|
|
|
newPath := strings.Replace(path, fmt.Sprintf("%s.", currentKey), "", 1)
|
|
|
|
if path == newPath {
|
|
|
|
// If the path hasn't changed, it means we're at the end of the path
|
|
|
|
// So we'll treat it as a string by re-marshaling it to JSON since it's a map.
|
|
|
|
// Note that the output JSON will be minified.
|
|
|
|
b, err := json.Marshal(value)
|
|
|
|
return string(b), len(b), err
|
|
|
|
}
|
|
|
|
return walk(newPath, value)
|
2020-08-13 03:42:13 +02:00
|
|
|
case string:
|
2021-06-06 00:41:42 +02: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 03:42:13 +02:00
|
|
|
return value, len(value), nil
|
|
|
|
case []interface{}:
|
|
|
|
return fmt.Sprintf("%v", value), len(value), nil
|
2020-04-15 02:31:30 +02:00
|
|
|
case interface{}:
|
2022-11-19 23:25:40 +01:00
|
|
|
newValue := fmt.Sprintf("%v", value)
|
|
|
|
return newValue, len(newValue), nil
|
2020-04-15 02:31:30 +02:00
|
|
|
default:
|
2020-08-13 03:42:13 +02:00
|
|
|
return "", 0, fmt.Errorf("couldn't walk through '%s' because type was '%T', but expected 'map[string]interface{}'", currentKey, value)
|
2020-04-15 01:20:00 +02: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 21:52:20 +02: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-15 01:20:00 +02:00
|
|
|
if err != nil {
|
2021-05-16 04:38:13 +02:00
|
|
|
return nil
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
2022-10-19 21:52:20 +02:00
|
|
|
currentKeyWithoutIndex := currentKey[:startOfBracket]
|
|
|
|
// if currentKeyWithoutIndex contains only an index (i.e. [0] or 0)
|
|
|
|
if len(currentKeyWithoutIndex) == 0 {
|
2022-12-23 15:55:17 +01:00
|
|
|
array, _ := value.([]interface{})
|
2020-04-15 01:20:00 +02:00
|
|
|
if len(array) > arrayIndex {
|
2022-10-19 21:52:20 +02:00
|
|
|
if isNestedArray {
|
|
|
|
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
return array[arrayIndex]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-19 21:52:20 +02:00
|
|
|
if value == nil || value.(map[string]interface{})[currentKeyWithoutIndex] == nil {
|
2020-07-24 19:20:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-19 21:52:20 +02:00
|
|
|
// if currentKeyWithoutIndex contains both a key and an index (i.e. data[0])
|
2022-12-23 15:55:17 +01:00
|
|
|
array, _ := value.(map[string]interface{})[currentKeyWithoutIndex].([]interface{})
|
2020-04-15 01:20:00 +02:00
|
|
|
if len(array) > arrayIndex {
|
2022-10-19 21:52:20 +02:00
|
|
|
if isNestedArray {
|
|
|
|
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
return array[arrayIndex]
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-11 04:56:38 +02:00
|
|
|
}
|
2022-11-04 01:46:35 +01:00
|
|
|
if valueAsSlice, ok := value.([]interface{}); ok {
|
|
|
|
// If the type is a slice, return it
|
|
|
|
return valueAsSlice
|
|
|
|
}
|
|
|
|
// otherwise, it's a map
|
2020-04-15 01:20:00 +02:00
|
|
|
return value.(map[string]interface{})[currentKey]
|
2020-04-11 04:56:38 +02:00
|
|
|
}
|