gocast

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2022 License: MIT Imports: 10 Imported by: 30

README

GoCast

GoDoc Build Status Go Report Card Coverage Status

Easily convert basic types into any other basic types.

What is GoCast?

Cast is a library to convert between different GO types in a consistent and easy way.

The library provides a set of methods with the universal casting of types. All casting methods starts from To{TargetType}.

Usage example

import "github.com/demdxx/gocast"

// Example ToString:
gocast.ToString("strasstr")         // "strasstr"
gocast.ToString(8)                  // "8"
gocast.ToString(8.31)               // "8.31"
gocast.ToString([]byte("one time")) // "one time"
gocast.ToString(nil)                // ""

var foo interface{} = "one more time"
gocast.ToString(foo)                // "one more time"

// Example ToInt:
gocast.ToInt(8)                  // 8
gocast.ToInt(8.31)               // 8
gocast.ToInt("8")                // 8
gocast.ToInt(true)               // 1
gocast.ToInt(false)              // 0

var eight interface{} = 8
gocast.ToInt(eight)              // 8
gocast.ToInt(nil)                // 0

gocast.ToFloat32("2.12")         // 2.12
gocast.ToFloat64("2.")           // 2.0
func sumAll(vals ...interface{}) int {
  var result int = 0
  for _, v := range vals {
    result += gocast.ToInt(v)
  }
  return result
}
Structures
type User struct {
  ID    uint64
  Email string
}

var user User

// Set structure values
err := gocast.SetStructFieldValue(&user, "ID", uint64(19))
err := gocast.SetStructFieldValue(&user, "Email", "iamawesome@mail.com")

id, err := gocast.StructFieldValue(user, "ID")
email, err := gocast.StructFieldValue(user, "Email")
fmt.Printf("User: %d - %s", id, email)
// > User: 19 - iamawesome@mail.com

Benchmarks

> go test -benchmem -v -race -bench=.

BenchmarkToBool
BenchmarkToBool-8               34045201                44.23 ns/op            0 B/op          0 allocs/op
BenchmarkToBoolByReflect
BenchmarkToBoolByReflect-8      19063656                62.12 ns/op            0 B/op          0 allocs/op
BenchmarkToFloat
BenchmarkToFloat-8              17534598                69.61 ns/op            2 B/op          0 allocs/op
BenchmarkToInt
BenchmarkToInt-8                17316328                68.66 ns/op            2 B/op          0 allocs/op
BenchmarkToUint
BenchmarkToUint-8               17812291                66.71 ns/op            2 B/op          0 allocs/op
BenchmarkToStringByReflect
BenchmarkToStringByReflect-8      485696              2108 ns/op               6 B/op          0 allocs/op
BenchmarkToString
BenchmarkToString-8               532069              1950 ns/op               6 B/op          0 allocs/op
BenchmarkParseTime
BenchmarkParseTime-8              695787              1671 ns/op             464 B/op          5 allocs/op
BenchmarkIsEmpty
BenchmarkIsEmpty-8              24910284                46.36 ns/op            0 B/op          0 allocs/op

License

The MIT License (MIT)

Copyright (c) 2014 Dmitry Ponomarev demdxx@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStructFieldNameUndefined      = errors.New("struct field name undefined")
	ErrStructFieldValueCantBeChanged = errors.New("struct field value cant be changed")
)

Structure method errors

View Source
var (
	ErrUnsettableValue = errors.New("can't set value")
)

Error list...

Functions

func IsEmpty

func IsEmpty(v interface{}) bool

IsEmpty value

func IsEmptyByReflection added in v1.0.2

func IsEmptyByReflection(v reflect.Value) bool

IsEmptyByReflection value

func ParseTime

func ParseTime(tm string) (t time.Time, err error)

ParseTime from string

func SetStructFieldValue added in v1.1.0

func SetStructFieldValue(st interface{}, name string, value interface{}) error

SetStructFieldValue puts value into the struct field

func StructFieldTags

func StructFieldTags(st interface{}, tag string) map[string]string

StructFieldTags returns Map with key->tag matching

func StructFieldTagsUnsorted

func StructFieldTagsUnsorted(st interface{}, tag string) ([]string, []string)

StructFieldTagsUnsorted returns field names and tag targets separately

func StructFieldValue added in v1.1.0

func StructFieldValue(st interface{}, name string) (interface{}, error)

StructFieldValue returns the value of the struct field

func StructFields

func StructFields(st interface{}, tag string) []string

StructFields returns the field names from the structure

func To

func To(v, to interface{}, tags string) (interface{}, error)

To cast any input type into the target

func ToBool

func ToBool(v interface{}) bool

ToBool from any other basic types

func ToBoolByReflect

func ToBoolByReflect(v reflect.Value) bool

ToBoolByReflect returns boolean from reflection

func ToFloat

func ToFloat(v interface{}) float64

ToFloat from any other basic types

func ToFloat32

func ToFloat32(v interface{}) float32

ToFloat32 from any other basic types

func ToFloat64

func ToFloat64(v interface{}) float64

ToFloat64 from any other basic types

func ToFloat64ByReflect

func ToFloat64ByReflect(v reflect.Value) float64

ToFloat64ByReflect returns float64 from reflection

func ToFloat64Slice

func ToFloat64Slice(v interface{}) []float64

ToFloat64Slice converts any input slice into Float64 type slice

func ToInt

func ToInt(v interface{}) int

ToInt from any other basic types

func ToInt16 added in v1.0.2

func ToInt16(v interface{}) int16

ToInt16 from any other basic types

func ToInt32

func ToInt32(v interface{}) int32

ToInt32 from any other basic types

func ToInt64

func ToInt64(v interface{}) int64

ToInt64 from any other basic types

func ToInt64ByReflect

func ToInt64ByReflect(v reflect.Value) int64

ToInt64ByReflect returns int64 from reflection

func ToIntSlice

func ToIntSlice(v interface{}) []int

ToIntSlice converts any input slice into Int type slice

func ToInterfaceSlice

func ToInterfaceSlice(v interface{}) []interface{}

ToInterfaceSlice converts any input slice into Interface type slice

func ToMap

func ToMap(dst, src interface{}, tag string, recursive bool) error

ToMap cast your Source into the Destination type tag defines the tags name in the structure to map the keys

func ToMapFrom

func ToMapFrom(src interface{}, tag string, recursive bool) (map[interface{}]interface{}, error)

ToMapFrom any Map/Object type

func ToSiMap

func ToSiMap(src interface{}, tag string, recursive bool) (map[string]interface{}, error)

ToSiMap converts input Map/Object type into the map[string]interface{}

func ToSlice

func ToSlice(dst, src interface{}, tags string) error

ToSlice converts any input slice into destination type slice

func ToString

func ToString(v interface{}) string

ToString from any type

func ToStringByReflect

func ToStringByReflect(v reflect.Value) string

ToStringByReflect converts reflection value to string

func ToStringMap

func ToStringMap(src interface{}, tag string, recursive bool) (map[string]string, error)

ToStringMap converts input Map/Object type into the map[string]string

func ToStringSlice

func ToStringSlice(v interface{}) []string

ToStringSlice converts any input slice into String type slice

func ToStruct

func ToStruct(dst, src interface{}, tag string) (err error)

ToStruct convert any input type into the target structure

func ToT

func ToT(v interface{}, t reflect.Type, tags string) (interface{}, error)

ToT cast any input type into the target reflection

func ToType

func ToType(v reflect.Value, t reflect.Type, tags string) (interface{}, error)

ToType from reflection value to reflection type

func ToUint

func ToUint(v interface{}) uint

ToUint from any other basic types

func ToUint16 added in v1.0.2

func ToUint16(v interface{}) uint32

ToUint32 from any other basic types

func ToUint32

func ToUint32(v interface{}) uint32

ToUint32 from any other basic types

func ToUint64

func ToUint64(v interface{}) uint64

ToUint64 from any other basic types

func ToUint64ByReflect

func ToUint64ByReflect(v reflect.Value) uint64

ToUint64ByReflect returns uint64 from reflection

Types

This section is empty.

Jump to

Keyboard shortcuts

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