package
1.7.1
Repository: https://github.com/sinlov-go/go-common-lib.git
Documentation: pkg.go.dev

# README

A simple and user-friendly reflection utility library.

This library's API is often considered low-level and unintuitive, making simple tasks like setting structure field values more complex than necessary.

The main features supported are:

  • Setting the values of structure fields, supporting nested structure field values by using paths such as A.B.C.

  • Getting the values, types, tags, etc., of structure fields.

  • Traversing all fields of a structure, supporting both select mode and range mode. If a deep traversal method like FieldsDeep is used, it will traverse all nested structures.

  • Function calls and method calls, supporting variadic parameters.

  • Creating new instances, checking interface implementations, and more.

Quick Start

Set nested struct field value

person := &Person{
	Name: "John",
	Age:  20,
	Country: Country{
		ID:   0,
		Name: "Perk",
	},
}

_ = SetEmbedField(person, "Country.ID", 1)

// Perk's ID: 1 
fmt.Printf("Perk's ID: %d \n", person.Country.ID)

Find json tag

type Person struct {
	Name string `json:"name" xml:"_name"`
}
p := &Person{}
// json:"name" xml:"_name"
fmt.Println(StructFieldTag(p, "Name"))
// name <nil>
fmt.Println(StructFieldTagValue(p, "Name", "json"))
// _name <nil>
fmt.Println(StructFieldTagValue(p, "Name", "xml"))

Filter instance fields (deep traversal)

type Person struct {
	id   string
	Age  int    `json:"int"`
	Name string `json:"name"`
	Home struct {
		Address string `json:"address"`
	}
}

p := &Person{}
fields, _ := SelectFieldsDeep(p, func(s string, field reflect.StructField, value reflect.Value) bool {
	return field.Tag.Get("json") != ""
})
// key: Age type: int
// key: Name type: string
// key: Home.Address type: string
for k, v := range fields {
	fmt.Printf("key: %s type: %v\n", k, v.Type())
}

Call a function

var addFunc = func(nums ...int) int {
		var sum int
		for _, num := range nums {
			sum += num
		}
		return sum
}

res, _ := CallFunc(addFunc, 1, 2, 3)

// 6
fmt.Println(res[0].Interface())

# Functions

AnonymousStructFields returns the slice of reflect.StructField of all anonymous fields in obj.
CallFunc invokes a function using reflection and returns the result.
CallFuncSlice has the same functionality as CallFunc, it must have variadic parameters, but it uses the CallSlice method of reflect.Value under the hood.
CallMethod calls the method `method` of the `obj` object and returns the result, supporting variadic parameters.
CallMethodSlice has the same functionality as CallMethod, and it uses CallFuncSlice as its underlying implementation.
EmbedField returns the reflect.Value of a field in the nested structure of obj based on the specified fieldPath.
EmbedFieldKind returns the reflect.Kind of a field in the nested structure of obj based on the specified fieldPath.
EmbedFieldType returns the reflect.Type of a field in the nested structure of obj based on the specified fieldPath.
EmbedFieldTypeStr returns the string of the reflect.Type of a field in the nested structure of obj based on the specified fieldPath.
EmbedFieldValue returns the actual value of a field in the nested structure of obj based on the specified fieldPath.
EmbedStructField returns the reflect.Value of a field in the nested structure of obj based on the specified fieldPath.
EmbedStructFieldKind returns the reflect.Kind of a field in the nested structure of obj based on the specified fieldPath.
EmbedStructFieldType returns the reflect.Type of a field in the nested structure of obj based on the specified fieldPath.
EmbedStructFieldTypeStr returns the string of the reflect.Type of a field in the nested structure of obj based on the specified fieldPath.
Field returns the reflect.Value of the provided obj field.
FieldKind returns the reflect.Kind of the provided obj field.
Fields returns a map of reflect.Value containing all the fields of the obj, with the field names as keys.
FieldsDeep traverses the obj deeply, including all nested structures, and returns all fields as reflect.Value in the form of a map, where the key is the path of the field.
FieldType returns the reflect.Type of the provided obj field.
FieldTypeStr returns the string of reflect.Type of the provided obj field.
FieldValue returns the actual value of the provided obj field.
GetPkgPath returns the package path of obj.
HasStructField checks if the provided obj struct has field named fieldName.
Implements returns whether obj implements the given interface in.
NewInstance returns a new instance of the same type as the input value.
RangeFields iterates over all fields of obj and calls function f on each field.
RangeFieldsDeep performs a deep traversal of obj and its nested structures, and calls function f on each field.
RangeStructFields iterates over all struct fields of obj and calls function f on each field.
SelectFields has the same functionality as Fields, but only the fields for which the function f returns true will be returned.
SelectFieldsDeep has the same functionality as FieldsDeep, but only the fields for which the function f returns true will be returned.
SelectStructFields has the same functionality as StructFields, but only the fields for which the function f returns true will be returned.
SetEmbedField sets a nested struct field using fieldPath.
SetField sets the fieldName field of the obj object according to the fieldValue parameter.
SetPrivateField is similar to SetField, but it allows you to set private fields of an object.
StructField returns the reflect.StructField of the provided obj field.
StructFieldKind returns the reflect.Kind of the provided obj field.
StructFields returns a slice of reflect.StructField containing all the fields of the obj.
StructFieldsFlatten returns "flattened" struct fields.
StructFieldTag returns the reflect.StructTag of the provided obj field.
StructFieldTagValue returns the provided obj field tag value.
StructFieldType returns the reflect.Type of the provided obj field.
StructFieldTypeStr returns the string of reflect.Type of the provided obj field.
Type returns the reflection type of obj.
TypePenetrateElem performs the same functionality as Type, but it will parse through all pointers until the final type is reached.
Value returns the reflection value of obj.
ValuePenetrateElem performs the same functionality as Value, but it will parse through all pointers until the final type is reached.