Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Repository files navigation

pointerstructure GoDoc

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), 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.

A quick code example is shown below:

complex:=map[string]interface{}{
"alice": 42,
"bob": []interface{}{
map[string]interface{}{
"name": "Bob",
},
},
}
value, err:=pointerstructure.Get(complex, "/bob/0/name")
iferr!=nil {
panic(err)
}
fmt.Printf("%s", value)
// Output:// Bob

Continuing the example above, you can also set values:

value, err=pointerstructure.Set(complex, "/bob/0/name", "Alice")
iferr!=nil {
panic(err)
}
value, err=pointerstructure.Get(complex, "/bob/0/name")
iferr!=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:

input:=struct {
Valuesmap[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")
iferr!=nil {
panic(err)
}
fmt.Printf("%s", value)
// Output:// Bob

About

Go library for addressing and reading/writing a specific value within any Go structure using a string syntax.

Resources

Stars

214 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages