structinfo

package module
v0.0.0-...-7f8bd69 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2021 License: MIT Imports: 4 Imported by: 5

README

structinfo

Build Status

GoDoc

Tools to inspect Go structs

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultStore = NewStore()

DefaultStore is used for package scoped functions

Functions

func JSONFieldsFromStruct

func JSONFieldsFromStruct(v reflect.Value) []string

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

func StructFieldFromJSONName(v reflect.Value, name string) string

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 NewStore

func NewStore() *Store

NewStore creates a new Store object

func (*Store) Clear

func (s *Store) Clear()

func (*Store) FieldName

func (s *Store) FieldName(rv reflect.Value, name string) (string, error)

FieldName returns the name of the struct's field that matches the given JSON name

func (*Store) FieldValue

func (s *Store) FieldValue(rv reflect.Value, name string) (reflect.Value, error)

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

func (*Store) JSONFieldNames

func (s *Store) JSONFieldNames(rv reflect.Value) ([]string, error)

JSONFields returns the list of json field names associated with the value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL