https://github.com/0xch4z/selectr
Select values from objects/arrays with key-path notation.
https://github.com/0xch4z/selectr
go golang interpreter parser
Last synced: about 1 year ago
JSON representation
Select values from objects/arrays with key-path notation.
- Host: GitHub
- URL: https://github.com/0xch4z/selectr
- Owner: 0xch4z
- License: mit
- Created: 2020-06-04T21:47:18.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-01T21:35:10.000Z (over 3 years ago)
- Last Synced: 2025-03-26T13:50:37.341Z (about 1 year ago)
- Topics: go, golang, interpreter, parser
- Language: Go
- Homepage:
- Size: 55.7 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# selectr [![GoDoc][godoc-badge]][godoc] ![test][test-badge]
> Select values from objects/arrays with key-path notation.
## Key-path notation
Key-path notation is a simple format for describing how to traverse data structures. It's very similar to C languages, but with limitations. See the [reference document](docs/key-path-notation.md) for more information.
## Usage
```go
m := map[string]interface{}{
"foo": map[string]interface{}{
"bar": []interface{}{
1,
2,
3,
},
},
}
sel, _ := selectr.Parse(".foo")
sel.Resolve(m) // => map[string]interface{}{"bar": []interface{}{1, 2, 3}}
sel, _ = selectr.Parse(".foo.bar")
sel.Resolve(m) // => []interface{}{1, 2, 3}
sel, _ = selectr.Parse(".foo.bar[1]")
sel.Resolve(m) // => 2
```
## Use cases
- Referencing a dynamic value in a JSON/YAML file:
Consider you maintain a program that allows users to reference arbitrary values, and one strategy of resolving a value is referencing a symbol in a JSON file.
```json
# example.json
{
"accounts": [{
"id": 123,
"name": "main"
}]
}
```
The end-user references `example.json` with the selectr `.accounts[0].name`.
```go
jsonBytes, _ := os.Open(fileName)
var m map[string]interface{}
json.Unmarshal(jsonBytes, &m)
sel, _ := selectr.Parse(selectrString)
val := sel.Resolve(m)
```
The `val` is resolved to `"main"`.
- Import dynamic values from dynamic data files.
[test-badge]: https://github.com/0xch4z/selectr/workflows/test/badge.svg
[godoc-badge]: https://godoc.org/github.com/0xch4z/selectr?status.svg
[godoc]: https://godoc.org/github.com/0xch4z/selectr