convert

package module
v0.0.0-...-27e81fb Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 6 Imported by: 0

README

go-convert

Convert a value into another type.

go get -u github.com/Eun/go-convert

Usage

// convert a int to a string
fmt.Printf("%v\n", convert.MustConvert(1, ""))


// convert a map into a struct
type User struct {
	ID int
	Name string
}
fmt.Printf("%#v\n", convert.MustConvert(
	map[string]string{
	    "Name": "Joe",
	    "ID": "10",
	},
	User{}))

// convert a map into well defined map
fmt.Printf("%v\n", MustConvert(
	map[string]interface{}{
		"Id":        "1",
		"Name":      "Joe",
		"Groups":    []string{"3", "6"},
		"Country":   "US",
 	},
    // convert Id to int and Groups to []int and keep the rest
	map[string]interface{}{
        "Id":      0,
		"Groups":  []int{},
 	}, 
))

// convert a interface slice into well defined interface slice
// making the first one an integer, the second a string and the third an float
fmt.Printf("%v\n", MustConvert([]string{"1", "2", "3"}, []interface{}{0, "", 0.0}))
Notice

This library is using reflection so be aware it might be slow in your usecase.

Documentation

Index

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

func Convert(src, dstTyp interface{}, options ...Option) (interface{}, error)

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 ConvertReflectValue(src, dstTyp reflect.Value, options ...Option) (reflect.Value, error)

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

func MustConvertReflectValue

func MustConvertReflectValue(src, dstTyp reflect.Value, options ...Option) reflect.Value

MustConvertReflectValue calls MustConvertReflectValue() 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

func New(options ...Option) *Converter

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

func (conv Converter) Convert(src, dstTyp interface{}, options ...Option) (interface{}, error)

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 (conv Converter) ConvertReflectValue(src, dstTyp reflect.Value, options ...Option) (reflect.Value, error)

func (Converter) MustConvert

func (conv Converter) MustConvert(src, dstTyp interface{}, options ...Option) interface{}

MustConvert calls Convert() but panics if there is an error

func (Converter) MustConvertReflectValue

func (conv Converter) MustConvertReflectValue(src, dstTyp reflect.Value, options ...Option) reflect.Value

type Error

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

func (*Error) Error

func (e *Error) Error() string

type InvalidTypeError

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

func (*InvalidTypeError) Error

func (e *InvalidTypeError) Error() string

type Option

type Option interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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