|
1 | 1 | package jpath
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
| 6 | + "reflect" |
| 7 | + "strconv" |
5 | 8 | "strings"
|
6 | 9 | )
|
7 | 10 |
|
| 11 | +var ( |
| 12 | + ErrInvalidPath = errors.New("invalid path") |
| 13 | + ErrNotSlice = errors.New("not a slice") |
| 14 | + ErrNotMap = errors.New("not a map") |
| 15 | + ErrOutOfBounds = errors.New("index out of bounds") |
| 16 | + ErrInvalidInterface = errors.New("cannot get interface of value") |
| 17 | + ErrNil = errors.New("cannot get index or key of nil") |
| 18 | + ErrKeyType = errors.New("cannot handle this type of key") |
| 19 | +) |
| 20 | + |
8 | 21 | // StripIndices removes the characters in between brackets in a json path
|
9 | 22 | func StripIndices(path string) string {
|
10 | 23 | r := make([]byte, 0, len(path))
|
@@ -61,3 +74,98 @@ func EscapeKey(v interface{}) string {
|
61 | 74 | }
|
62 | 75 | return fmt.Sprintf("%q", s)
|
63 | 76 | }
|
| 77 | + |
| 78 | +func Split(path string) (head, tail string) { |
| 79 | + if path == "" { |
| 80 | + return "", "" |
| 81 | + } |
| 82 | + |
| 83 | + if path[0] == '[' { |
| 84 | + for i := 1; i < len(path); i++ { |
| 85 | + if path[i] == ']' { |
| 86 | + return path[0 : i+1], path[i+1 : len(path)] |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + // Skipping first character as we espect the path to start with a dot. |
| 91 | + for i := 1; i < len(path); i++ { |
| 92 | + if path[i] == '.' || path[i] == '[' { |
| 93 | + return path[0:i], path[i:len(path)] |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // tail not found, returning the path as head |
| 98 | + return path, "" |
| 99 | +} |
| 100 | + |
| 101 | +func getKey(s string, kind reflect.Kind) (reflect.Value, error) { |
| 102 | + switch kind { |
| 103 | + default: |
| 104 | + return reflect.Value{}, ErrKeyType |
| 105 | + case reflect.Int: |
| 106 | + i, err := strconv.Atoi(s) |
| 107 | + if err != nil { |
| 108 | + return reflect.Value{}, err |
| 109 | + } |
| 110 | + return reflect.ValueOf(i), nil |
| 111 | + case reflect.String: |
| 112 | + return reflect.ValueOf(s), nil |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func ExecutePath(path string, i interface{}) (interface{}, error) { |
| 117 | + // TODO(yazgazan): better errors |
| 118 | + head, tail := Split(path) |
| 119 | + if head == "" { |
| 120 | + return i, nil |
| 121 | + } |
| 122 | + |
| 123 | + v := reflect.ValueOf(i) |
| 124 | + |
| 125 | + switch head[0] { |
| 126 | + default: |
| 127 | + return nil, ErrInvalidPath |
| 128 | + case '[': |
| 129 | + if v.Kind() != reflect.Slice { |
| 130 | + return nil, ErrNotSlice |
| 131 | + } |
| 132 | + if v.IsNil() { |
| 133 | + return nil, ErrNil |
| 134 | + } |
| 135 | + if head[len(head)-1] != ']' { |
| 136 | + return nil, ErrInvalidPath |
| 137 | + } |
| 138 | + index, err := strconv.Atoi(head[1 : len(head)-1]) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + if index >= v.Len() { |
| 143 | + return nil, ErrOutOfBounds |
| 144 | + } |
| 145 | + val := v.Index(index) |
| 146 | + if !val.CanInterface() { |
| 147 | + return nil, ErrInvalidInterface |
| 148 | + } |
| 149 | + return ExecutePath(tail, val.Interface()) |
| 150 | + case '.': |
| 151 | + if v.Kind() != reflect.Map { |
| 152 | + return nil, ErrNotMap |
| 153 | + } |
| 154 | + keyStr := head[1:len(head)] |
| 155 | + if keyStr == "" { |
| 156 | + return nil, ErrInvalidPath |
| 157 | + } |
| 158 | + key, err := getKey(keyStr, v.Type().Key().Kind()) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + if v.IsNil() { |
| 163 | + return nil, ErrNil |
| 164 | + } |
| 165 | + val := v.MapIndex(key) |
| 166 | + if !val.CanInterface() { |
| 167 | + return nil, ErrInvalidInterface |
| 168 | + } |
| 169 | + return ExecutePath(tail, val.Interface()) |
| 170 | + } |
| 171 | +} |
0 commit comments