node

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2021 License: MIT Imports: 7 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormTimeFormat

func NormTimeFormat(timeFmt string) string

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.

func ValueType

func ValueType(n *Node) string

ValueType returns a simple string representation of the type.

Types

type FieldString

type FieldString func(n *Node, heritage []*Node) (name string)

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 Parent

func Parent(child *Node, nodes map[string]*Node) (parent *Node)

Parent returns the parent struct node if one exists.

func Parents

func Parents(child *Node, nodes map[string]*Node) (parents []*Node)

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

func (n *Node) FieldName() string

FieldName is offered for convenience in getting the string value of the struct field name. Same as calling "Field.Name".

func (*Node) FullName

func (n *Node) FullName() string

FullName returns "Prefix.FieldName". If no prefix exists then "FieldName" is the full name.

func (*Node) GetBoolTag

func (n *Node) GetBoolTag(key string) bool

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) GetMeta added in v0.3.2

func (n *Node) GetMeta(key string) string

func (*Node) GetTag

func (n *Node) GetTag(key string) string

GetTag has the same behavior as "reflect.StructTag.Get" but checks first if the value exists as a runtime override first.

func (*Node) IsBool

func (n *Node) IsBool() bool

func (*Node) IsDuration

func (n *Node) IsDuration() bool

func (*Node) IsSlice

func (n *Node) IsSlice() bool

func (*Node) IsString added in v0.3.2

func (n *Node) IsString() bool

func (*Node) IsStringSlice added in v0.3.2

func (n *Node) IsStringSlice() bool

func (*Node) IsStruct

func (n *Node) IsStruct() bool

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) IsTime

func (n *Node) IsTime() bool

IsTime returns true when the node represents a time.Time struct.

func (*Node) Kind

func (n *Node) Kind() reflect.Kind

Kind is for convenience instead of calling n.FieldValue.Kind().

func (*Node) ParentName

func (n *Node) ParentName() string

ParentName returns the full name of the field parent if one exists.

func (*Node) ParentsNames

func (n *Node) ParentsNames() []string

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

func (n *Node) SetBoolTag(key string, value bool)

SetBoolTag behaves like SetTag only the tag value is correctly set as a string parsable bool.

func (*Node) SetFieldValue

func (n *Node) SetFieldValue(s string) error

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) SetMeta added in v0.3.2

func (n *Node) SetMeta(key, value string)

func (*Node) SetSlice

func (n *Node) SetSlice(vals []string) error

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) SetTag

func (n *Node) SetTag(key, value string)

func (*Node) SetTime

func (n *Node) SetTime(tv, timeFmt string) (usedFmt string, err error)

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

func (n *Node) SliceString() []string

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

func (n *Node) String() 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

func (n *Node) TimeString(timeFmt string) string

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.

func (*Node) ValueType

func (n *Node) ValueType() string

ValueType is the string value of field.Type().String()

For reference: time.Time == "time.Time" time.Duration == "time.Duration"

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

func MakeAllNodes(o Options, vs ...interface{}) []*Nodes

MakeAllNodes returns a slice of *Nodes from the provided options and interfaces.

func MakeNodes

func MakeNodes(o Options, v interface{}) *Nodes

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.

func (*Nodes) List

func (ns *Nodes) List() []*Node

func (*Nodes) Map

func (ns *Nodes) Map() map[string]*Node

func (*Nodes) SetTag added in v0.3.2

func (ns *Nodes) SetTag(fieldName, key, value string) error

SetTag will attempt to set fieldName Node tag with key and value. An error is returned if the node is not found.

Note that fieldName is the dot "." separated field path.

func (*Nodes) StructPtr added in v0.3.0

func (ns *Nodes) StructPtr() interface{}

StructPtr returns the underlying struct pointer.

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
}

Jump to

Keyboard shortcuts

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