rflutil

package module
v0.0.0-...-693b7b7 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 5 Imported by: 1

README

Go Version GoDoc Build Status Coverage Status GoReport

Reflection utility functions for Go

Installation

go get github.com/tiendc/go-rflutil

Usage

Slice functions
SliceLen
slice := []int{1, 2, 3}
v, err := SliceLen(reflect.ValueOf(slice)) // v == 3
SliceGet
slice := []int{1, 2, 3}
v, err := SliceGet[int](reflect.ValueOf(slice), 1)     // v == 2
v, err := SliceGet[int](reflect.ValueOf(slice), 3)     // err is ErrIndexOutOfRange
v, err := SliceGet[string](reflect.ValueOf(slice), 1)  // err is ErrTypeUnmatched
SliceSet
slice := []int{1, 2, 3}
err := SliceSet(reflect.ValueOf(slice), 1, 22)    // slice[1] == 22
err := SliceSet(reflect.ValueOf(slice), 3, 44)    // err is ErrIndexOutOfRange
err := SliceSet(reflect.ValueOf(slice), 1, "22")  // err is ErrTypeUnmatched
SliceAppend
slice := []int{1, 2, 3}
slice2, err := SliceAppend(reflect.ValueOf(slice), 4)   // slice2 == []int{1, 2, 3, 4}
slice2, err := SliceAppend(reflect.ValueOf(slice), "4") // err is ErrTypeUnmatched
SliceGetAll
slice := []int{1, 2, 3}
s, err := SliceGetAll(reflect.ValueOf(slice)) // returns []reflect.Value
SliceAs
slice := []any{1, 2, 3}
s, err := SliceAs[int64](reflect.ValueOf(slice)) // s == []int64{1,2,3}
Map functions
MapLen
aMap := map[int]string{1: "11", 2: "22", 3: "33"}
v, err := MapLen(reflect.ValueOf(aMap), 1) // v == 3
MapGet
aMap := map[int]string{1: "11", 2: "22", 3: "33"}
v, err := MapGet[string](reflect.ValueOf(aMap), 1) // v == "11"
v, err := MapGet[int](reflect.ValueOf(aMap), 3)    // err is ErrTypeUnmatched
MapSet
aMap := map[int]string{1: "11", 2: "22", 3: "33"}
err := MapSet(reflect.ValueOf(aMap), 1, "111") // success
err := MapSet(reflect.ValueOf(aMap), 4, "444") // success
err := MapSet(reflect.ValueOf(aMap), 5, 555)   // err is ErrTypeUnmatched
MapDelete
aMap := map[int]string{1: "11", 2: "22", 3: "33"}
err := MapDelete(reflect.ValueOf(aMap), 1) // success
err := MapDelete(reflect.ValueOf(aMap), 4) // success
MapKeys
aMap := map[int]string{1: "11", 2: "22", 3: "33"}
keys, err := MapKeys(reflect.ValueOf(aMap)) // returns a slice []reflect.Value of keys
MapEntries
aMap := map[int]string{1: "11", 2: "22", 3: "33"}
entries, err := MapEntries(reflect.ValueOf(aMap)) // returns a slice []MapEntry of entries
Struct functions
StructGetField
type S struct {
    I int
    S string
}
s := S{I: 1, S: "11"}
v, err := StructGetField[int](reflect.ValueOf(s), "I", true)     // v == 1
v, err := StructGetField[string](reflect.ValueOf(s), "S", true)  // v == "11"
v, err := StructGetField[string](reflect.ValueOf(s), "s", false) // v == "11"
v, err := StructGetField[string](reflect.ValueOf(s), "s", true)  // err is ErrNotFound
StructSetField
type S struct {
    I int
    S string
}
s := S{I: 1, S: "11"}
err := StructSetField[int](reflect.ValueOf(&s), "I", 11, true)        // success
err := StructSetField[string](reflect.ValueOf(&s), "S", "111", true)  // success
err := StructSetField[string](reflect.ValueOf(&s), "s", "111", false) // success
err := StructSetField[string](reflect.ValueOf(&s), "s", "111", true)  // err is ErrNotFound
StructListFields
type S struct {
    I int    `mytag:"ii"`
    S string `mytag:"ss"`
}
s := S{I: 1, S: "11"}
fields, err := StructListFields(reflect.ValueOf(&s), false, "")      // fields == []string{"I", "S"}
fields, err := StructListFields(reflect.ValueOf(&s), false, "mytag") // fields == []string{"ii", "ss"}
StructToMap / StructToMapEx
type S struct {
    I  int    `json:"ii"`
    S  string `json:"ss"`
    i8 int8
}
s := S{I: 1, S: "11"}
v, err := StructToMap(reflect.ValueOf(s), false, nil) // v == map[string]any{"I": 1, "S": "11"}

// Pass arg parseJSONTag with "true"
v, err := StructToMap(reflect.ValueOf(s), true, nil)  // v == map[string]any{"ii": 1, "ss": "11"}

// Use StructToMapEx in case you want to parse custom tag
v, err := StructToMapEx(reflect.ValueOf(s), "json", nil)  // v == map[string]any{"ii": 1, "ss": "11"}

// Pass arg keyFunc to extract unexported fields
v, err := StructToMap(reflect.ValueOf(s), false, func(name string, isExported bool) string {
	return strings.ToLower(name)
})  // v == map[string]any{"i": 1, "s": "11", "i8": 0}
ParseTag / ParseTagOf / ParseTagsOf
type S struct {
    I int    `mytag:"i,optional,k=v"`
    S string `mytag:"s,optional,k1=v1,k2=v2,omitempty"`
    U uint   `mytag:"-,optional"`
}

s := S{I: 1, S: "11", U: 10}
sVal := reflect.ValueOf(s)
iField, _ := sVal.Type().FieldByName("I")
tag, err := ParseTag(&iField, "mytag", ",") // tag.Name == i
                                            // tag.Attrs == map[string]string{"optional": "", "k", "v"}
Common functions
ValueAs
v, err := ValueAs[float32](reflect.ValueOf(97)) // v == float32(97)
v, err := ValueAs[string](reflect.ValueOf(97))  // v == "a"

Contributing

  • You are welcome to make pull requests for new functions and bug fixes.

Authors

License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeInvalid        = errors.New("ErrTypeInvalid")
	ErrTypeUnmatched      = errors.New("ErrTypeUnmatched")
	ErrNotFound           = errors.New("ErrNotFound")
	ErrValueUnaddressable = errors.New("ErrValueUnaddressable")
	ErrValueUnsettable    = errors.New("ErrValueUnsettable")
	ErrIndexOutOfRange    = errors.New("ErrIndexOutOfRange")
)

Functions

func MapDelete

func MapDelete[K comparable](m reflect.Value, k K) error

MapDelete delete the given key from a map

func MapGet

func MapGet[V any, K comparable](m reflect.Value, k K) (V, error)

MapGet get value from a map by key

func MapKeys

func MapKeys(m reflect.Value) ([]reflect.Value, error)

MapKeys get all keys of a map

func MapLen

func MapLen(m reflect.Value) (int, error)

MapLen get number of entries in a map

func MapSet

func MapSet[K comparable, V any](m reflect.Value, k K, v V) error

MapSet set value for a key of a map

func SliceAppend

func SliceAppend[T any](s reflect.Value, v T) ([]T, error)

SliceAppend appends the given value to a slice

func SliceAs

func SliceAs[T any](s reflect.Value) ([]T, error)

SliceAs convert all elements of a slice to the target type

func SliceGet

func SliceGet[T any](s reflect.Value, i int) (T, error)

SliceGet get value from a slice type at the given index

func SliceGetAll

func SliceGetAll(s reflect.Value) ([]reflect.Value, error)

SliceGetAll get all elements of a slice

func SliceLen

func SliceLen(s reflect.Value) (int, error)

SliceLen get number of elements of a slice

func SliceSet

func SliceSet[T any](s reflect.Value, i int, v T) error

SliceSet set value in a slice type at the given index

func StructGetField

func StructGetField[T any](v reflect.Value, name string, caseSensitive bool) (T, error)

StructGetField get struct field value by field name as T type Input should be a struct, a ptr to a struct, or an interface containing a struct

func StructListFields

func StructListFields(
	v reflect.Value,
	listUnexportedFields bool,
	customTag string,
) ([]string, error)

func StructSetField

func StructSetField[T any](v reflect.Value, name string, value T, caseSensitive bool) error

StructSetField set struct field value by field name as T type

func StructToMap

func StructToMap(
	v reflect.Value,
	parseJSONTag bool,
	keyFunc func(name string, isExported bool) string,
) (map[string]any, error)

StructToMap convert a struct to a map Pass the keyFunc as nil to default to use field name and ignore unexported fields.

func StructToMapEx

func StructToMapEx(
	v reflect.Value,
	customTag string,
	keyFunc func(name string, isExported bool) string,
) (map[string]any, error)

StructToMapEx convert a struct to a map Pass the keyFunc as nil to default to use field name and ignore unexported fields. nolint: gocognit

func ValueAs

func ValueAs[T any](v reflect.Value) (T, error)

Types

type MapEntry

type MapEntry struct {
	Key   reflect.Value
	Value reflect.Value
}

func MapEntries

func MapEntries(m reflect.Value) ([]MapEntry, error)

MapEntries get all entries of a map

type Tag

type Tag struct {
	Name      string
	FieldName string
	Ignored   bool // when name is "-"
	Attrs     map[string]string
}

func ParseTag

func ParseTag(field *reflect.StructField, tagName, delim string) (*Tag, error)

ParseTag parse tag for the given struct field

func ParseTagOf

func ParseTagOf(v reflect.Value, fieldName, tagName, delim string) (*Tag, error)

ParseTagOf parse tag for the struct and field name

func ParseTagsOf

func ParseTagsOf(v reflect.Value, tagName, delim string) ([]*Tag, error)

ParseTagsOf parse tags of all struct fields

func (*Tag) GetAttr

func (tag *Tag) GetAttr(key string) (string, bool)

func (*Tag) GetAttrDefault

func (tag *Tag) GetAttrDefault(key string, defVal string) string

func (*Tag) HasAttr

func (tag *Tag) HasAttr(key string) bool

Jump to

Keyboard shortcuts

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