coerce

package module
v0.0.0-...-254d9a2 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2016 License: GPL-3.0 Imports: 7 Imported by: 4

README

coerce

Golang typecasting for the lazy.

Includes coercion of map[string]interface{} values into named fields.

Installation

go get github.com/SeeSpotRun/coerce

API

See godoc

Example

package main
import (
	"fmt"
	"github.com/SeeSpotRun/coerce"
)

func main() {

	// cast to a single var:
	var i integer
	s := "1234"
	coerce.Var(&i, s) //      i == int(1234)


	// return a typecast var:
	f := coerce.Float32(s) // f == float32(1234.0)


	// coerce struct fields from a map[string]interface{}:
	type x struct{
		intslice  []int
		boolval   bool
		s         string
	}
	var myx x

	mymap := map[string]interface{} {
		"intslice":  []string {"5", "12", "0.5k"},
		"boolval" :  true,
		"s"       :  "hello",
	}

	err := coerce.Struct(&myx, mymap)
	fmt.Println(err, myx) // gives: <nil> {[5 12 512] true hello}

}

Contributing

Feel welcome to contribute via Issues and Pull Requests

Go Report Card

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float32

func Float32(from interface{}) (f float32, e error)

Float32 tries to return a float32 value based on content of 'from'

func Float64

func Float64(from interface{}) (f float64, e error)

Float64 tries to return a float64 value based on content of 'from'

func Int

func Int(from interface{}) (i int, e error)

Int tries to return an int value based on content of 'from'

func Int64

func Int64(from interface{}) (i int64, e error)

Int64 tries to return an int64 value based on content of 'from'

func String

func String(from interface{}) (s string)

String is equivalent to fmt.Sprint(from)

func Struct

func Struct(to interface{}, from map[string]interface{}, formats ...string) error

Struct attempts to unmarshall the values in 'from' into the fields in the structure pointed to by 'target'. The field names are used as map keys. Optional format strings can be used to morph the field names into keys, eg "--%s" will map field "foo" to key "--foo". If more than one format is supplied, these will be tried in order until the first matching key is found. When coercing from string to any integer types, if the string ends with B|K|M|G|T (case-insensitive) then these will be interpreted as multipliers of 1, 1024, etc.

Example:

type x struct{
	intslice  []int
	boolval   bool
	s         string
}

mymap := map[string]interface{} {
	"--intslice":  []string {"5", "12", "0.5k"},
	"--boolval" :  true,
	"-s"        :  "hello",
}

var myx x

err := coerce.Struct(&myx, mymap, "--%s", "-%s")
fmt.Println(err, myx) // <nil> {[5 12 512] true hello}

Note: coercing unexported fields uses 'unsafe' pointers

func Uint

func Uint(from interface{}) (u uint, e error)

Uint tries to return a uint value based on content of 'from'

func Uint64

func Uint64(from interface{}) (u int64, e error)

Uint64 tries to return a uint64 value based on content of 'from'

func Var

func Var(pto interface{}, from interface{}) error

Var attempts to cast the content of 'from' into the variable pointed to by 'pto'

Types

This section is empty.

Jump to

Keyboard shortcuts

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