Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONPointer ¶
type JSONPointer struct {
// contains filtered or unexported fields
}
JSONPointer represents a JavaScript Object Notation (JSON) Pointer.
func New ¶
func New(ptr string) (JSONPointer, error)
New creates a JSON Pointer based on the given string.
A JSON Pointer can be represented in a JSON string value. All instances of quotation mark '"' (%x22), reverse solidus '\' (%x5C), and control (%x00-1F) characters MUST be escaped.
func (*JSONPointer) Delete ¶
func (ptr *JSONPointer) Delete(document interface{}) (interface{}, error)
Delete removes the value corresponding with the JSON Pointer.
If the document is an array and the pointer removes an element at the root level, an error will be returned. Since the document can not be updated since the array needs te be recreated. (see examples)
Example ¶
package main
import (
"fmt"
ptr "github.com/oas3/json-pointer"
)
func main() {
doc := map[string]interface{}{
"foo": []interface{}{"bar", "baz"},
}
p, _ := ptr.New("/foo/0")
r, _ := p.Delete(doc)
fmt.Println(doc)
fmt.Println(r)
}
Output: map[foo:[baz]] bar
Example (Error) ¶
package main
import (
"fmt"
ptr "github.com/oas3/json-pointer"
)
func main() {
doc := []interface{}{"bar", "baz"}
p, _ := ptr.New("/0")
_, err := p.Delete(doc)
fmt.Println(err)
}
Output: can not delete from an array at root level
func (*JSONPointer) Get ¶
func (ptr *JSONPointer) Get(document interface{}) (interface{}, error)
Get returns the value corresponding with the JSON Pointer.
Example ¶
package main
import (
"fmt"
ptr "github.com/oas3/json-pointer"
)
func main() {
doc := map[string]interface{}{
"foo": []interface{}{"bar", "baz"},
}
var p ptr.JSONPointer
p, _ = ptr.New("")
r0, _ := p.Get(doc)
fmt.Println(r0)
p, _ = ptr.New("/foo")
r1, _ := p.Get(doc)
fmt.Println(r1)
p, _ = ptr.New("/foo/0")
r2, _ := p.Get(doc)
fmt.Println(r2)
}
Output: map[foo:[bar baz]] [bar baz] bar
func (*JSONPointer) Set ¶
func (ptr *JSONPointer) Set(value, document interface{}) (interface{}, reflect.Kind, error)
Set assigns a the given value to the JSON Pointers location.
func (*JSONPointer) String ¶
func (ptr *JSONPointer) String() string