GoStruct

package
v0.0.0-...-391253a Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: GPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package GoStruct - A framework for type agnostic and recursive data.

Package GoStruct - Snaffooed from https://github.com/fatih/structs

Package GoStruct - Snaffooed from https://github.com/fatih/structs Package GoStruct contains various utilities functions to work with structs.

Package GoStruct - Snaffooed from https://github.com/fatih/structs

Index

Constants

View Source
const (
	// NameGoStruct - Name of field within structure that allows for assigning tags to the parent.
	// Add like this:
	// type ResultData []struct {
	//     GoStruct      GoStruct.GoStruct  `json:"-" DataTable:"true" DataTableSortOn:"UnitConvertId"`
	//
	// ...
	//
	// }
	NameGoStruct       = "GoStruct"
	PkgGoStruct        = "GoStruct.GoStruct"
	NameGoStructParent = "GoStructParent"
	PkgGoStructParent  = "GoStruct.GoStructParent"

	// PointId - Point id in the form p\d+ or \d+ or free-form text.
	PointId = "PointId"
	// PointIdFrom -  Searches current structure for field value to use for naming when hitting a slice, (as opposed to using an index).
	PointIdFrom = "PointIdFrom"
	// PointIdFromChild -  Searches child for field value to use for naming when hitting a slice, (as opposed to using an index).
	PointIdFromChild = "PointIdFromChild"
	// PointIdReplace -  Replace PointNameFrom instead of append.
	PointIdReplace = "PointIdReplace"

	// PointParentId -  Associated parent of point.
	PointParentId = "PointParentId"

	// PointUpdateFreq -  Point update frequency - Total, Yearly, Monthly, Day.
	PointUpdateFreq   = "PointUpdateFreq"
	UpdateFreqInstant = "instant"
	UpdateFreq5Mins   = "5mins"
	UpdateFreq15Mins  = "15mins"
	UpdateFreq30Mins  = "30mins"
	UpdateFreqBoot    = "boot"
	UpdateFreqDay     = "daily"
	UpdateFreqMonth   = "monthly"
	UpdateFreqYear    = "yearly"
	UpdateFreqTotal   = "total"

	// PointValueType -  Value type of point: energy, date, battery, temperature.
	PointValueType = "PointValueType"
	// PointUnit -  Units: Wh, kWh, C, h.
	PointUnit = "PointUnit"
	// PointUnitFrom -  Get PointUnit from another field structure.
	PointUnitFrom = "PointUnitFrom"
	// PointUnitFromParent -  Get PointUnit from another parent field structure.
	PointUnitFromParent = "PointUnitFromParent"
	// PointVariableUnit -  Will be any number of units - used mainly in table display.
	PointVariableUnit = "PointVariableUnit"

	// PointIgnore -  Ignore this point.
	PointIgnore = "PointIgnore"
	// PointIgnoreIfNil -  Ignore this point if a child is nil or empty.
	PointIgnoreIfNil = "PointIgnoreIfNil"
	// PointIgnoreIfChildFromNil -  Ignore this point if a child is nil or empty.
	PointIgnoreIfChildFromNil = "PointIgnoreIfChildFromNil"
	// PointIgnoreZero -  Ignore arrays with zero size, (default true).
	PointIgnoreZero = "PointIgnoreZero"

	// PointAliasTo -  Alias this point to another point.
	PointAliasTo = "PointAliasTo"
	// PointAliasFrom -  Alias this point from another point.
	PointAliasFrom = "PointAliasFrom"
	// PointVirtual -  Create a Virtual point alias.
	PointVirtual = "PointVirtual"
	// PointVirtualShift -  Create a Virtual point alias.
	PointVirtualShift = "PointVirtualShift"

	// PointGroupName -  Point group name.
	PointGroupName = "PointGroupName"
	// PointGroupNameFrom -  Get PointGroupName from another field structure.
	PointGroupNameFrom = "PointGroupNameFrom"

	// PointName -  Human-readable name of point.
	PointName = "PointName"

	// PointIcon -  Icon of point.
	PointIcon = "PointIcon"

	// PointNameDateFormat -  Date format when using PointNameFrom, (if the field is a time.Time type).
	PointNameDateFormat = "PointNameDateFormat"

	// PointArrayFlatten -  Flatten an array into a string. EG: ["one", "two", "three"]
	PointArrayFlatten = "PointArrayFlatten"

	// PointSplitForce -  Force a point to split.
	PointSplitForce = "PointSplitForce"

	// PointSplitOn -  Split a point into an array separating by defined string.
	PointSplitOn = "PointSplitOn"

	// PointSplitOnType -  What valueTypes will be used for a split.
	PointSplitOnType = "PointSplitOnType"

	// PointTimestampFrom -  Pull timestamp from another field structure.
	PointTimestampFrom = "PointTimestampFrom"

	// PointValueReplace - Search endpoint value and replace with string contained in PointValueReplaceWith
	PointValueReplace = "PointValueReplace"
	// PointValueReplaceWith -
	PointValueReplaceWith = "PointValueReplaceWith"

	// PointDevice - Define a PointDevice for this point.
	PointDevice = "PointDevice"
	// PointDeviceFrom - Reference PointDevice from another sibling.
	PointDeviceFrom = "PointDeviceFrom"
	// PointDeviceFromParent - Reference PointDevice from a parent.
	PointDeviceFromParent = "PointDeviceFromParent"

	// IsDataTable -  This entity is a data table - Will only traverse down one child.
	IsDataTable = "DataTable"
	// DataTableChild - Will be included in table listings.
	DataTableChild = "DataTableChild"
	// DataTableId -  Table id, (defaults to Json tag).
	DataTableId = "DataTableId"
	// DataTableName -  Table Name, (defaults to DataTableId).
	DataTableName = "DataTableName"
	// DataTableTitle -  Table Title, (defaults to DataTableId in name format).
	DataTableTitle = "DataTableTitle"
	// DataTableMerge -  Merge rows together - useful for when we use, for EG: []valueTypes.Float
	DataTableMerge = "DataTableMerge"
	// DataTableIndex -  Show index on table.
	DataTableIndex = "DataTableIndex"
	// DataTableIndexTitle -  Show index title on table.
	DataTableIndexTitle = "DataTableIndexTitle"
	// DataTableIndexNames -  Use this comma separated list of names to replace index numbers.
	DataTableIndexNames = "DataTableIndexNames"
	// DataTableSortOn -  Sort table using this Field.
	DataTableSortOn = "DataTableSortOn"
	// DataTablePivot -  Pivot the table.
	DataTablePivot = "DataTablePivot"

	// PointListFlatten -  Flatten a point into a string - only for list display.
	PointListFlatten = "PointListFlatten"
)

Variables

View Source
var (
	// DefaultTagName is the default tag name for struct fields which provides
	// a more granular to tweak certain structs. Lookup the necessary functions
	// for more info.
	DefaultTagName = "structs" // struct's field default tag name
)

Functions

func AddFloatValues

func AddFloatValues(precision int, refs ...*Reflect) float64

func AddIntegerValues

func AddIntegerValues(refs ...*Reflect) int64

func FillMap

func FillMap(s interface{}, out map[string]interface{})

FillMap is the same as Map. Instead of returning the output, it fills the given map.

func GetChildGoStruct

func GetChildGoStruct(ref interface{}, limit int) (*DataTags, *DataTags)

func GetStructFields

func GetStructFields(ref interface{}) map[string]string

func GetStructFieldsAsArray

func GetStructFieldsAsArray(ref *Reflect) []string

func GetStructValuesAsArray

func GetStructValuesAsArray(ref *Reflect) []string

func HasZero

func HasZero(s interface{}) bool

HasZero returns true if any field is equal to a zero value. For more info refer to Struct types HasZero() method. It panics if s's kind is not struct.

func IsStruct

func IsStruct(s interface{}) bool

IsStruct returns true if the given variable is a struct or a pointer to struct.

func IsZero

func IsZero(s interface{}) bool

IsZero returns true if all fields is equal to a zero value. For more info refer to Struct types IsZero() method. It panics if s's kind is not struct.

func Map

func Map(s interface{}) map[string]interface{}

Map converts the given struct to a map[string]interface{}. For more info refer to Struct types Map() method. It panics if s's kind is not struct.

func Name

func Name(s interface{}) string

Name returns the structs's type name within its package. It returns an empty string for unnamed types. It panics if s's kind is not struct.

func Names

func Names(s interface{}) []string

Names returns a slice of field names. For more info refer to Struct types Names() method. It panics if s's kind is not struct.

func StrSet

func StrSet(src string, dst string) string

func Values

func Values(s interface{}) []interface{}

Values converts the given struct to a []interface{}. For more info refer to Struct types Values() method. It panics if s's kind is not struct.

func VerifyOptionsRequired

func VerifyOptionsRequired(ref interface{}) error

VerifyOptionsRequired Verify fields within the structure are required.

Types

type DataTags

type DataTags struct {
	Endpoint EndPointPath `json:"endpoint,omitempty"`

	Required bool `json:"required"`

	Json string `json:"json,omitempty"`

	PointDevice           string `json:"point_device,omitempty"`
	PointDeviceFrom       string `json:"point_device_from,omitempty"`
	PointDeviceFromParent string `json:"point_device_from_parent,omitempty"`
	PointId               string `json:"point_id,omitempty"`

	PointUpdateFreq string `json:"point_update_freq,omitempty"`
	PointValueType  string `json:"point_value_type,omitempty"`

	PointAliasTo      string `json:"point_alias_to,omitempty"`
	PointVirtual      bool   `json:"point_virtual,omitempty"`
	PointVirtualShift int64  `json:"point_virtual_shift,omitempty"`

	PointTimestamp     time.Time `json:"point_timestamp,omitempty"`
	PointTimestampFrom string    `json:"point_timestamp_from,omitempty"`

	PointUnit           string `json:"point_unit,omitempty"`
	PointUnitFrom       string `json:"point_unit_from,omitempty"`
	PointUnitFromParent string `json:"point_unit_from_parent,omitempty"`
	PointVariableUnit   bool   `json:"point_variable_unit,omitempty"`

	PointName        string `json:"point_name,omitempty"`
	PointIdReplace   bool   `json:"point_name_append,omitempty"`
	PointIdFrom      string `json:"point_id_from,omitempty"`
	PointIdFromChild string `json:"point_name_from_child,omitempty"`
	// PointIdFromParent         string    `json:"point_name_from_parent,omitempty"`
	PointNameDateFormat string `json:"point_name_date_format,omitempty"`

	PointIcon string `json:"point_icon,omitempty"`

	PointIgnore               bool   `json:"point_ignore,omitempty"`
	PointIgnoreZero           bool   `json:"point_ignore_zero,omitempty"`
	PointIgnoreIfNil          string `json:"point_ignore_if_nil,omitempty"`
	PointIgnoreIfChildFromNil string `json:"point_ignore_if_child_nil,omitempty"`

	PointGroupName     string `json:"point_group_name,omitempty"`
	PointGroupNameFrom string `json:"point_group_name_from,omitempty"`
	PointArrayFlatten  bool   `json:"point_array_flatten,omitempty"`
	PointListFlatten   bool   `json:"point_list_flatten,omitempty"`

	PointSplitOn     string `json:"point_split_on,omitempty"`
	PointSplitOnType string `json:"point_split_on_type,omitempty"`

	PointValueReplace     string `json:"point_value_replace,omitempty"`
	PointValueReplaceWith string `json:"point_value_replace_with,omitempty"`

	DataTable           bool     `json:"data_table,omitempty"`
	DataTableChild      bool     `json:"data_table_child,omitempty"`
	DataTablePivot      bool     `json:"data_table_pivot,omitempty"`
	DataTableId         string   `json:"data_table_id,omitempty"`
	DataTableName       string   `json:"data_table_name,omitempty"`
	DataTableTitle      string   `json:"data_table_title,omitempty"`
	DataTableMerge      bool     `json:"data_table_merge,omitempty"`
	DataTableIndex      bool     `json:"data_table_show_index,omitempty"`
	DataTableIndexNames []string `json:"data_table_index_names,omitempty"`
	DataTableSortOn     string   `json:"data_table_sort_on,omitempty"`
	DataTableIndexTitle string   `json:"data_table_index_title,omitempty"`

	ValueType string `json:"value_type,omitempty"`
	ValueKind string `json:"value_kind,omitempty"`

	StrBool tagStrings `json:"-"`
}

func (*DataTags) GetTags

func (ds *DataTags) GetTags(fieldTo reflect.StructField, fieldVo reflect.Value) *DataTags

GetTags -

func (*DataTags) SetFrom

func (ds *DataTags) SetFrom(from *DataTags) error

SetFrom - Copy DataTags structure from src to dst with zero/nil checking.

func (DataTags) String

func (ds DataTags) String() string

func (*DataTags) UpdateTags

func (ds *DataTags) UpdateTags(parent *Reflect, current *Reflect) *DataTags

type EndPointPath

type EndPointPath struct {
	// contains filtered or unexported fields
}

func NewEndPointPath

func NewEndPointPath(path ...string) EndPointPath

func (*EndPointPath) AddString

func (e *EndPointPath) AddString(path ...string) string

func (*EndPointPath) Append

func (e *EndPointPath) Append(path ...string)

func (*EndPointPath) Clear

func (e *EndPointPath) Clear()

func (*EndPointPath) Copy

func (e *EndPointPath) Copy() EndPointPath

func (*EndPointPath) First

func (e *EndPointPath) First() string

func (*EndPointPath) Index

func (e *EndPointPath) Index(index int) string

func (*EndPointPath) InsertFirst

func (e *EndPointPath) InsertFirst(s string)

func (*EndPointPath) IsBeginsWith

func (e *EndPointPath) IsBeginsWith(compare string) bool

func (*EndPointPath) IsZero

func (e *EndPointPath) IsZero() bool

func (*EndPointPath) Last

func (e *EndPointPath) Last() string

func (EndPointPath) MarshalJSON

func (e EndPointPath) MarshalJSON() ([]byte, error)

MarshalJSON - Convert value to JSON

func (*EndPointPath) PopLast

func (e *EndPointPath) PopLast()

func (*EndPointPath) ReplaceFirst

func (e *EndPointPath) ReplaceFirst(s string)

func (*EndPointPath) ShiftLeft

func (e *EndPointPath) ShiftLeft(s int64)

func (EndPointPath) String

func (e EndPointPath) String() string

func (*EndPointPath) Strings

func (e *EndPointPath) Strings() []string

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field represents a single struct field that encapsulates high level functions around the field.

func Fields

func Fields(s interface{}) []*Field

Fields returns a slice of *Field. For more info refer to Struct types Fields() method. It panics if s's kind is not struct.

func (*Field) Field

func (f *Field) Field(name string) *Field

Field returns the field from a nested struct. It panics if the nested struct is not exported or if the field was not found.

func (*Field) FieldOk

func (f *Field) FieldOk(name string) (*Field, bool)

FieldOk returns the field from a nested struct. The boolean returns whether the field was found (true) or not (false).

func (*Field) Fields

func (f *Field) Fields() []*Field

Fields returns a slice of Fields. This is particular handy to get the fields of a nested struct . A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field *http.Request `structs:"-"`

It panics if field is not exported or if field's kind is not struct

func (*Field) IsEmbedded

func (f *Field) IsEmbedded() bool

IsEmbedded returns true if the given field is an anonymous field (embedded)

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field is not initialized (has a zero value). It panics if the field is not exported.

func (*Field) Kind

func (f *Field) Kind() reflect.Kind

Kind returns the fields kind, such as "string", "map", "bool", etc ..

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) Set

func (f *Field) Set(val interface{}) error

Set sets the field to given value v. It returns an error if the field is not settable (not addressable or not exported) or if the given value's type doesn't match the fields type.

func (*Field) Tag

func (f *Field) Tag(key string) string

Tag returns the value associated with key in the tag string. If there is no such key in the tag, Tag returns the empty string.

func (*Field) Value

func (f *Field) Value() interface{}

Value returns the underlying value of the field. It panics if the field is not exported.

func (*Field) Zero

func (f *Field) Zero() error

Zero sets the field to its zero value. It returns an error if the field is not settable (not addressable or not exported).

type GoStruct

type GoStruct bool

type GoStructParent

type GoStructParent bool

type Reflect

type Reflect struct {
	FieldPath       EndPointPath
	ParentReflect   *Reflect
	CurrentReflect  *Reflect
	ChildReflect    []*Reflect
	ChildReflectMap map[string]*Reflect

	Valid         bool
	DataStructure DataTags
	GoStructs     struct {
		Parent  *DataTags
		Current *DataTags
	}

	Interface  interface{}
	IsNil      bool
	IsExported bool

	IsOk           bool
	Value          valueTypes.UnitValues
	InterfaceValue interface{}
	IsStart        bool

	Index     int
	Length    int
	FieldName string
	Kind      reflect.Kind
	TypeOf    reflect.Type
	ValueOf   reflect.Value
	FieldTo   reflect.StructField
	FieldVo   reflect.Value
	// contains filtered or unexported fields
}

Reflect - Combines all the common reflect work into one package So we don't have to keep repeating ourselves.

func (*Reflect) AsJson

func (r *Reflect) AsJson() string

func (*Reflect) Copy

func (r *Reflect) Copy() Reflect

func (*Reflect) CopyEndPointPath

func (r *Reflect) CopyEndPointPath() EndPointPath

func (*Reflect) CountChildren

func (r *Reflect) CountChildren() (int, int)

func (*Reflect) EndPointPath

func (r *Reflect) EndPointPath() *EndPointPath

func (*Reflect) GetDataTableIndexNames

func (r *Reflect) GetDataTableIndexNames() []string

func (*Reflect) GetFieldPath

func (r *Reflect) GetFieldPath() EndPointPath

func (*Reflect) GetGoStruct

func (r *Reflect) GetGoStruct() *DataTags

func (*Reflect) GetGoStructCurrent

func (r *Reflect) GetGoStructCurrent() *Reflect

func (*Reflect) GetValueFloat

func (r *Reflect) GetValueFloat() float64

func (*Reflect) HasGoStruct

func (r *Reflect) HasGoStruct() bool

func (*Reflect) Init

func (r *Reflect) Init(parent interface{}, current interface{}, name EndPointPath)

func (*Reflect) IsDataTableIndex

func (r *Reflect) IsDataTableIndex() bool

func (*Reflect) IsDataTableMerge

func (r *Reflect) IsDataTableMerge() bool

func (*Reflect) IsGoStruct

func (r *Reflect) IsGoStruct() bool

func (*Reflect) IsGoStructForParent

func (r *Reflect) IsGoStructForParent() bool

func (*Reflect) IsGroup

func (r *Reflect) IsGroup() bool

func (*Reflect) IsKnown

func (r *Reflect) IsKnown() bool

func (*Reflect) IsNotPointIdReplace

func (r *Reflect) IsNotPointIdReplace() bool

func (*Reflect) IsNotTable

func (r *Reflect) IsNotTable() bool

func (*Reflect) IsPointArrayFlatten

func (r *Reflect) IsPointArrayFlatten() bool

func (*Reflect) IsPointIdReplace

func (r *Reflect) IsPointIdReplace() bool

func (*Reflect) IsPointIgnore

func (r *Reflect) IsPointIgnore() bool

func (*Reflect) IsPointIgnoreZero

func (r *Reflect) IsPointIgnoreZero() bool

func (*Reflect) IsPointListFlatten

func (r *Reflect) IsPointListFlatten() bool

func (*Reflect) IsPointTimestampNotZero

func (r *Reflect) IsPointTimestampNotZero() bool

func (*Reflect) IsPointTimestampZero

func (r *Reflect) IsPointTimestampZero() bool

func (*Reflect) IsPointVariableUnit

func (r *Reflect) IsPointVariableUnit() bool

func (*Reflect) IsPointVirtual

func (r *Reflect) IsPointVirtual() bool

func (*Reflect) IsTable

func (r *Reflect) IsTable() bool

func (*Reflect) IsTableChild

func (r *Reflect) IsTableChild() (bool, int)

func (*Reflect) IsUnknown

func (r *Reflect) IsUnknown() bool

func (*Reflect) Name

func (r *Reflect) Name() string

func (*Reflect) PointGroupName

func (r *Reflect) PointGroupName() string

func (*Reflect) PointIcon

func (r *Reflect) PointIcon() string

func (*Reflect) PointId

func (r *Reflect) PointId() string

func (*Reflect) PointIgnoreIfChildFromNil

func (r *Reflect) PointIgnoreIfChildFromNil() bool

func (*Reflect) PointName

func (r *Reflect) PointName() string

func (*Reflect) PointUpdateFreq

func (r *Reflect) PointUpdateFreq() string

func (*Reflect) SetByIndex

func (r *Reflect) SetByIndex(parent *Reflect, current *Reflect, index int, indexName reflect.Value)

func (*Reflect) SetDataTableIndexNames

func (r *Reflect) SetDataTableIndexNames(args ...string)

func (*Reflect) SetDataTableIndexTitle

func (r *Reflect) SetDataTableIndexTitle(args string)

func (*Reflect) SetEndPointPath

func (r *Reflect) SetEndPointPath(epp EndPointPath) *EndPointPath

func (*Reflect) SetGoStructOptions

func (r *Reflect) SetGoStructOptions(limit int) bool

SetGoStructOptions - Copies and updates DataTags from a GoStruct object.

func (*Reflect) SetPointId

func (r *Reflect) SetPointId() EndPointPath

SetPointId - Sets the EndPointPath based off struct tags?

func (*Reflect) SetUnit

func (r *Reflect) SetUnit(unit string)

func (*Reflect) SetUnitValue

func (r *Reflect) SetUnitValue(value valueTypes.UnitValue)

func (*Reflect) SetUnitValues

func (r *Reflect) SetUnitValues(value valueTypes.UnitValues)

func (*Reflect) SetValue

func (r *Reflect) SetValue(value interface{})

func (*Reflect) SetValuePrecision

func (r *Reflect) SetValuePrecision(precision int)

func (Reflect) String

func (r Reflect) String() string

func (*Reflect) UpdateUnit

func (r *Reflect) UpdateUnit()

func (*Reflect) ValueFirst

func (r *Reflect) ValueFirst() *valueTypes.UnitValue

func (*Reflect) ValueLast

func (r *Reflect) ValueLast() *valueTypes.UnitValue

func (*Reflect) ValueLength

func (r *Reflect) ValueLength() int

func (*Reflect) ValueType

func (r *Reflect) ValueType() string

func (*Reflect) ValueUnit

func (r *Reflect) ValueUnit() string

func (*Reflect) ValuesRange

func (r *Reflect) ValuesRange() []valueTypes.UnitValue

type ReflectArray

type ReflectArray []ReflectArrayRow

func (*ReflectArray) AddRow

func (ta *ReflectArray) AddRow(refs ...*Reflect) ReflectArray

func (*ReflectArray) GetRow

func (ta *ReflectArray) GetRow(row int) ReflectArrayRow

type ReflectArrayRow

type ReflectArrayRow []*Reflect

type ReflectMap

type ReflectMap map[string]Reflect

type Required

type Required []string

func GetOptionsRequired

func GetOptionsRequired(ref interface{}) Required

GetOptionsRequired Get field options within the structure that are required.

func (*Required) IsNotRequired

func (r *Required) IsNotRequired(field string) bool

func (*Required) IsRequired

func (r *Required) IsRequired(field string) bool

type Struct

type Struct struct {
	TagName string
	// contains filtered or unexported fields
}

Struct encapsulates a struct type to provide several high level functions around the struct.

func New

func New(s interface{}) *Struct

New returns a new *Struct with the struct s. It panics if the s's kind is not struct.

func (*Struct) Field

func (s *Struct) Field(name string) *Field

Field returns a new Field struct that provides several high level functions around a single struct field entity. It panics if the field is not found.

func (*Struct) FieldOk

func (s *Struct) FieldOk(name string) (*Field, bool)

FieldOk returns a new Field struct that provides several high level functions around a single struct field entity. The boolean returns true if the field was found.

func (*Struct) Fields

func (s *Struct) Fields() []*Field

Fields returns a slice of Fields. A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

It panics if s's kind is not struct.

func (*Struct) FillMap

func (s *Struct) FillMap(out map[string]interface{})

FillMap is the same as Map. Instead of returning the output, it fills the given map.

func (*Struct) HasZero

func (s *Struct) HasZero() bool

HasZero returns true if a field in a struct is not initialized (zero value). A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected. It panics if s's kind is not struct.

func (*Struct) IsZero

func (s *Struct) IsZero() bool

IsZero returns true if all fields in a struct is a zero value (not initialized) A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected. It panics if s's kind is not struct.

func (*Struct) Map

func (s *Struct) Map() map[string]interface{}

Map converts the given struct to a map[string]interface{}, where the keys of the map are the field names and the values of the map the associated values of the fields. The default key string is the struct field name but can be changed in the struct field's tag value. The "structs" key in the struct's field tag value is the key name. Example:

// Field appears in map as key "myName".
Name string `structs:"myName"`

A tag value with the content of "-" ignores that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A tag value with the content of "string" uses the stringer to get the value. Example:

// The value will be output of Animal's String() func.
// Map will panic if Animal does not implement String().
Field *Animal `structs:"field,string"`

A tag value with the option of "flatten" used in a struct field is to flatten its fields in the output map. Example:

// The FieldStruct's fields will be flattened into the output map.
FieldStruct time.Time `structs:",flatten"`

A tag value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

A tag value with the option of "omitempty" ignores that particular field if the field value is empty. Example:

// Field appears in map as key "myName", but the field is
// skipped if empty.
Field string `structs:"myName,omitempty"`

// Field appears in map as key "Field" (the default), but
// the field is skipped if empty.
Field string `structs:",omitempty"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected.

func (*Struct) Name

func (s *Struct) Name() string

Name returns the structs's type name within its package. For more info refer to Name() function.

func (*Struct) Names

func (s *Struct) Names() []string

Names returns a slice of field names. A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

It panics if s's kind is not struct.

func (*Struct) Values

func (s *Struct) Values() []interface{}

Values converts the given s struct's field values to a []interface{}. A struct tag with the content of "-" ignores the that particular field. Example:

// Field is ignored by this package.
Field int `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Fields is not processed further by this package.
Field time.Time     `structs:",omitnested"`
Field *http.Request `structs:",omitnested"`

A tag value with the option of "omitempty" ignores that particular field and is not added to the values if the field value is empty. Example:

// Field is skipped if empty
Field string `structs:",omitempty"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected.

type StructMap

type StructMap struct {
	StructMapOptions

	SortOn     string
	Start      *Reflect
	Map        map[string]*Reflect
	TableMap   map[string]*Reflect
	VirtualMap map[string]*Reflect

	Error error
	// contains filtered or unexported fields
}

func (*StructMap) Add

func (sm *StructMap) Add(Current *Reflect)

func (*StructMap) AddTable

func (sm *StructMap) AddTable(Current *Reflect)

func (*StructMap) AddVirtual

func (sm *StructMap) AddVirtual(Current *Reflect)

func (*StructMap) Exists

func (sm *StructMap) Exists(Current *Reflect) bool

func (*StructMap) InitScan

func (sm *StructMap) InitScan(current interface{}, options StructMapOptions)

func (*StructMap) IsEmpty

func (sm *StructMap) IsEmpty(Current *Reflect) bool

func (*StructMap) IsInvalid

func (sm *StructMap) IsInvalid(Current *Reflect)

func (*StructMap) IsNil

func (sm *StructMap) IsNil(Current *Reflect) bool

func (*StructMap) IsPointArrayFlatten

func (sm *StructMap) IsPointArrayFlatten(Current *Reflect) bool

func (*StructMap) IsPointSplitOn

func (sm *StructMap) IsPointSplitOn(Current *Reflect) bool

func (*StructMap) IsTable

func (sm *StructMap) IsTable(Current *Reflect) bool

func (*StructMap) IsUnexported

func (sm *StructMap) IsUnexported(Current *Reflect) bool

func (*StructMap) IsUnsupported

func (sm *StructMap) IsUnsupported(Current *Reflect)

func (*StructMap) PrintDebug

func (sm *StructMap) PrintDebug(format string, args ...interface{})

func (*StructMap) Process

func (sm *StructMap) Process(Child *Reflect) bool

func (*StructMap) Scan

func (sm *StructMap) Scan(Parent *Reflect, Current *Reflect) *StructMap

func (*StructMap) ScanMap

func (sm *StructMap) ScanMap(Parent *Reflect, Current *Reflect) bool

func (*StructMap) ScanSlice

func (sm *StructMap) ScanSlice(Parent *Reflect, Current *Reflect) bool

func (*StructMap) ScanStruct

func (sm *StructMap) ScanStruct(Parent *Reflect, Current *Reflect) bool

type StructMapOptions

type StructMapOptions struct {
	StartAt        string
	Name           EndPointPath
	TimeStamp      time.Time
	Debug          bool
	AddUnexported  bool // Add unexported values, (default: false).
	AddUnsupported bool // Add unsupported types, (default: false).
	AddInvalid     bool // Add invalid types, (default: false).
	AddEmpty       bool // Add empty values, (default: false).
	AddNil         bool // Add nil values, (default: false).
}

type StructTable

type StructTable struct {
	Area    string
	Name    string
	Current *Reflect

	MapName    string
	Reflects   ReflectArray
	Values     StructValues
	Table      output.Table
	SortOn     string
	ShowIndex  bool
	IndexTitle string
	IsValid    bool
	Columns    []string
	Rows       int
	Cols       int
	ActualRows int
	ActualCols int

	Debug bool
	Error error
}

func (*StructTable) AddRow

func (ta *StructTable) AddRow(refs ...*Reflect)

func (*StructTable) CreateTable

func (ta *StructTable) CreateTable() (output.Table, error)

func (*StructTable) Get

func (ta *StructTable) Get() ReflectArray

func (*StructTable) GetHeaders

func (ta *StructTable) GetHeaders() []string

func (*StructTable) GetRow

func (ta *StructTable) GetRow(row int) ReflectArrayRow

func (*StructTable) GetValues

func (ta *StructTable) GetValues() StructValues

func (*StructTable) PrintDebug

func (ta *StructTable) PrintDebug(format string, args ...interface{})

func (*StructTable) Process

func (ta *StructTable) Process(area string, name string, Current *Reflect) error

type StructTables

type StructTables map[string]*StructTable

func (*StructTables) GetTableNames

func (sm *StructTables) GetTableNames() []string

type StructValue

type StructValue map[string]valueTypes.UnitValue

type StructValues

type StructValues []StructValue

func (*StructValues) GetCell

func (ta *StructValues) GetCell(row int, col string) valueTypes.UnitValue

type StructValuesMap

type StructValuesMap map[string]StructValues

Directories

Path Synopsis
Package gojson - generates go struct defintions from JSON documents
Package gojson - generates go struct defintions from JSON documents

Jump to

Keyboard shortcuts

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