https://github.com/mitchellh/pointerstructure
Go library for addressing and reading/writing a specific value within any Go structure using a string syntax.
https://github.com/mitchellh/pointerstructure
Last synced: 9 months ago
JSON representation
Go library for addressing and reading/writing a specific value within any Go structure using a string syntax.
- Host: GitHub
- URL: https://github.com/mitchellh/pointerstructure
- Owner: mitchellh
- License: mit
- Archived: true
- Created: 2017-02-04T14:23:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-09-06T00:53:42.000Z (almost 3 years ago)
- Last Synced: 2025-10-10T17:56:53.647Z (9 months ago)
- Language: Go
- Homepage: https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc
- Size: 49.8 KB
- Stars: 215
- Watchers: 4
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pointerstructure [](https://godoc.org/github.com/mitchellh/pointerstructure)
pointerstructure is a Go library for identifying a specific value within
any Go structure using a string syntax.
pointerstructure is based on
[JSON Pointer (RFC 6901)](https://tools.ietf.org/html/rfc6901), but
reimplemented for Go.
The goal of pointerstructure is to provide a single, well-known format
for addressing a specific value. This can be useful for user provided
input on structures, diffs of structures, etc.
## Features
* Get the value for an address
* Set the value for an address within an existing structure
* Delete the value at an address
* Sorting a list of addresses
## Installation
Standard `go get`:
```
$ go get github.com/mitchellh/pointerstructure
```
## Usage & Example
For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/pointerstructure).
A quick code example is shown below:
```go
complex := map[string]interface{}{
"alice": 42,
"bob": []interface{}{
map[string]interface{}{
"name": "Bob",
},
},
}
value, err := pointerstructure.Get(complex, "/bob/0/name")
if err != nil {
panic(err)
}
fmt.Printf("%s", value)
// Output:
// Bob
```
Continuing the example above, you can also set values:
```go
value, err = pointerstructure.Set(complex, "/bob/0/name", "Alice")
if err != nil {
panic(err)
}
value, err = pointerstructure.Get(complex, "/bob/0/name")
if err != nil {
panic(err)
}
fmt.Printf("%s", value)
// Output:
// Alice
```
The library also supports `Get` operations on structs including using the `pointer`
struct tag to override struct field names:
```go
input := struct {
Values map[string]interface{} `pointer:"embedded"`
}{
Values: map[string]interface{}{
"alice": 42,
"bob": []interface{}{
map[string]interface{}{
"name": "Bob",
},
},
},
}
value, err := Get(input, "/embedded/bob/0/name")
if err != nil {
panic(err)
}
fmt.Printf("%s", value)
// Output:
// Bob
```