Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder interface { // AddField creates new struct's field. // It expects field's name, type and string. // Type is provided as an instance of some golang type. // Tag is provided as classical golang field tag. // // builder.AddField("SomeFloatField", 0.0, `json:"boolean" validate:"gte=10"`) // AddField(name string, typ interface{}, tag string) Builder // RemoveField removes existing struct's field. // // builder.RemoveField("SomeFloatField") // RemoveField(name string) Builder // HasField checks if struct has a field with a given name. // // if builder.HasField("SomeFloatField") { ... // HasField(name string) bool // GetField returns struct's field definition. // If there is no such field, it returns nil. // Usable to edit existing struct's field. // // field := builder.GetField("SomeFloatField") // GetField(name string) FieldConfig // Build returns definition for dynamic struct. // Definition can be used to create new instances. // // dStruct := builder.Build() // Build() DynamicStruct }
Builder holds all fields' definitions for desired structs. It gives opportunity to add or remove fields, or to edit existing ones. In the end, it provides definition for dynamic structs, used to create new instances.
func ExtendStruct ¶
func ExtendStruct(value interface{}) Builder
ExtendStruct extends existing instance of struct and returns new instance of Builder interface.
builder := dynamicstruct.MergeStructs(MyStruct{})
func MergeStructs ¶
func MergeStructs(values ...interface{}) Builder
MergeStructs merges a list of existing instances of structs and returns new instance of Builder interface.
builder := dynamicstruct.MergeStructs(MyStructOne{}, MyStructTwo{}, MyStructThree{})
type DynamicStruct ¶
type DynamicStruct interface { // New provides new instance of defined dynamic struct. // // value := dStruct.New() // New() interface{} // NewSliceOfStructs provides new slice of defined dynamic struct, with 0 length and capacity. // // value := dStruct.NewSliceOfStructs() // NewSliceOfStructs() interface{} // New provides new map of defined dynamic struct with desired key type. // // value := dStruct.NewMapOfStructs("") // NewMapOfStructs(key interface{}) interface{} }
DynamicStruct contains defined dynamic struct. This definition can't be changed anymore, once is built. It provides a method for creating new instances of same defintion.
type Field ¶
type Field interface { // GetName returns field's name in struct. // // name := field.GetName() // Name() string // PointerInt returns a pointer for instance of int type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").PointerInt() // PointerInt() *int // Int returns an instance of int type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").Int() // Int() int // PointerInt8 returns a pointer for instance of int8 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").PointerInt8() // PointerInt8() *int8 // Int8 returns aan instance of int8 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").Int8() // Int8() int8 // PointerInt16 returns a pointer for instance of int16 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").PointerInt16() // PointerInt16() *int16 // Int16 returns an instance of int16 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").Int16() // Int16() int16 // PointerInt32 returns a pointer for instance of int32 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").PointerInt32() // PointerInt32() *int32 // Int32 returns an instance of int32 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").Int32() // Int32() int32 // PointerInt64 returns a pointer for instance of int64 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").PointerInt64() // PointerInt64() *int64 // Int64 returns an instance of int64 type. // It panics if field's value can't be casted to desired type. // // number := reader.GetField("SomeField").Int64() // Int64() int64 // PointerUint returns a pointer for instance of uint type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").PointerUint() // PointerUint() *uint // Uint returns an instance of uint type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").Uint() // Uint() uint // PointerUint8 returns a pointer for instance of uint8 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").PointerUint8() // PointerUint8() *uint8 // Uint8 returns an instance of uint8 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").Uint8() // Uint8() uint8 // PointerUint16 returns a pointer for instance of uint16 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").PointerUint16() // PointerUint16() *uint16 // Uint16 returns an instance of uint16 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").Uint16() // Uint16() uint16 // PointerUint32 returns a pointer for instance of uint32 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").PointerUint32() // PointerUint32() *uint32 // Uint32 returns an instance of uint32 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").Uint32() // Uint32() uint32 // PointerUint64 returns a pointer for instance of uint64 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").PointerUint64() // PointerUint64() *uint64 // Uint64 returns an instance of uint64 type. // It panics if field's value can't be casted to desired type. // // unsigned := reader.GetField("SomeField").Uint64() // Uint64() uint64 // PointerFloat32 returns a pointer for instance of float32 type. // It panics if field's value can't be casted to desired type. // // boolean := reader.GetField("SomeField").PointerFloat32() // PointerFloat32() *float32 // Float32 returns an of float32 type. // It panics if field's value can't be casted to desired type. // // boolean := reader.GetField("SomeField").Float32() // Float32() float32 // PointerFloat64 returns a pointer for instance of float64 type. // It panics if field's value can't be casted to desired type. // // boolean := reader.GetField("SomeField").PointerFloat64() // PointerFloat64() *float64 // Float64 returns an instance of float64 type. // It panics if field's value can't be casted to desired type. // // boolean := reader.GetField("SomeField").Float64() // Float64() float64 // PointerString returns a pointer for instance of string type. // It panics if field's value can't be casted to desired type. // // text := reader.GetField("SomeField").PointerString() // PointerString() *string // String returns aan instance of string type. // It panics if field's value can't be casted to desired type. // // text := reader.GetField("SomeField").String() // String() string // PointerBool returns a pointer for instance of bool type. // It panics if field's value can't be casted to desired type. // // boolean := reader.GetField("SomeField").PointerBool() // PointerBool() *bool // Bool returns an instance of bool type. // It panics if field's value can't be casted to desired type. // // boolean := reader.GetField("SomeField").Bool() // Bool() bool // PointerTime returns a pointer for instance of time.Time{} type. // It panics if field's value can't be casted to desired type. // // dateTime := reader.GetField("SomeField").PointerTime() // PointerTime() *time.Time // Time returns an instance of time.Time{} type. // It panics if field's value can't be casted to desired type. // // dateTime := reader.GetField("SomeField").Time() // Time() time.Time // Interface returns an interface which represents field's value. // Useful for casting value into desired type. // // slice, ok := reader.GetField("SomeField").Interface().([]int) // Interface() interface{} }
Field is a wrapper for struct's field's value. It provides methods for easier field's value reading.
type FieldConfig ¶
type FieldConfig interface { // SetType changes field's type. // Expected value is an instance of golang type. // // field.SetType([]int{}) // SetType(typ interface{}) FieldConfig // SetTag changes fields's tag. // Expected value is an string which represents classical // golang tag. // // field.SetTag(`json:"slice"`) // SetTag(tag string) FieldConfig }
FieldConfig holds single field's definition. It provides possibility to edit field's type and tag.
type Reader ¶
type Reader interface { // HasField checks if struct instance has a field with a given name. // // if reader.HasField("SomeFloatField") { ... // HasField(name string) bool // GetField returns struct instance's field value. // If there is no such field, it returns nil. // Usable to edit existing struct's field. // // field := reader.GetField("SomeFloatField") // GetField(name string) Field // GetAllFields returns a list of all struct instance's fields. // // for _, field := range reader.GetAllFields() { ... // GetAllFields() []Field // ToStruct maps all read values to passed instance of struct, by setting // all its values for fields with same names. // It returns an error if argument is not a pointer to a struct. // // err := reader.ToStruct(&instance) // ToStruct(value interface{}) error // ToSliceOfReaders returns a list of Reader interfaces if value is representation // of slice itself. // // readers := reader.ToReaderSlice() // ToSliceOfReaders() []Reader // ToMapReaderOfReaders returns a map of Reader interfaces if value is representation // of map itself with some key type. // // readers := reader.ToReaderMap() // ToMapReaderOfReaders() map[interface{}]Reader // GetValue returns original value used in reader. // // instance := reader.GetValue() // GetValue() interface{} }
Reader is helper interface which provides possibility to access struct's fields' values, reads and provides theirs values.