zreflect

package
v0.12.7 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Struct containerKind = iota
	Map
)

Variables

View Source
var ConvMap = map[reflect.Kind]TypeConv{
	reflect.String: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		v = reflect.ValueOf(val)
		return
	},
	reflect.Bool: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		b, err := strconv.ParseBool(val)
		if err != nil {
			return
		}
		v = reflect.ValueOf(b)
		return
	},
	reflect.Int: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.Atoi(val)
		if err != nil {
			return
		}
		v = reflect.ValueOf(i)
		return
	},
	reflect.Int8: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseInt(val, 10, 8)
		if err != nil {
			return
		}
		v = reflect.ValueOf(int8(i))
		return
	},
	reflect.Int16: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseInt(val, 10, 16)
		if err != nil {
			return
		}
		v = reflect.ValueOf(int16(i))
		return
	},
	reflect.Int32: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseInt(val, 10, 32)
		if err != nil {
			return
		}
		v = reflect.ValueOf(int32(i))
		return
	},
	reflect.Int64: func(val string, field reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		if field.Type() == reflect.TypeOf(time.Second) {
			dur, err := time.ParseDuration(val)
			if err != nil {
				return v, err
			}
			v = reflect.ValueOf(dur)
			return v, err
		}
		i, err := strconv.ParseInt(val, 10, 64)
		if err != nil {
			return
		}
		v = reflect.ValueOf(i)
		return
	},
	reflect.Uint: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseUint(val, 10, 64)
		if err != nil {
			return
		}
		v = reflect.ValueOf(uint(i))
		return
	},
	reflect.Uint8: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseUint(val, 10, 8)
		if err != nil {
			return
		}
		v = reflect.ValueOf(uint8(i))
		return
	},
	reflect.Uint16: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseUint(val, 10, 16)
		if err != nil {
			return
		}
		v = reflect.ValueOf(uint16(i))
		return
	},
	reflect.Uint32: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseUint(val, 10, 32)
		if err != nil {
			return
		}
		v = reflect.ValueOf(uint32(i))
		return
	},
	reflect.Uint64: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseUint(val, 10, 64)
		if err != nil {
			return
		}
		v = reflect.ValueOf(i)
		return
	},
	reflect.Float32: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseFloat(val, 10)
		if err != nil {
			return
		}
		v = reflect.ValueOf(float32(i))
		return
	},
	reflect.Float64: func(val string, _ reflect.Value, _ ConversionMap) (v reflect.Value, err error) {
		i, err := strconv.ParseFloat(val, 10)
		if err != nil {
			return
		}
		v = reflect.ValueOf(i)
		return
	},
	reflect.Slice: func(val string, field reflect.Value, convMap ConversionMap) (res reflect.Value, err error) {
		parts := strings.Split(val, ",")
		elem := field.Type().Elem()
		res = reflect.MakeSlice(reflect.SliceOf(elem), 0, len(parts))
		for _, p := range parts {
			conv, exists := convMap[elem.Kind()]
			if exists {
				val, err := conv(p, field, convMap)
				if err != nil {
					return res, err
				}
				res = reflect.Append(res, val)
			} else {
				err = fmt.Errorf("invalid type of %s found in slice", elem.Kind())
				return
			}
		}
		return
	},
}
View Source
var ErrCantConvertFromString = errors.New("unable to convert string into value")

Functions

func FieldIterator added in v0.12.1

func FieldIterator(v any) *fieldIterator

func FieldNames added in v0.8.12

func FieldNames(val interface{}, tags ...string) (res []string)

Get the names of each field in a struct

func GetValueFromString added in v0.12.3

func GetValueFromString(field reflect.Value, strVal string, opts ...opt) (res reflect.Value, err error)

func SetValue added in v0.12.1

func SetValue(val reflect.Value, toVal any, opts ...opt) (err error)

func SetValueFromString added in v0.12.3

func SetValueFromString(val reflect.Value, toVal string, opts ...opt) (err error)

Parse the given string and use it to set the given value

func WithStructTags added in v0.12.3

func WithStructTags(tags reflect.StructTag) opt

Types

type BoolSet added in v0.12.1

type BoolSet interface {
	Set(bool)
}

type BoolSetErr added in v0.12.1

type BoolSetErr interface {
	Set(bool) (err error)
}

type ConversionMap

type ConversionMap map[reflect.Kind]TypeConv

type FloatSet added in v0.12.1

type FloatSet interface {
	Set(float64)
}

type FloatSetErr added in v0.12.1

type FloatSetErr interface {
	Set(float64) (err error)
}

type IntSet added in v0.12.1

type IntSet interface {
	Set(int)
}

type IntSetErr added in v0.12.1

type IntSetErr interface {
	Set(int) (err error)
}

type StringSet added in v0.12.1

type StringSet interface {
	Set(string)
}

type StringSetErr added in v0.12.1

type StringSetErr interface {
	Set(string) (err error)
}

type TypeConv

type TypeConv func(val string, field reflect.Value, m ConversionMap) (reflect.Value, error)

Jump to

Keyboard shortcuts

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