Documentation
¶
Index ¶
- Variables
- func Convert(src, dstTyp interface{}, options ...Option) (interface{}, error)
- func ConvertReflectValue(src, dstTyp reflect.Value, options ...Option) (reflect.Value, error)
- func GetHumanName(v interface{}) string
- func MustConvert(src, dstTyp interface{}, options ...Option) interface{}
- func MustConvertReflectValue(src, dstTyp reflect.Value, options ...Option) reflect.Value
- type Converter
- func (conv Converter) Convert(src, dstTyp interface{}, options ...Option) (interface{}, error)
- func (conv Converter) ConvertReflectValue(src, dstTyp reflect.Value, options ...Option) (reflect.Value, error)
- func (conv Converter) MustConvert(src, dstTyp interface{}, options ...Option) interface{}
- func (conv Converter) MustConvertReflectValue(src, dstTyp reflect.Value, options ...Option) reflect.Value
- type Error
- type InvalidTypeError
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Options = struct { SkipUnknownFields func() Option SkipPointers func() Option CustomConverter func(interface{}) Option }{ SkipUnknownFields: func() Option { return skipUnknownFieldsOption{} }, SkipPointers: func() Option { return skipPointersOption{} }, CustomConverter: customConverter, }
Functions ¶
func Convert ¶
Convert converts the specified value to the specified type and returns it. The behavior can be influenced by using the options Example:
str, err := Convert(8, "") if err != nil { panic(err) } fmt.Printf("%s\n", str.(string))
Example ¶
// Convert a map of strings to a struct // this is the struct we want to convert to type User struct { ID int Name string } // this is the data we want to convert data := map[string]string{ "id": "10", "Name": "Joe", } // do the conversion user := MustConvert(data, User{}) // user is now an instance of User fmt.Printf("Hello %s, your ID is %d\n", user.(User).Name, user.(User).ID)
func ConvertReflectValue ¶
func GetHumanName ¶
func GetHumanName(v interface{}) string
GetHumanName returns a friendly human readable name for an type Example:
fmt.Println(GetHumanName(&time.Time{})) prints *time.Time
func MustConvert ¶
func MustConvert(src, dstTyp interface{}, options ...Option) interface{}
MustConvert calls Convert() but panics if there is an error
Types ¶
type Converter ¶
type Converter struct { Options []Option // contains filtered or unexported fields }
Converter is the instance that will be used to convert values
func New ¶
New creates a new converter that can be used multiple times
Example ¶
// Convert a map of strings to a struct type User struct { ID int Name string CreatedOn time.Time } // this is the data we want to convert data := map[string]string{ "id": "10", "Name": "Joe", "createdOn": "2001-01-01T00:00:00Z", } // create a converter converter := New( Options.CustomConverter(func(from string) (time.Time, error) { return time.Parse(time.RFC3339, from) }), ) user := converter.MustConvert(data, User{}) // user is now an instance of User fmt.Printf("%#v\n", user.(User))
func (Converter) Convert ¶
Convert converts the specified value to the specified type and returns it. The behavior can be influenced by using the options Example:
str, err := Convert(8, "") if err != nil { panic(err) } fmt.Printf("%s\n", str.(string))
func (Converter) ConvertReflectValue ¶
func (Converter) MustConvert ¶
MustConvert calls Convert() but panics if there is an error
type InvalidTypeError ¶
type InvalidTypeError struct {
// contains filtered or unexported fields
}
func (*InvalidTypeError) Error ¶
func (e *InvalidTypeError) Error() string
Click to show internal directories.
Click to hide internal directories.