Documentation ¶
Overview ¶
Package refl provides reflection helpers.
Index ¶
- Constants
- func As(v interface{}, target interface{}) bool
- func DeepIndirect(t reflect.Type) reflect.Type
- func FindEmbeddedSliceOrMap(i interface{}) reflect.Type
- func FindTaggedName(structPtr, fieldPtr interface{}, tagName string) (string, error)
- func HasTaggedFields(i interface{}, tagName string) bool
- func IsSliceOrMap(i interface{}) bool
- func IsStruct(i interface{}) bool
- func IsZero(v reflect.Value) bool
- func JoinErrors(errs ...error) error
- func PopulateFieldsFromTags(structPtr interface{}, fieldTag reflect.StructTag) error
- func ReadBoolPtrTag(tag reflect.StructTag, name string, holder **bool) error
- func ReadBoolTag(tag reflect.StructTag, name string, holder *bool) error
- func ReadFloatPtrTag(tag reflect.StructTag, name string, holder **float64) error
- func ReadFloatTag(tag reflect.StructTag, name string, holder *float64) error
- func ReadIntPtrTag(tag reflect.StructTag, name string, holder **int64) error
- func ReadIntTag(tag reflect.StructTag, name string, holder *int64) error
- func ReadStringPtrTag(tag reflect.StructTag, name string, holder **string)
- func ReadStringTag(tag reflect.StructTag, name string, holder *string)
- func Tagged(structPtr, fieldPtr interface{}, tagName string) string
- func WalkTaggedFields(v reflect.Value, f WalkTaggedFieldFn, tagName string)
- type SentinelError
- type TypeString
- type WalkTaggedFieldFn
Examples ¶
Constants ¶
const ( ErrNeedPointer = SentinelError("could not find field value in struct") ErrMissingFieldValue = SentinelError("can not take address of structure, please pass a pointer") ErrMissingStructOrField = SentinelError("structPtr and fieldPtr are required") )
Sentinel errors.
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v0.1.4
func As(v interface{}, target interface{}) bool
As unwraps interface value to find value assignable to target.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/swaggest/refl" ) func main() { var ( v interface{} vv json.Marshaler ) vv = json.RawMessage(`{"abc":123}`) v = vv target := new(json.RawMessage) fmt.Println(refl.As(v, target), string(*target)) }
Output: true {"abc":123}
func DeepIndirect ¶
DeepIndirect returns first encountered non-pointer type.
func FindEmbeddedSliceOrMap ¶
FindEmbeddedSliceOrMap checks if variable has a slice/array/map or a pointer to it embedded.
func FindTaggedName ¶
FindTaggedName returns tagged name of an entity field.
Entity field is defined by pointer to owner structure and pointer to field in that structure.
entity := MyEntity{} name, found := sm.FindTaggedName(&entity, &entity.UpdatedAt, "db")
func HasTaggedFields ¶
HasTaggedFields checks if the structure has fields with tag name.
func IsSliceOrMap ¶
func IsSliceOrMap(i interface{}) bool
IsSliceOrMap checks if variable is a slice/array/map or a pointer to it.
func IsStruct ¶
func IsStruct(i interface{}) bool
IsStruct checks if variable is a struct or a pointer to a struct.
func IsZero ¶
IsZero reports whether v is the zero value for its type. It panics if the argument is invalid.
Adapted from go1.13 reflect.IsZero.
func PopulateFieldsFromTags ¶
PopulateFieldsFromTags extracts values from field tag and puts them in according property of structPtr.
Example ¶
package main import ( "encoding/json" "fmt" "log" "reflect" "github.com/swaggest/refl" ) func main() { type schema struct { Title string Desc *string Min *float64 Max float64 Limit int64 Offset *int64 Deprecated bool Required *bool } type value struct { Property string `title:"Value" desc:"..." min:"-1.23" max:"10.1" limit:"5" offset:"2" deprecated:"true" required:"f"` } s := schema{} tag := reflect.TypeOf(value{}).Field(0).Tag err := refl.PopulateFieldsFromTags(&s, tag) if err != nil { log.Fatal(err) } j, err := json.Marshal(s) if err != nil { log.Fatal(err) } fmt.Println(string(j)) }
Output: {"Title":"Value","Desc":"...","Min":-1.23,"Max":10.1,"Limit":5,"Offset":2,"Deprecated":true,"Required":false}
func ReadBoolPtrTag ¶
ReadBoolPtrTag reads bool value from field tag into a pointer.
func ReadBoolTag ¶
ReadBoolTag reads bool value from field tag into a value.
func ReadFloatPtrTag ¶
ReadFloatPtrTag reads float64 value from field tag into a pointer.
func ReadFloatTag ¶
ReadFloatTag reads float64 value from field tag into a value.
func ReadIntPtrTag ¶
ReadIntPtrTag reads int64 value from field tag into a pointer.
func ReadIntTag ¶
ReadIntTag reads int64 value from field tag into a value.
func ReadStringPtrTag ¶
ReadStringPtrTag reads string value from field tag into a pointer.
func ReadStringTag ¶
ReadStringTag reads string value from field tag into a value.
func WalkTaggedFields ¶
func WalkTaggedFields(v reflect.Value, f WalkTaggedFieldFn, tagName string)
WalkTaggedFields iterates top level fields of structure including anonymous embedded fields.
Types ¶
type SentinelError ¶ added in v0.1.4
type SentinelError string
SentinelError is a constant error.
func (SentinelError) Error ¶ added in v0.1.4
func (se SentinelError) Error() string
Error implements error.
type TypeString ¶
type TypeString string
TypeString is a type name with import path.
func GoType ¶
func GoType(t reflect.Type) TypeString
GoType returns string representation of type name including import path.
Example ¶
package main import ( "fmt" "reflect" "github.com/swaggest/refl" fancypath "github.com/swaggest/refl/internal/Fancy-Path" "github.com/swaggest/refl/internal/sample" ) func main() { fmt.Println(refl.GoType(reflect.TypeOf(new(fancypath.Sample)))) fmt.Println(refl.GoType(reflect.TypeOf(new(sample.TestSampleStruct)))) }
Output: *github.com/swaggest/refl/internal/Fancy-Path::fancypath.Sample *github.com/swaggest/refl/internal/sample.TestSampleStruct
type WalkTaggedFieldFn ¶
type WalkTaggedFieldFn func(v reflect.Value, sf reflect.StructField, tag string)
WalkTaggedFieldFn defines callback.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
Fancy-Path
Package fancypath contains test data.
|
Package fancypath contains test data. |
sample
Package sample contains test data.
|
Package sample contains test data. |