Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultStore = NewStore()
DefaultStore is used for package scoped functions
Functions ¶
func JSONFieldsFromStruct ¶
JSONFieldsFromStruct returns the names of JSON fields associated with the given struct. Returns nil if v is not a struct
(This method should probably be considered deprecated)
func StructFieldFromJSONName ¶
StructFieldFromJSONName returns the struct field name on the given struct value. Empty value means the field is either not public, or does not exist.
This can be used to map JSON field names to actual struct fields.
(This method should probably be considered deprecated)
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store holds cached information about struct data
func (*Store) FieldName ¶
FieldName returns the name of the struct's field that matches the given JSON name
func (*Store) FieldValue ¶
FieldValue returns the reflect.Value corresponding to the given JSON name from the given object. Note that this differs from querying the reflect.Value.FieldByName, because we're matching against the *JSON* name, not necessarily the Go struct field name
Example ¶
package main
import (
"fmt"
"log"
"reflect"
"github.com/lestrrat-go/structinfo"
)
func main() {
var x struct {
Name string `json:"name"`
}
nameVal, err := structinfo.DefaultStore.FieldValue(reflect.ValueOf(&x), "name")
if err != nil {
log.Fatal(err)
}
nameVal.SetString("foo")
fmt.Println(x.Name)
}
Output: foo