Documentation ¶
Index ¶
- func NormTimeFormat(timeFmt string) string
- func ValueType(n *Node) string
- type FieldString
- type Node
- func (n *Node) FieldName() string
- func (n *Node) FullName() string
- func (n *Node) GetBoolTag(key string) bool
- func (n *Node) GetMeta(key string) string
- func (n *Node) GetTag(key string) string
- func (n *Node) IsBool() bool
- func (n *Node) IsDuration() bool
- func (n *Node) IsSlice() bool
- func (n *Node) IsString() bool
- func (n *Node) IsStringSlice() bool
- func (n *Node) IsStruct() bool
- func (n *Node) IsTime() bool
- func (n *Node) Kind() reflect.Kind
- func (n *Node) ParentName() string
- func (n *Node) ParentsNames() []string
- func (n *Node) SetBoolTag(key string, value bool)
- func (n *Node) SetFieldValue(s string) error
- func (n *Node) SetMeta(key, value string)
- func (n *Node) SetSlice(vals []string) error
- func (n *Node) SetStruct(v interface{})
- func (n *Node) SetTag(key, value string)
- func (n *Node) SetTime(tv, timeFmt string) (usedFmt string, err error)
- func (n *Node) SliceString() []string
- func (n *Node) String() string
- func (n *Node) TimeString(timeFmt string) string
- func (n *Node) ValueType() string
- type Nodes
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NormTimeFormat ¶
NormTimeFormat accepts an actual time format or a shorthand version of all the common go time package formats using the same name as the variable time in accordance to the "timeFormats" map defined in this module.
If timeFmt is empty then time.RFC3339 is returned.
Types ¶
type FieldString ¶
FieldString defines a func type for creating a field name for node n taking into account it's node heritage from heritage.
type Node ¶
type Node struct { Prefix string // The prefix name representing the parent(s). // Actual struct field value. Can be used to get/set the currently set value. // Note that "ValueBefore" will never get stored as a pointer but the actual value // the struct field pointer points to. FieldValue reflect.Value Field reflect.StructField // Index is the field index in the struct. AKA the field's "order" relative to other // fields in the struct. Note that since private fields are skipped the Index value // can also skip. For example you may end up with fields in the same struct with // Index values {1,2,4,5} because '3' is a private member. Index int // contains filtered or unexported fields }
Node is an abstraction of a struct field.
It contains a reference to the actual field value among other useful information.
func Parents ¶
Parents returns all parents of the child. The returned slice is ordered from oldest parent to the immediate parent of the child.
func (*Node) FieldName ¶
FieldName is offered for convenience in getting the string value of the struct field name. Same as calling "Field.Name".
func (*Node) FullName ¶
FullName returns "Prefix.FieldName". If no prefix exists then "FieldName" is the full name.
func (*Node) GetBoolTag ¶
GetBoolTag behaves like GetTag except the value is parsed as a bool value and returned. If the value doesn't exist then false is returned. If the value does not parse then false is returned.
func (*Node) GetTag ¶
GetTag has the same behavior as "reflect.StructTag.Get" but checks first if the value exists as a runtime override first.
func (*Node) IsDuration ¶
func (*Node) IsStringSlice ¶ added in v0.3.2
func (*Node) IsStruct ¶
IsStruct is called to determine if the node represents a struct. Struct node values cannot be "Get"ed or "Set"ed. They are mostly useful for their field tag information.
If the value is a struct:
Struct node values are typically not set unless it's a specially handled struct type such as time.Time.
Usually struct values are not set directly Unless it's a special struct like time.Time In which case the time.Time struct fields are not recursed but is treated as a special case.
func (*Node) ParentName ¶
ParentName returns the full name of the field parent if one exists.
func (*Node) ParentsNames ¶
ParentsNames returns a list of all parents by full name in order from most to least distant relative.
Returns an empty slice if there is no lineage.
func (*Node) SetBoolTag ¶
SetBoolTag behaves like SetTag only the tag value is correctly set as a string parsable bool.
func (*Node) SetFieldValue ¶
SetValue attempts to convert the field value "fv" represented as a string and assign it to the node value. An error is returned if the string cannot be converted to the underlying go type.
Panics if the node is a pointer, slice or struct.
func (*Node) SetSlice ¶
SetSlice attempts to convert slice values "vals" to the underlying field slice primitive type and set the resulting slice as the field value.
SetSlice is a different method than SetFieldValue because it requires a slice of strings instead of just a string. Even if it accepted a single stream representing a list of items it would also need to know the item separator or other information about separation such as if the starting and terminating "[]" should be removed.
func (*Node) SetStruct ¶
func (n *Node) SetStruct(v interface{})
SetStruct will attempt to assign "v" as the underlying node field value. panics if "v" is not a struct.
func (*Node) SetTime ¶
SetTime attempts to set a time.Time value from a string. Optionally a go format can be provided. "time.Time" types are a special explicitly supported struct case.
If node is not a time.Time type then an error is returned. Returns the timeFmt applied in converting the tv string to a time.Time value.
Default timeFmt format is time.RFC3339.
func (*Node) SliceString ¶
SliceString performs the same action as String but for slices, returning a slice of strings in such a format that the result can be fed back into the "SetSlice" method to obtain the same initial result.
SliceString panics if the underlying field value type is not a slice or the underlying slice type is not on the basic type list.
func (*Node) String ¶
String fulfills the "Stringer" interface and returns the string representation of the value.
The string representation is such that the value could then be set again with one of the appropriate "Set" methods.
func (*Node) TimeString ¶
TimeString is a special case method for handling "time.Time" field types.
It's a separate method because it needs the timeFmt format information in order to know the string format.
TimeString panics if the value type is not "time.Time".
timeFmt can be an exact go time format definition or the format name as it appears in the common time package formats. For example if the value of timeFmt == "RFC3339" then time.RFC3339 format will be used.
type Nodes ¶
type Nodes struct {
// contains filtered or unexported fields
}
Nodes is a struct container for all the generated nodes. It contains a map and slice representation of all nodes. The map allows for named node traversal and the slice maintains original struct field order.
func MakeAllNodes ¶ added in v0.3.0
MakeAllNodes returns a slice of *Nodes from the provided options and interfaces.
func MakeNodes ¶
MakeNodes will generate a map of nodes representing the fields in the struct including all child struct fields recursively.
The map key is a dot separated string Field.ChildField.ChildField where Field is a struct field name and ChildField is an embedded struct field.
MakeNodes only returns public fields. Thus there is no risk of editing an un-editable field and causing a panic.
s interface{} must be a struct pointer or MakeNodes will panic.
Certain types do not map for the purposes of reading in configuration values. Therefore the following types are ignored: - arrays (slices are used) - functions - channels - complex - interface - maps
Note that any nil pointers will get initialized. Therefore, using "MakeNodes" has the side effect of fully initializing the provided struct and all its sub-parts (except private members which are skipped).
"time.Time" is NOT included by default in the Options.NoFollow list.
The nodes returned from "MakeNodes" should not be deleted or modified except through sanctioned node methods as there may be unintended side effects.
type Options ¶
type Options struct { // NoFollow is a slice of strings of struct types // where the underlying struct fields are ignored but // the struct itself is still added as a node. NoFollow []string // IgnoreTypes are types that are ignored when generating the // struct node tree. Certain underlying go types such as functions and channels // are always ignored. This list allows the user to define a list of types // such as a custom "int" type. IgnoreTypes []string }