Documentation ¶
Index ¶
- Constants
- type Finder
- func (f *Finder) Error() string
- func (f *Finder) Find(names ...string) *Finder
- func (f *Finder) FindTop(names ...string) *Finder
- func (f *Finder) FromKeys(fks *FinderKeys) *Finder
- func (f *Finder) GetNameSeparator() string
- func (f *Finder) HasError() bool
- func (f *Finder) Into(names ...string) *Finder
- func (f *Finder) Reset() *Finder
- func (f *Finder) ToMap() (map[string]interface{}, error)
- type FinderKeys
- type Getter
- func (g *Getter) Bool(name string) (bool, bool)
- func (g *Getter) Byte(name string) (byte, bool)
- func (g *Getter) Bytes(name string) ([]byte, bool)
- func (g *Getter) Complex128(name string) (complex128, bool)
- func (g *Getter) Complex64(name string) (complex64, bool)
- func (g *Getter) Float32(name string) (float32, bool)
- func (g *Getter) Float64(name string) (float64, bool)
- func (g *Getter) Get(name string) (interface{}, bool)
- func (g *Getter) GetGetter(name string) (*Getter, bool)
- func (g *Getter) GetType(name string) (reflect.Type, bool)
- func (g *Getter) GetValue(name string) (reflect.Value, bool)
- func (g *Getter) Has(name string) bool
- func (g *Getter) Int(name string) (int, bool)
- func (g *Getter) Int16(name string) (int16, bool)
- func (g *Getter) Int32(name string) (int32, bool)
- func (g *Getter) Int64(name string) (int64, bool)
- func (g *Getter) Int8(name string) (int8, bool)
- func (g *Getter) IsArray(name string) bool
- func (g *Getter) IsBool(name string) bool
- func (g *Getter) IsByte(name string) bool
- func (g *Getter) IsBytes(name string) bool
- func (g *Getter) IsChan(name string) bool
- func (g *Getter) IsComplex128(name string) bool
- func (g *Getter) IsComplex64(name string) bool
- func (g *Getter) IsFloat32(name string) bool
- func (g *Getter) IsFloat64(name string) bool
- func (g *Getter) IsFunc(name string) bool
- func (g *Getter) IsInt(name string) bool
- func (g *Getter) IsInt16(name string) bool
- func (g *Getter) IsInt32(name string) bool
- func (g *Getter) IsInt64(name string) bool
- func (g *Getter) IsInt8(name string) bool
- func (g *Getter) IsMap(name string) bool
- func (g *Getter) IsSlice(name string) bool
- func (g *Getter) IsString(name string) bool
- func (g *Getter) IsStruct(name string) bool
- func (g *Getter) IsUint(name string) bool
- func (g *Getter) IsUint16(name string) bool
- func (g *Getter) IsUint32(name string) bool
- func (g *Getter) IsUint64(name string) bool
- func (g *Getter) IsUint8(name string) bool
- func (g *Getter) IsUintptr(name string) bool
- func (g *Getter) IsUnsafePointer(name string) bool
- func (g *Getter) MapGet(name string, f func(int, *Getter) (interface{}, error)) ([]interface{}, error)
- func (g *Getter) Names() []string
- func (g *Getter) NumField() int
- func (g *Getter) Slice(name string) ([]interface{}, bool)
- func (g *Getter) String(name string) (string, bool)
- func (g *Getter) ToMap() map[string]interface{}
- func (g *Getter) Uint(name string) (uint, bool)
- func (g *Getter) Uint16(name string) (uint16, bool)
- func (g *Getter) Uint32(name string) (uint32, bool)
- func (g *Getter) Uint64(name string) (uint64, bool)
- func (g *Getter) Uint8(name string) (uint8, bool)
- func (g *Getter) Uintptr(name string) (uintptr, bool)
- func (g *Getter) UnsafePointer(name string) (unsafe.Pointer, bool)
Examples ¶
Constants ¶
const VERSION = "0.9.1"
VERSION says my version number
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Finder ¶
type Finder struct {
// contains filtered or unexported fields
}
Finder is the struct that builds the nested struct finder. All methods are NOT goroutine safe yet (TODO:)
Example ¶
type Group struct { Name string Boss string } type Company struct { Name string Address string Period int *Group } type School struct { Name string GraduatedYear int } type Person struct { Name string Age int *Company *School } i := &Person{ Name: "Joe Davis", Age: 45, Company: &Company{ Name: "XXX Cars inc.", Address: "New York", Period: 20, Group: &Group{ Name: "YYY Group Holdings", Boss: "Donald", }, }, School: &School{ Name: "ABC College", GraduatedYear: 1995, }, } // 2nd argument is the separator string for nested field names separating finder, err := NewFinderWithSep(i, ">") // Note: // If "NewFinder(i)" is called instead of "NewFinderWithSep", default separator "." is automatically used. // finder, err := NewFinder(i) if err != nil { panic(err) } // Finder provides method chain mechanism m, err := finder. // Find(...string) returns a Finder that fields in NESTED struct are looked up by field name arguments. Find("School"). // Into(...string) returns a Finder that NESTED struct fields are looked up by field name arguments. // This example looks up `person.Company.Address` field. Into("Company").Find("Address"). // If multi arguments are assigned for Into method, then execute multi level nesting. // This example looks up `person.Company.Group.Name` and `person.Company.Group.Boss` fields. Into("Company", "Group").Find("Name", "Boss"). // ToMap converts from found struct fields to map. ToMap() if err != nil { panic(err) } fmt.Printf("%#v", m)
Output: map[string]interface {}{"Company>Address":"New York", "Company>Group>Boss":"Donald", "Company>Group>Name":"YYY Group Holdings", "School":structil.School{Name:"ABC College", GraduatedYear:1995}}
func NewFinder ¶
NewFinder returns a concrete Finder that uses and obtains from i. i must be a struct or struct pointer.
func NewFinderWithGetter ¶
NewFinderWithGetter returns a concrete Finder that uses and obtains from g. g must be a Getter
func NewFinderWithGetterAndSep ¶
NewFinderWithGetterAndSep returns a concrete Finder that uses and obtains from g using the separator string. g must be a Getter
func NewFinderWithSep ¶
NewFinderWithSep returns a concrete Finder that uses and obtains from i using the separator string. i must be a struct or struct pointer.
func (*Finder) Find ¶
Find returns a Finder that fields in struct are looked up and held named names.
func (*Finder) FindTop ¶ added in v0.2.0
FindTop returns a Finder that top level fields in struct are looked up and held named names. Deprecated: planning to remove this method.
func (*Finder) FromKeys ¶ added in v0.2.0
func (f *Finder) FromKeys(fks *FinderKeys) *Finder
FromKeys returns a Finder that looked up by FinderKeys generated from configuration file.
Example (Json) ¶
type Group struct { Name string Boss string } type Company struct { Name string Address string Period int *Group } type School struct { Name string GraduatedYear int } type Person struct { Name string Age int *Company *School } i := &Person{ Name: "Joe Davis", Age: 45, Company: &Company{ Name: "XXX Cars inc.", Address: "New York", Period: 20, Group: &Group{ Name: "YYY Group Holdings", Boss: "Donald", }, }, School: &School{ Name: "ABC College", GraduatedYear: 1995, }, } // testdata/finder_from_conf/ex_json.json as follows: // // { // "Keys":[ // { // "Company":[ // { // "Group":[ // "Name", // "Boss" // ] // }, // "Address", // "Period" // ] // }, // "Name", // "Age" // ] // } fks, err := NewFinderKeys("testdata/finder_from_conf", "ex_json") if err != nil { panic(err) } finder, err := NewFinder(i) if err != nil { panic(err) } m, err := finder.FromKeys(fks).ToMap() if err != nil { panic(err) } fmt.Printf("%#v", m)
Output: map[string]interface {}{"Age":45, "Company.Address":"New York", "Company.Group.Boss":"Donald", "Company.Group.Name":"YYY Group Holdings", "Company.Period":20, "Name":"Joe Davis"}
Example (Yml) ¶
type Group struct { Name string Boss string } type Company struct { Name string Address string Period int *Group } type School struct { Name string GraduatedYear int } type Person struct { Name string Age int *Company *School } i := &Person{ Name: "Joe Davis", Age: 45, Company: &Company{ Name: "XXX Cars inc.", Address: "New York", Period: 20, Group: &Group{ Name: "YYY Group Holdings", Boss: "Donald", }, }, School: &School{ Name: "ABC College", GraduatedYear: 1995, }, } // testdata/finder_from_conf/ex_yml.yml as follows: // // Keys: // - Company: // - Group: // - Name // - Boss // - Address // - Period // - Name // - Age // Get `FinderKeys` object by calling `NewFinderKeys` with config file dir and baseName // This config file path is "testdata/finder_from_conf/ex_json.json" fks, err := NewFinderKeys("testdata/finder_from_conf", "ex_yml") if err != nil { panic(err) } finder, err := NewFinder(i) if err != nil { panic(err) } // And build `Finder` object using `FromKeys` method with `FinderKeys` object // This returns the same result as follows: // // finder = finder.Find("Name", "Age"). // Into("Company").Find("Address", "Period"). // Into("Company", "Group").Find("Name", "Boss") m, err := finder.FromKeys(fks).ToMap() if err != nil { panic(err) } fmt.Printf("%#v", m)
Output: map[string]interface {}{"Age":45, "Company.Address":"New York", "Company.Group.Boss":"Donald", "Company.Group.Name":"YYY Group Holdings", "Company.Period":20, "Name":"Joe Davis"}
func (*Finder) GetNameSeparator ¶
GetNameSeparator returns the separator string for nested struct name separating. Default is "." (dot).
type FinderKeys ¶ added in v0.2.0
type FinderKeys struct {
// contains filtered or unexported fields
}
FinderKeys is the struct that have keys for Finder.
func NewFinderKeys ¶ added in v0.3.1
func NewFinderKeys(dir string, baseName string) (*FinderKeys, error)
NewFinderKeys returns a FinderKeys object that is created from configuration file indicated by dir and name file.
func (*FinderKeys) Len ¶ added in v0.2.0
func (fks *FinderKeys) Len() int
Len returns length of FinderKeys
type Getter ¶
type Getter struct {
// contains filtered or unexported fields
}
Getter is the struct that wraps the basic Getter method.
Example ¶
type Company struct { Name string Address string Period int } type Person struct { Name string Age int *Company } i := &Person{ Name: "Tony", Age: 25, Company: &Company{ Name: "Tiger inc.", Address: "Tokyo", Period: 3, }, } // i must be a struct or struct pointer getter, err := NewGetter(i) if err != nil { panic(err) } name, _ := getter.String("Name") // get as string age, _ := getter.Int("Age") // get as int company, _ := getter.Get("Company") // get as interface{} fmt.Printf( "num of fields=%d\nfield names=%v\n'Name'=%s\n'Age'=%d\n'Company'=%+v", getter.NumField(), // get num of fields getter.Names(), // get field names name, age, company, )
Output: num of fields=3 field names=[Name Age Company] 'Name'=Tony 'Age'=25 'Company'={Name:Tiger inc. Address:Tokyo Period:3}
func NewGetter ¶
NewGetter returns a concrete Getter that uses and obtains from i. i must be a struct or struct pointer.
func (*Getter) Bool ¶
Bool returns the byte of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not bool.
func (*Getter) Byte ¶
Byte returns the byte of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not byte.
func (*Getter) Bytes ¶
Bytes returns the []byte of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not []byte.
func (*Getter) Complex128 ¶ added in v0.4.0
func (g *Getter) Complex128(name string) (complex128, bool)
Complex128 returns the complex128 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not complex128.
func (*Getter) Complex64 ¶ added in v0.4.0
Complex64 returns the complex64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not complex64.
func (*Getter) Float32 ¶ added in v0.4.0
Float32 returns the float32 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not float32.
func (*Getter) Float64 ¶
Float64 returns the float64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not float64.
func (*Getter) Get ¶
Get returns the interface of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field.
func (*Getter) GetGetter ¶ added in v0.8.0
GetGetter returns the Getter of interface of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not struct or struct pointer.
func (*Getter) GetType ¶
GetType returns the reflect.Type object of the original struct field named "name". 2nd return value will be false if the original struct does not have a "name" field.
func (*Getter) GetValue ¶
GetValue returns the reflect.Value object of the original struct field named "name". 2nd return value will be false if the original struct does not have a "name" field.
func (*Getter) Int ¶
Int returns the int of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int.
func (*Getter) Int16 ¶ added in v0.4.0
Int16 returns the int16 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int16.
func (*Getter) Int32 ¶ added in v0.4.0
Int32 returns the int32 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int32.
func (*Getter) Int64 ¶
Int64 returns the int64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int64.
func (*Getter) Int8 ¶ added in v0.4.0
Int8 returns the int8 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int8.
func (*Getter) IsArray ¶ added in v0.4.0
IsArray reports whether type of the original struct field named name is slice.
func (*Getter) IsBool ¶
IsBool reports whether type of the original struct field named name is bool.
func (*Getter) IsByte ¶
IsByte reports whether type of the original struct field named name is byte.
func (*Getter) IsBytes ¶
IsBytes reports whether type of the original struct field named name is []byte.
func (*Getter) IsChan ¶
IsChan reports whether type of the original struct field named name is chan.
func (*Getter) IsComplex128 ¶ added in v0.4.0
IsComplex128 reports whether type of the original struct field named name is []byte.
func (*Getter) IsComplex64 ¶ added in v0.4.0
IsComplex64 reports whether type of the original struct field named name is []byte.
func (*Getter) IsFloat32 ¶ added in v0.4.0
IsFloat32 reports whether type of the original struct field named name is float32.
func (*Getter) IsFloat64 ¶
IsFloat64 reports whether type of the original struct field named name is float64.
func (*Getter) IsFunc ¶
IsFunc reports whether type of the original struct field named name is func.
func (*Getter) IsInt16 ¶ added in v0.4.0
IsInt16 reports whether type of the original struct field named name is int16.
func (*Getter) IsInt32 ¶ added in v0.4.0
IsInt32 reports whether type of the original struct field named name is int32.
func (*Getter) IsInt64 ¶
IsInt64 reports whether type of the original struct field named name is int64.
func (*Getter) IsInt8 ¶ added in v0.4.0
IsInt8 reports whether type of the original struct field named name is int8.
func (*Getter) IsSlice ¶
IsSlice reports whether type of the original struct field named name is slice.
func (*Getter) IsString ¶
IsString reports whether type of the original struct field named name is string.
func (*Getter) IsStruct ¶
IsStruct reports whether type of the original struct field named name is struct.
func (*Getter) IsUint ¶
IsUint reports whether type of the original struct field named name is uint.
func (*Getter) IsUint16 ¶ added in v0.4.0
IsUint16 reports whether type of the original struct field named name is uint16.
func (*Getter) IsUint32 ¶ added in v0.4.0
IsUint32 reports whether type of the original struct field named name is uint32.
func (*Getter) IsUint64 ¶
IsUint64 reports whether type of the original struct field named name is uint64.
func (*Getter) IsUint8 ¶ added in v0.4.0
IsUint8 reports whether type of the original struct field named name is uint8.
func (*Getter) IsUintptr ¶ added in v0.4.0
IsUintptr reports whether type of the original struct field named name is uintptr.
func (*Getter) IsUnsafePointer ¶ added in v0.4.0
IsUnsafePointer reports whether type of the original struct field named name is []byte.
func (*Getter) MapGet ¶
func (g *Getter) MapGet(name string, f func(int, *Getter) (interface{}, error)) ([]interface{}, error)
MapGet returns the interface slice of mapped values of the original struct field named name.
Example ¶
type Company struct { Name string Address string Period int } type Person struct { Name string Age int Companies []*Company } i := &Person{ Name: "Tony", Age: 25, Companies: []*Company{ { Name: "Tiger inc.", Address: "Tokyo", Period: 3, }, { Name: "Dragon inc.", Address: "Osaka", Period: 4, }, }, } getter, err := NewGetter(i) if err != nil { panic(err) } // Each of "Companies" field are applied map function as follows. fn := func(i int, g *Getter) (interface{}, error) { period, _ := g.Int("Period") name, _ := g.String("Name") return fmt.Sprintf( "You worked for %d years since you joined the company %s", period, name, ), nil } // 1st argeument must be a field name of array or slice field. // function assigned 2nd argument is applied each "Companies" element. intfs, err := getter.MapGet("Companies", fn) if err != nil { panic(err) } fmt.Printf("%#v", intfs)
Output: []interface {}{"You worked for 3 years since you joined the company Tiger inc.", "You worked for 4 years since you joined the company Dragon inc."}
func (*Getter) Slice ¶ added in v0.8.0
Slice returns the slice of interface of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not slice of interface.
func (*Getter) String ¶
String returns the string of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not string.
func (*Getter) Uint ¶
Uint returns the uint of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint.
func (*Getter) Uint16 ¶ added in v0.4.0
Uint16 returns the uint16 of the original struct field named name.Getter 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint16.
func (*Getter) Uint32 ¶ added in v0.4.0
Uint32 returns the uint32 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint32.
func (*Getter) Uint64 ¶
Uint64 returns the uint64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint64.
func (*Getter) Uint8 ¶ added in v0.4.0
Uint8 returns the uint8 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint8.
func (*Getter) Uintptr ¶ added in v0.4.0
Uintptr returns the uintptr of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uintptr.
func (*Getter) UnsafePointer ¶ added in v0.4.0
UnsafePointer returns the unsafe.Pointer of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not unsafe.Pointer.