Documentation ¶
Overview ¶
Package kit provides various reflect type functions for GoKi system, including:
* kit.TypeRegistry (types.go) for associating string names with reflect.Type values, to allow dynamic marshaling of structs, and also bidirectional string conversion of const int iota (enum) types. It is used by the GoKi ki system, hence the kit (ki types) name.
To register a new type, add:
var KiT_TypeName = kit.Types.AddType(&TypeName{}, [props|nil])
where the props is a map[string]interface{} of optional properties that can be associated with the type -- this is used in the GoGi graphical interface system for example to color objects of different types using the background-color property. KiT_TypeName variable can be conveniently used wherever a reflect.Type of that type is needed.
* kit.EnumRegistry (enums.go) that registers constant int iota (aka enum) types, and provides general conversion utilities to / from string, int64, general properties associated with enum types, and deals with bit flags
* kit.Type (type.go) struct provides JSON and XML Marshal / Unmarshal functions for saving / loading reflect.Type using registrered type names.
* convert.go: robust interface{}-based type conversion routines that are useful in more lax user-interface contexts where "common sense" conversions between strings, numbers etc are useful
* embeds.go: various functions for managing embedded struct types, e.g., determining if a given type embeds another type (directly or indirectly), and iterating over fields to flatten the otherwise nested nature of the field encoding in embedded types.
Index ¶
- Variables
- func BitFlagsToString(bflg int64, en interface{}) string
- func CloneToType(typ reflect.Type, val interface{}) reflect.Value
- func EmbeddedStruct(stru interface{}, embed reflect.Type) interface{}
- func EmbeddedTypeImplements(typ, iface reflect.Type) bool
- func EnumIfaceFromInt64(ival int64, typ reflect.Type) interface{}
- func EnumIfaceFromString(str string, typ reflect.Type) interface{}
- func EnumInt64ToString(ival int64, typ reflect.Type) string
- func EnumMarshalJSON(eval interface{}) ([]byte, error)
- func EnumToInt64(eval interface{}) int64
- func EnumToString(eval interface{}) string
- func EnumUnmarshalJSON(eval interface{}, b []byte) error
- func FlatFieldByName(typ reflect.Type, nm string) (reflect.StructField, bool)
- func FlatFieldInterfaceByName(stru interface{}, nm string) interface{}
- func FlatFieldInterfaces(stru interface{}) []interface{}
- func FlatFieldTag(typ reflect.Type, nm, tag string) string
- func FlatFieldVals(stru interface{}) []reflect.Value
- func FlatFieldValueByName(stru interface{}, nm string) reflect.Value
- func FlatFields(typ reflect.Type) []reflect.StructField
- func FlatFieldsTypeFun(typ reflect.Type, fun func(typ reflect.Type, field reflect.StructField) bool) bool
- func FlatFieldsValueFun(stru interface{}, ...) bool
- func FullTypeName(typ reflect.Type) string
- func IfaceIsNil(it interface{}) bool
- func KindIsBasic(vk reflect.Kind) bool
- func MakeMap(typ reflect.Type) reflect.Value
- func MakeOfType(typ reflect.Type) reflect.Value
- func MakePtrValue(v reflect.Value) reflect.Value
- func MakeSlice(typ reflect.Type, len, cap int) reflect.Value
- func Max32(a, b float32) float32
- func MaxInt(a, b int) int
- func Min32(a, b float32) float32
- func MinInt(a, b int) int
- func MinPos(a, b float64) float64
- func MinPos32(a, b float32) float32
- func NonPtrInterface(el interface{}) interface{}
- func NonPtrType(typ reflect.Type) reflect.Type
- func NonPtrValue(v reflect.Value) reflect.Value
- func OnePtrInterface(el interface{}) interface{}
- func OnePtrType(typ reflect.Type) reflect.Type
- func OnePtrValue(v reflect.Value) reflect.Value
- func PtrInterface(el interface{}) interface{}
- func PtrType(typ reflect.Type) reflect.Type
- func PtrValue(v reflect.Value) reflect.Value
- func Sel(a ...interface{}) []interface{}
- func SetEnumFromInt64(eval interface{}, ival int64, typ reflect.Type) error
- func SetEnumFromString(eptr interface{}, str string) error
- func SetEnumValueFromInt64(eval reflect.Value, ival int64) error
- func SetEnumValueFromString(eval reflect.Value, str string) error
- func SetRobust(to, from interface{}) bool
- func String(k Type) string
- func ToBool(it interface{}) (bool, bool)
- func ToFloat(it interface{}) (float64, bool)
- func ToFloat32(it interface{}) (float32, bool)
- func ToInt(it interface{}) (int64, bool)
- func ToString(it interface{}) string
- func TypeEmbeds(typ, embed reflect.Type) bool
- func UnhideIfaceValue(v reflect.Value) reflect.Value
- func ValueIsZero(v reflect.Value) bool
- type EnumRegistry
- func (tr *EnumRegistry) AddEnum(en interface{}, bitFlag bool, props map[string]interface{}) reflect.Type
- func (tr *EnumRegistry) AddEnumAltLower(en interface{}, bitFlag bool, props map[string]interface{}, prefix string) reflect.Type
- func (tr *EnumRegistry) AllTagged(key string) []reflect.Type
- func (tr *EnumRegistry) AltStrings(enumName string) map[int64]string
- func (tr *EnumRegistry) Enum(name string) reflect.Type
- func (tr *EnumRegistry) EnumInt64ToAltString(ival int64, typnm string) string
- func (tr *EnumRegistry) EnumInt64ToString(ival int64, typ reflect.Type) string
- func (tr *EnumRegistry) EnumToAltString(eval interface{}) string
- func (tr *EnumRegistry) EnumToString(eval interface{}) string
- func (tr *EnumRegistry) IsBitFlag(typ reflect.Type) bool
- func (tr *EnumRegistry) NVals(eval interface{}) int64
- func (tr *EnumRegistry) Prop(enumName, propKey string) interface{}
- func (tr *EnumRegistry) Properties(enumName string) map[string]interface{}
- func (tr *EnumRegistry) SetEnumFromAltString(eptr interface{}, str string) error
- func (tr *EnumRegistry) SetEnumFromString(eptr interface{}, str string) error
- func (tr *EnumRegistry) SetEnumFromStringAltFirst(eptr interface{}, str string) error
- func (tr *EnumRegistry) SetEnumValueFromAltString(eval reflect.Value, str string) error
- func (tr *EnumRegistry) SetEnumValueFromInt64(eval reflect.Value, ival int64) error
- func (tr *EnumRegistry) SetEnumValueFromString(eval reflect.Value, str string) error
- func (tr *EnumRegistry) SetEnumValueFromStringAltFirst(eval reflect.Value, str string) error
- func (tr *EnumRegistry) TypeRegistered(typ reflect.Type) bool
- func (tr *EnumRegistry) TypeValues(et reflect.Type, alt bool) []EnumValue
- func (tr *EnumRegistry) Values(enumName string, alt bool) []EnumValue
- type EnumValue
- type TestFlags
- type Type
- type TypeAndName
- type TypeAndNameList
- type TypeRegistry
- func (tr *TypeRegistry) AddType(obj interface{}, props map[string]interface{}) reflect.Type
- func (tr *TypeRegistry) AllEmbedsOf(embed reflect.Type, inclusive, includeBases bool) []reflect.Type
- func (tr *TypeRegistry) AllImplementersOf(iface reflect.Type, includeBases bool) []reflect.Type
- func (tr *TypeRegistry) AllTagged(key string) []reflect.Type
- func (tr *TypeRegistry) Init()
- func (tr *TypeRegistry) Inst(typ reflect.Type) interface{}
- func (tr *TypeRegistry) InstByName(typeName string) interface{}
- func (tr *TypeRegistry) Prop(typ reflect.Type, propKey string) interface{}
- func (tr *TypeRegistry) PropByName(typeName, propKey string) interface{}
- func (tr *TypeRegistry) Properties(typ reflect.Type, makeNew bool) map[string]interface{}
- func (tr *TypeRegistry) PropsByName(typeName string, makeNew bool) map[string]interface{}
- func (tr *TypeRegistry) Type(typeName string) reflect.Type
Constants ¶
This section is empty.
Variables ¶
var KiT_TestFlags = Enums.AddEnumAltLower(TestFlagsN, false, nil, "Test")
var ShortTypeNames = true
if ShortTypeNames is true, we just use the standard "base".TypeName instead of the full path that PgkPath returns -- this should work unless there are conflicts but the savings in JSON files etc is probably worth it..
Functions ¶
func BitFlagsToString ¶
BitFlagsToString converts an int64 of bit flags into a string representation of the bits that are set -- en is number of defined bits, and also provides the type name for looking up strings
func CloneToType ¶
CloneToType creates a new object of given type, and uses SetRobust to copy an existing value (of perhaps another type) into it -- only expected to work for basic types
func EmbeddedStruct ¶
EmbeddedStruct returns the embedded struct of given type within given struct
func EmbeddedTypeImplements ¶
EmbeddedTypeImplements checks if given type implements given interface, or it embeds a type that does so -- must pass a type constructed like this: reflect.TypeOf((*gi.Node2D)(nil)).Elem()
func EnumIfaceFromInt64 ¶
EnumIfaceFromInt64 returns an interface{} value which is an enum value of given type (not a pointer to it), set to given integer value
func EnumIfaceFromString ¶
EnumIfaceFromString returns an interface{} value which is an enum value of given type (not a pointer to it), set to given string value
func EnumInt64ToString ¶
EnumInt64ToString first converts an int64 to enum of given type, and then converts that to a string value
func EnumMarshalJSON ¶
func EnumToInt64 ¶
func EnumToInt64(eval interface{}) int64
EnumToInt64 converts an enum into an int64 using reflect -- just use int64(eval) when you have the enum in hand -- this is when you just have a generic item
func EnumToString ¶
func EnumToString(eval interface{}) string
EnumToString converts an enum value to its corresponding string value -- you could just call fmt.Sprintf("%v") too but this is slightly faster, and it also works for bitflags which regular stringer does not
func EnumUnmarshalJSON ¶
func FlatFieldByName ¶
FlatFieldByName returns field in type or embedded structs within type, by name -- native function already does flat version, so this is just for reference and consistency
func FlatFieldInterfaceByName ¶
func FlatFieldInterfaceByName(stru interface{}, nm string) interface{}
FlatFieldInterfaceByName finds field in object and embedded objects, by name, returning interface{} to pointer of field, or nil if not found
func FlatFieldInterfaces ¶
func FlatFieldInterfaces(stru interface{}) []interface{}
FlatFieldInterfaces returns a slice list of all the field interface{} values *as pointers to the field value* (i.e., calling Addr() on the Field Value) for fields of given struct (must pass a pointer to the struct) and any of its embedded structs -- returns nil on error (logged)
func FlatFieldTag ¶
FlatFieldTag returns given tag value in field in type or embedded structs within type, by name -- empty string if not set or field not found
func FlatFieldVals ¶
FlatFieldsVals returns a slice list of all the field reflect.Value's for fields of given struct (must pass a pointer to the struct) and any of its embedded structs -- returns nil on error (logged)
func FlatFieldValueByName ¶
FlatFieldValueByName finds field in object and embedded objects, by name, returning reflect.Value of field -- native version of Value function already does flat find, so this just provides a convenient wrapper
func FlatFields ¶
func FlatFields(typ reflect.Type) []reflect.StructField
FlatFields returns a slice list of all the StructField type information for fields of given type and any embedded types -- returns nil on error (logged)
func FlatFieldsTypeFun ¶
func FlatFieldsTypeFun(typ reflect.Type, fun func(typ reflect.Type, field reflect.StructField) bool) bool
FlatFieldsTypeFun calls a function on all the the primary fields of a given struct type, including those on anonymous embedded structs that this struct has, passing the current (embedded) type and StructField -- effectively flattens the reflect field list -- if fun returns false then iteration stops -- overall rval is false if iteration was stopped or there was an error (logged), true otherwise
func FlatFieldsValueFun ¶
func FlatFieldsValueFun(stru interface{}, fun func(stru interface{}, typ reflect.Type, field reflect.StructField, fieldVal reflect.Value) bool) bool
FlatFieldsValueFun calls a function on all the the primary fields of a given struct value (must pass a pointer to the struct) including those on anonymous embedded structs that this struct has, passing the current (embedded) type and StructField -- effectively flattens the reflect field list
func FullTypeName ¶
FullTypeName returns the full package-qualified type name -- this is what is used for encoding type names in the registry
func IfaceIsNil ¶
func IfaceIsNil(it interface{}) bool
IfaceIsNil checks if an interface value is nil -- the interface itself could be nil, or the value pointed to by the interface could be nil -- this checks both, safely
func KindIsBasic ¶
KindIsBasic returns true if the reflect.Kind is a basic type such as Int, Float, etc
func MakeMap ¶
MakeMap makes a map that is actually addressable, getting around the hidden interface{} that reflect.MakeMap makes, by calling UnhideIfaceValue (from ptrs.go)
func MakeOfType ¶
MakeOfType creates a new object of given type with appropriate magic foo to make it usable
func MakePtrValue ¶
MakePtrValue makes a new pointer to the given value, adding an extra level of indirection, and then removing that indirection, resulting in something that is now addressable / assignable -- this is necessary for enums..
func MakeSlice ¶
MakeSlice makes a map that is actually addressable, getting around the hidden interface{} that reflect.MakeSlice makes, by calling UnhideIfaceValue (from ptrs.go)
func NonPtrInterface ¶
func NonPtrInterface(el interface{}) interface{}
NonPtrInterface returns the non-pointer value of an interface
func NonPtrType ¶
NonPtrType returns the non-pointer underlying type
func NonPtrValue ¶
NonPtrValue returns the non-pointer underlying value
func OnePtrInterface ¶
func OnePtrInterface(el interface{}) interface{}
OnePtrInterface returns the pointer value of an interface, if it is possible to get one through Addr()
func OnePtrType ¶
OnePtrType returns a type that is exactly one pointer away from a non-pointer type
func OnePtrValue ¶
OnePtrValue returns a value that is exactly one pointer away from a non-pointer type
func PtrInterface ¶
func PtrInterface(el interface{}) interface{}
PtrInterface returns the pointer value of an interface, if it is possible to get one through Addr()
func PtrValue ¶
PtrValue returns the pointer version (Addr()) of the underlying value if the value is not already a Ptr
func Sel ¶
func Sel(a ...interface{}) []interface{}
Sel implements the "mute" function from here http://blog.vladimirvivien.com/2014/03/hacking-go-filter-values-from-multi.html provides a way to select a particular return value in a single expression, without having a separate assignment in between -- I just call it "Sel" as I'm unlikely to remember how to type a mu
func SetEnumFromInt64 ¶
SetEnumFromInt64 sets enum value from int64 value -- must pass a pointer to the enum and also needs raw type of the enum as well -- can't get it from the interface{} reliably
func SetEnumFromString ¶
SetEnumFromString sets enum value from string -- must pass a *pointer* to the enum item. IMPORTANT: requires the modified stringer go generate utility that generates a StringToTypeName method
func SetEnumValueFromInt64 ¶
SetEnumValueFromInt64 sets enum value from int64 value, using a reflect.Value representation of the enum
func SetEnumValueFromString ¶
SetEnumValueFromString sets enum value from string using reflect.Value IMPORTANT: requires the modified stringer go generate utility that generates a StringToTypeName method
func SetRobust ¶
func SetRobust(to, from interface{}) bool
SetRobust robustly sets the to value from the from value -- to must be a pointer-to -- only for basic field values -- use copier package for more complex cases
func ToString ¶
func ToString(it interface{}) string
ToString robustly converts anything to a String -- because Stringer is so ubiquitous, and we fall back to fmt.Sprintf(%v) in worst case, this should definitely work in all cases, so there is no bool return value
func TypeEmbeds ¶
TypeEmbeds checks if given type embeds another type, at any level of recursive embedding (including being the type itself)
func UnhideIfaceValue ¶
UnhideIfaceValue returns a reflect.Value for any of the Make* functions that is actually assignable -- even though these functions return a pointer to the new object, it is somehow hidden behind an interface{} and this magic foo, posted by someone somewhere that I cannot now find again, un-hides it..
func ValueIsZero ¶
ValueIsZero returns true if the reflect.Value is Zero or nil or invalid or otherwise doesn't have a useful value -- from https://github.com/golang/go/issues/7501
Types ¶
type EnumRegistry ¶
type EnumRegistry struct { Enums map[string]reflect.Type // Props contains properties that can be associated with each enum type -- e.g., "BitFlag": true -- "AltStrings" : map[int64]string, or other custom settings Props map[string]map[string]interface{} // Vals contains cached EnumValue representations of the enum values -- used by EnumValues method Vals map[string][]EnumValue }
EnumRegistry is a map from an enum-style const int type name to a corresponding reflect.Type and conversion methods generated by (modified) stringer that convert to / from strings -- need to explicitly register each new type by calling AddEnum in the process of creating a new global variable, as in:
var KiT_MyEnum = kit.Enums.AddEnum(MyEnumN, bitFlag true/false, TypeNameProps (or nil))
where MyEnum is the name of the type, MyEnumN is the enum value representing the number of defined enums (always good practice to define this value, for ease of extension by others), and TypeNameProps is nil or a map[string]interface{} of properties, OR:
var KiT_MyEnum = kit.Enums.AddEnumAltLower(MyEnumN, bitFlag true/false, TypeNameProps, "Prefix")
which automatically registers alternative names as lower-case versions of const names with given prefix removed -- often what is used in e.g., json or xml kinds of formats
special properties:
* "N": max value of enum defined -- number of enum entries (assuming ordinal, which is all that is currently supported here)
* "BitFlag": true -- each value represents a bit in a set of bit flags, so the string rep of a value contains an or-list of names for each bit set, separated by "|". Use the bitflag package to set and clear bits while keeping the definition of the flags as a standard ordinal integer value -- much more flexible than pre-compiling the bitmasks. Usually should be an int64 type.
* "AltStrings": map[int64]string -- provides an alternative string mapping for the enum values
Also recommend defining JSON I/O functions for each registered enum -- much safer to save enums as strings than using their raw numerical values, which can change over time:
func (ev TestFlags) MarshalJSON() ([]byte, error) { return kit.EnumMarshalJSON(ev) } func (ev *TestFlags) UnmarshalJSON() ([]byte, error) { return kit.EnumUnmarshalJSON(ev) }
var Enums EnumRegistry
Enums is master registry of enum types -- can also create your own package-specific ones
func (*EnumRegistry) AddEnum ¶
func (tr *EnumRegistry) AddEnum(en interface{}, bitFlag bool, props map[string]interface{}) reflect.Type
AddEnum adds a given type to the registry -- requires the N value to set N from and grab type info from -- if bitFlag then sets BitFlag property, and each value represents a bit in a set of bit flags, so the string rep of a value contains an or-list of names for each bit set, separated by | -- can also add additional properties -- they are copied so can be re-used across enums
func (*EnumRegistry) AddEnumAltLower ¶
func (tr *EnumRegistry) AddEnumAltLower(en interface{}, bitFlag bool, props map[string]interface{}, prefix string) reflect.Type
AddEnumAltLower adds a given type to the registry -- requires the N value to set N from and grab type info from -- automatically initializes AltStrings alternative string map based on the name with given prefix removed (e.g., a type name-based prefix) and lower-cased -- also requires the number of enums -- assumes starts at 0
func (*EnumRegistry) AllTagged ¶
func (tr *EnumRegistry) AllTagged(key string) []reflect.Type
AllTagged returns a list of all registered enum types that include a given property key value -- does not check for the value of that value -- just its existence
func (*EnumRegistry) AltStrings ¶
func (tr *EnumRegistry) AltStrings(enumName string) map[int64]string
AltStrings returns optional alternative string map for enums -- e.g., lower-case, without prefixes etc -- can put multiple such alt strings in the one string with your own separator, in a predefined order, if necessary, and just call strings.Split on those and get the one you want -- nil if not set
func (*EnumRegistry) Enum ¶
func (tr *EnumRegistry) Enum(name string) reflect.Type
Enum finds an enum type based on its type name -- returns nil if not found
func (*EnumRegistry) EnumInt64ToAltString ¶
func (tr *EnumRegistry) EnumInt64ToAltString(ival int64, typnm string) string
EnumInt64ToAltString converts an int64 value to the enum of given type, and then into corresponding alternative string value
func (*EnumRegistry) EnumInt64ToString ¶
func (tr *EnumRegistry) EnumInt64ToString(ival int64, typ reflect.Type) string
EnumInt64ToAltString converts an int64 value to the enum of given type, and then into corresponding string value
func (*EnumRegistry) EnumToAltString ¶
func (tr *EnumRegistry) EnumToAltString(eval interface{}) string
EnumToAltString converts an enum value to its corresponding alternative string value
func (*EnumRegistry) EnumToString ¶
func (tr *EnumRegistry) EnumToString(eval interface{}) string
EnumToString converts an enum value to its corresponding string value -- you could just call fmt.Sprintf("%v") too but this is slightly faster
func (*EnumRegistry) IsBitFlag ¶
func (tr *EnumRegistry) IsBitFlag(typ reflect.Type) bool
IsBitFlag checks if this enum is for bit flags instead of mutually-exclusive int values -- checks BitFlag property -- if true string rep of a value contains an or-list of names for each bit set, separated by |
func (*EnumRegistry) NVals ¶
func (tr *EnumRegistry) NVals(eval interface{}) int64
NVals returns the number of defined enum values
func (*EnumRegistry) Prop ¶
func (tr *EnumRegistry) Prop(enumName, propKey string) interface{}
Prop safely finds an enum type property from enum type name and property key -- nil if not found
func (*EnumRegistry) Properties ¶
func (tr *EnumRegistry) Properties(enumName string) map[string]interface{}
Props returns properties for this type -- makes props map if not already made
func (*EnumRegistry) SetEnumFromAltString ¶
func (tr *EnumRegistry) SetEnumFromAltString(eptr interface{}, str string) error
SetEnumFromAltString sets from alternative string list using an interface{} to the enum -- must pass a *pointer* to the enum item.
func (*EnumRegistry) SetEnumFromString ¶
func (tr *EnumRegistry) SetEnumFromString(eptr interface{}, str string) error
SetEnumFromString sets enum value from string -- must pass a *pointer* to the enum item. IMPORTANT: requires the modified stringer go generate utility that generates a StringToTypeName method
func (*EnumRegistry) SetEnumFromStringAltFirst ¶
func (tr *EnumRegistry) SetEnumFromStringAltFirst(eptr interface{}, str string) error
SetEnumFromStringAltFirst first attempts to set an enum from an alternative string, and if that fails, then it tries to set from the regular string representation func (tr *EnumRegistry)
func (*EnumRegistry) SetEnumValueFromAltString ¶
func (tr *EnumRegistry) SetEnumValueFromAltString(eval reflect.Value, str string) error
SetEnumValueFromAltString sets value from alternative string using a reflect.Value -- must pass a *pointer* value to the enum item.
func (*EnumRegistry) SetEnumValueFromInt64 ¶
func (tr *EnumRegistry) SetEnumValueFromInt64(eval reflect.Value, ival int64) error
SetEnumValueFromInt64 sets the enum value using reflect.Value representation from a generic int64 value
func (*EnumRegistry) SetEnumValueFromString ¶
func (tr *EnumRegistry) SetEnumValueFromString(eval reflect.Value, str string) error
SetEnumValueFromString sets enum value from string, into a reflect.Value IMPORTANT: requires the modified stringer go generate utility that generates a StringToTypeName method
func (*EnumRegistry) SetEnumValueFromStringAltFirst ¶
func (tr *EnumRegistry) SetEnumValueFromStringAltFirst(eval reflect.Value, str string) error
SetEnumValueFromStringAltFirst first attempts to set an enum from an alternative string, and if that fails, then it tries to set from the regular string representation func (tr *EnumRegistry)
func (*EnumRegistry) TypeRegistered ¶
func (tr *EnumRegistry) TypeRegistered(typ reflect.Type) bool
TypeRegistered returns true if the given type is registered as an enum type
func (*EnumRegistry) TypeValues ¶
func (tr *EnumRegistry) TypeValues(et reflect.Type, alt bool) []EnumValue
TypeValues returns an EnumValue slice for all the values of an enum type -- if alt is true and alt names exist, then those are used
type EnumValue ¶
type EnumValue struct { Name string `desc:"name for this value"` Value int64 `desc:"integer value"` Type reflect.Type `desc:"the enum type that this value belongs to"` }
EnumValue represents enum values, in common int64 terms, e.g., for GUI
type TestFlags ¶
type TestFlags int32
testing
func (*TestFlags) FromString ¶
func (TestFlags) MarshalJSON ¶
func (*TestFlags) UnmarshalJSON ¶
type Type ¶
Type provides JSON, XML marshal / unmarshal with encoding of underlying type name using kit.Types type name registry
func (Type) FullTypeName ¶
the full name of the type: package name + "." + type name
func (Type) MarshalJSON ¶
MarshalJSON saves only the type name
func (Type) MarshalXML ¶
MarshalXML saves only the type name
func (*Type) UnmarshalJSON ¶
UnmarshalJSON loads the type name and looks it up in the Types registry of type names
func (*Type) UnmarshalXML ¶
UnmarshalXML loads the type name and looks it up in the Types registry of type names
type TypeAndName ¶
a type and a name -- useful for specifying configurations of children in Ki nodes, and various other use-cases
type TypeAndNameList ¶
type TypeAndNameList []TypeAndName
list of type-and-names -- can be created from a string spec
func (*TypeAndNameList) SetFromString ¶
func (t *TypeAndNameList) SetFromString(str string) error
construct a type-and-name list from a list of type name pairs, space separated -- can include any json-like { } , [ ] formatting which is all stripped away and just the pairs of names are used
type TypeRegistry ¶
type TypeRegistry struct { // Types is a map from name to reflect.Type Types map[string]reflect.Type // Props are type properties -- nodes can get default properties from // their types and then optionally override them with their own settings Props map[string]map[string]interface{} // Insts contain an instance of each type (the one passed during AddType) Insts map[string]interface{} }
TypeRegistry is a map from type name (package path + "." + type name) to reflect.Type -- need to explicitly register each new type by calling AddType in the process of creating a new global variable, as in:
var KiT_TypeName = ki.Types.AddType(&TypeName{}, [props|nil])
where TypeName is the name of the type -- note that it is ESSENTIAL to pass a pointer so that the type is considered addressable, even after we get Elem() of it.
props is a map[string]interface{} of optional properties that can be associated with the type -- this is used in the GoGi graphical interface system for example to color objects of different types using the background-color property.
var Types TypeRegistry
Types is master registry of types that embed Ki Nodes
func (*TypeRegistry) AddType ¶
func (tr *TypeRegistry) AddType(obj interface{}, props map[string]interface{}) reflect.Type
AddType adds a given type to the registry -- requires an empty object to grab type info from (which is then stored in Insts) -- must be passed as a pointer to ensure that it is an addressable, settable type -- also optional properties that can be associated with the type and accessible e.g. for view-specific properties etc -- these props MUST be specific to this type as they are used directly, not copied!!
func (*TypeRegistry) AllEmbedsOf ¶
func (tr *TypeRegistry) AllEmbedsOf(embed reflect.Type, inclusive, includeBases bool) []reflect.Type
AllEmbedsOf returns a list of all registered types that embed (inherit from in C++ terminology) the given type -- inclusive determines whether the type itself is included in list -- includeBases indicates whether to include types marked with property of base-type -- typically not useful for user-facing type selection
func (*TypeRegistry) AllImplementersOf ¶
AllImplementersOf returns a list of all registered types that implement the given interface type at any level of embedding -- must pass a type constructed like this: reflect.TypeOf((*gi.Node2D)(nil)).Elem() -- includeBases indicates whether to include types marked with property of base-type -- typically not useful for user-facing type selection
func (*TypeRegistry) AllTagged ¶
func (tr *TypeRegistry) AllTagged(key string) []reflect.Type
AllTagged returns a list of all registered types that include a given property key value -- does not check for the value of that value -- just its existence
func (*TypeRegistry) Init ¶
func (tr *TypeRegistry) Init()
Init initializes the type registry, including adding basic types
func (*TypeRegistry) Inst ¶
func (tr *TypeRegistry) Inst(typ reflect.Type) interface{}
Inst returns the interface{} instance of given type (it is a pointer to that type) -- returns nil if not found
func (*TypeRegistry) InstByName ¶
func (tr *TypeRegistry) InstByName(typeName string) interface{}
InstByName returns the interface{} instance of given type (it is a pointer to that type) -- returns nil if not found
func (*TypeRegistry) Prop ¶
func (tr *TypeRegistry) Prop(typ reflect.Type, propKey string) interface{}
Prop safely finds a type property from type and property key -- nil if not found
func (*TypeRegistry) PropByName ¶
func (tr *TypeRegistry) PropByName(typeName, propKey string) interface{}
PropByName safely finds a type property from type name and property key -- nil if not found
func (*TypeRegistry) Properties ¶
func (tr *TypeRegistry) Properties(typ reflect.Type, makeNew bool) map[string]interface{}
Properties returns properties for given type -- optionally makes props map if not already made -- can use this to register properties for types that are not registered
func (*TypeRegistry) PropsByName ¶
func (tr *TypeRegistry) PropsByName(typeName string, makeNew bool) map[string]interface{}
PropsByName returns properties for given type name -- optionally makes props map if not already made -- can use this to register properties for types that are not registered