Reset
This commit is contained in:
131
jsonpath/jsonpath.go
Normal file
131
jsonpath/jsonpath.go
Normal file
@ -0,0 +1,131 @@
|
||||
package jsonpath
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Eval is a half-baked json path implementation that needs some love
|
||||
func Eval(path string, b []byte) (string, int, error) {
|
||||
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
|
||||
}
|
||||
var object interface{}
|
||||
if err := json.Unmarshal(b, &object); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
return walk(path, object)
|
||||
}
|
||||
|
||||
// walk traverses the object and returns the value as a string as well as its length
|
||||
func walk(path string, object interface{}) (string, int, error) {
|
||||
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:])
|
||||
}
|
||||
currentKey := keys[0]
|
||||
switch value := extractValue(currentKey, object).(type) {
|
||||
case map[string]interface{}:
|
||||
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)
|
||||
case string:
|
||||
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)
|
||||
}
|
||||
return value, len(value), nil
|
||||
case []interface{}:
|
||||
return fmt.Sprintf("%v", value), len(value), nil
|
||||
case interface{}:
|
||||
newValue := fmt.Sprintf("%v", value)
|
||||
return newValue, len(newValue), nil
|
||||
default:
|
||||
return "", 0, fmt.Errorf("couldn't walk through '%s' because type was '%T', but expected 'map[string]interface{}'", currentKey, value)
|
||||
}
|
||||
}
|
||||
|
||||
func extractValue(currentKey string, value interface{}) interface{} {
|
||||
// Check if the current key ends with [#]
|
||||
if strings.HasSuffix(currentKey, "]") && strings.Contains(currentKey, "[") {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
currentKeyWithoutIndex := currentKey[:startOfBracket]
|
||||
// if currentKeyWithoutIndex contains only an index (i.e. [0] or 0)
|
||||
if len(currentKeyWithoutIndex) == 0 {
|
||||
array, _ := value.([]interface{})
|
||||
if len(array) > arrayIndex {
|
||||
if isNestedArray {
|
||||
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
||||
}
|
||||
return array[arrayIndex]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if value == nil || value.(map[string]interface{})[currentKeyWithoutIndex] == nil {
|
||||
return nil
|
||||
}
|
||||
// if currentKeyWithoutIndex contains both a key and an index (i.e. data[0])
|
||||
array, _ := value.(map[string]interface{})[currentKeyWithoutIndex].([]interface{})
|
||||
if len(array) > arrayIndex {
|
||||
if isNestedArray {
|
||||
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
||||
}
|
||||
return array[arrayIndex]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if valueAsSlice, ok := value.([]interface{}); ok {
|
||||
// If the type is a slice, return it
|
||||
// This happens when the body (value) is a JSON array
|
||||
return valueAsSlice
|
||||
}
|
||||
if valueAsMap, ok := value.(map[string]interface{}); ok {
|
||||
// If the value is a map, then we get the currentKey from that map
|
||||
// This happens when the body (value) is a JSON object
|
||||
return valueAsMap[currentKey]
|
||||
}
|
||||
// If the value is neither a map, nor a slice, nor an index, then we cannot retrieve the currentKey
|
||||
// from said value. This usually happens when the body (value) is null.
|
||||
return value
|
||||
}
|
11
jsonpath/jsonpath_bench_test.go
Normal file
11
jsonpath/jsonpath_bench_test.go
Normal file
@ -0,0 +1,11 @@
|
||||
package jsonpath
|
||||
|
||||
import "testing"
|
||||
|
||||
func BenchmarkEval(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Eval("ids[0]", []byte(`{"ids": [1, 2]}`))
|
||||
Eval("long.simple.walk", []byte(`{"long": {"simple": {"walk": "value"}}}`))
|
||||
Eval("data[0].apps[1].name", []byte(`{"data": [{"apps": [{"name":"app1"}, {"name":"app2"}, {"name":"app3"}]}]}`))
|
||||
}
|
||||
}
|
196
jsonpath/jsonpath_test.go
Normal file
196
jsonpath/jsonpath_test.go
Normal file
@ -0,0 +1,196 @@
|
||||
package jsonpath
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEval(t *testing.T) {
|
||||
type Scenario struct {
|
||||
Name string
|
||||
Path string
|
||||
Data string
|
||||
ExpectedOutput string
|
||||
ExpectedOutputLength int
|
||||
ExpectedError bool
|
||||
}
|
||||
scenarios := []Scenario{
|
||||
{
|
||||
Name: "simple",
|
||||
Path: "key",
|
||||
Data: `{"key": "value"}`,
|
||||
ExpectedOutput: "value",
|
||||
ExpectedOutputLength: 5,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "simple-with-invalid-data",
|
||||
Path: "key",
|
||||
Data: "invalid data",
|
||||
ExpectedOutput: "",
|
||||
ExpectedOutputLength: 0,
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "invalid-path",
|
||||
Path: "key",
|
||||
Data: `{}`,
|
||||
ExpectedOutput: "",
|
||||
ExpectedOutputLength: 0,
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "long-simple-walk",
|
||||
Path: "long.simple.walk",
|
||||
Data: `{"long": {"simple": {"walk": "value"}}}`,
|
||||
ExpectedOutput: "value",
|
||||
ExpectedOutputLength: 5,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "array-of-objects",
|
||||
Path: "ids[1].id",
|
||||
Data: `{"ids": [{"id": 1}, {"id": 2}]}`,
|
||||
ExpectedOutput: "2",
|
||||
ExpectedOutputLength: 1,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "array-of-values",
|
||||
Path: "ids[0]",
|
||||
Data: `{"ids": [1, 2]}`,
|
||||
ExpectedOutput: "1",
|
||||
ExpectedOutputLength: 1,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "array-of-values-with-no-path",
|
||||
Path: "",
|
||||
Data: `[1, 2]`,
|
||||
ExpectedOutput: "[1 2]", // the output is an array
|
||||
ExpectedOutputLength: 2,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "array-of-values-and-invalid-index",
|
||||
Path: "ids[wat]",
|
||||
Data: `{"ids": [1, 2]}`,
|
||||
ExpectedOutput: "",
|
||||
ExpectedOutputLength: 0,
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "array-of-values-at-root",
|
||||
Path: "[1]",
|
||||
Data: `[1, 2]`,
|
||||
ExpectedOutput: "2",
|
||||
ExpectedOutputLength: 1,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "array-of-objects-at-root",
|
||||
Path: "[0]",
|
||||
Data: `[{"id": 1}, {"id": 2}]`,
|
||||
ExpectedOutput: `{"id":1}`,
|
||||
ExpectedOutputLength: 8,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "array-of-objects-with-int-at-root",
|
||||
Path: "[0].id",
|
||||
Data: `[{"id": 1}, {"id": 2}]`,
|
||||
ExpectedOutput: "1",
|
||||
ExpectedOutputLength: 1,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "array-of-objects-at-root-and-invalid-index",
|
||||
Path: "[5].id",
|
||||
Data: `[{"id": 1}, {"id": 2}]`,
|
||||
ExpectedOutput: "",
|
||||
ExpectedOutputLength: 0,
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "long-walk-and-array",
|
||||
Path: "data.ids[0].id",
|
||||
Data: `{"data": {"ids": [{"id": 1}, {"id": 2}, {"id": 3}]}}`,
|
||||
ExpectedOutput: "1",
|
||||
ExpectedOutputLength: 1,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "nested-array",
|
||||
Path: "[3][2]",
|
||||
Data: `[[1, 2], [3, 4], [], [5, 6, 7]]`,
|
||||
ExpectedOutput: "7",
|
||||
ExpectedOutputLength: 1,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "object-with-nested-arrays",
|
||||
Path: "data[1][1]",
|
||||
Data: `{"data": [["a", "b", "c"], ["d", "eeeee", "f"]]}`,
|
||||
ExpectedOutput: "eeeee",
|
||||
ExpectedOutputLength: 5,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "object-with-arrays-of-objects",
|
||||
Path: "data[0].apps[1].name",
|
||||
Data: `{"data": [{"apps": [{"name":"app1"}, {"name":"app2"}, {"name":"app3"}]}]}`,
|
||||
ExpectedOutput: "app2",
|
||||
ExpectedOutputLength: 4,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "object-with-arrays-of-objects-with-missing-element",
|
||||
Path: "data[0].apps[1].name",
|
||||
Data: `{"data": [{"apps": []}]}`,
|
||||
ExpectedOutput: "",
|
||||
ExpectedOutputLength: 0,
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "partially-invalid-path-issue122",
|
||||
Path: "data.name.invalid",
|
||||
Data: `{"data": {"name": "john"}}`,
|
||||
ExpectedOutput: "",
|
||||
ExpectedOutputLength: 0,
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "float-as-string",
|
||||
Path: "balance",
|
||||
Data: `{"balance": "123.40000000000005"}`,
|
||||
ExpectedOutput: "123.40000000000005",
|
||||
ExpectedOutputLength: 18,
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "float-as-number",
|
||||
Path: "balance",
|
||||
Data: `{"balance": 123.40000000000005}`,
|
||||
ExpectedOutput: "123.40000000000005",
|
||||
ExpectedOutputLength: 18,
|
||||
ExpectedError: false,
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.Name, func(t *testing.T) {
|
||||
output, outputLength, err := Eval(scenario.Path, []byte(scenario.Data))
|
||||
if (err != nil) != scenario.ExpectedError {
|
||||
if scenario.ExpectedError {
|
||||
t.Errorf("Expected error, got '%v'", err)
|
||||
} else {
|
||||
t.Errorf("Expected no error, got '%v'", err)
|
||||
}
|
||||
}
|
||||
if outputLength != scenario.ExpectedOutputLength {
|
||||
t.Errorf("Expected output length to be %v, but was %v", scenario.ExpectedOutputLength, outputLength)
|
||||
}
|
||||
if output != scenario.ExpectedOutput {
|
||||
t.Errorf("Expected output to be %v, but was %v", scenario.ExpectedOutput, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user