Minor update
This commit is contained in:
parent
2878ad6a27
commit
18b8290e86
@ -7,11 +7,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Eval is a half-baked json path implementation that needs some love
|
||||||
func Eval(path string, b []byte) (string, error) {
|
func Eval(path string, b []byte) (string, error) {
|
||||||
var object interface{}
|
var object interface{}
|
||||||
err := json.Unmarshal(b, &object)
|
err := json.Unmarshal(b, &object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Try to unmarshall it into an array instead
|
// Try to unmarshal it into an array instead
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return walk(path, object)
|
return walk(path, object)
|
||||||
@ -20,8 +21,6 @@ func Eval(path string, b []byte) (string, error) {
|
|||||||
func walk(path string, object interface{}) (string, error) {
|
func walk(path string, object interface{}) (string, error) {
|
||||||
keys := strings.Split(path, ".")
|
keys := strings.Split(path, ".")
|
||||||
currentKey := keys[0]
|
currentKey := keys[0]
|
||||||
// if there's more than one key, then walk deeper
|
|
||||||
if len(keys) > 1 {
|
|
||||||
switch value := extractValue(currentKey, object).(type) {
|
switch value := extractValue(currentKey, object).(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
return walk(strings.Replace(path, fmt.Sprintf("%s.", currentKey), "", 1), value)
|
return walk(strings.Replace(path, fmt.Sprintf("%s.", currentKey), "", 1), value)
|
||||||
@ -30,9 +29,6 @@ func walk(path string, object interface{}) (string, error) {
|
|||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("couldn't walk through '%s' because type was '%T', but expected 'map[string]interface{}'", currentKey, value)
|
return "", fmt.Errorf("couldn't walk through '%s' because type was '%T', but expected 'map[string]interface{}'", currentKey, value)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// if there's only one key and the target key is that key, then return its value
|
|
||||||
return fmt.Sprintf("%v", extractValue(currentKey, object)), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractValue(currentKey string, value interface{}) interface{} {
|
func extractValue(currentKey string, value interface{}) interface{} {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user