Bumps the minor-and-patch group with 8 updates in the / directory: | Package | From | To | | --- | --- | --- | | [github.com/gookit/config/v2](https://github.com/gookit/config) | `2.2.8` | `2.2.9` | | [github.com/jellydator/ttlcache/v3](https://github.com/jellydator/ttlcache) | `3.4.0` | `3.4.1` | | [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) | `1.8.21` | `1.8.22` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.14.2` | `2.14.3` | | [github.com/onsi/ginkgo/v2](https://github.com/onsi/ginkgo) | `2.31.0` | `2.32.0` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.42.0` | `1.42.1` | | [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) | `1.17.1` | `1.18.2` | | [google.golang.org/grpc](https://github.com/grpc/grpc-go) | `1.81.1` | `1.82.0` | Updates `github.com/gookit/config/v2` from 2.2.8 to 2.2.9 - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.8...v2.2.9) Updates `github.com/jellydator/ttlcache/v3` from 3.4.0 to 3.4.1 - [Release notes](https://github.com/jellydator/ttlcache/releases) - [Commits](https://github.com/jellydator/ttlcache/compare/v3.4.0...v3.4.1) Updates `github.com/kovidgoyal/imaging` from 1.8.21 to 1.8.22 - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.21...v1.8.22) Updates `github.com/nats-io/nats-server/v2` from 2.14.2 to 2.14.3 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.14.2...v2.14.3) Updates `github.com/onsi/ginkgo/v2` from 2.31.0 to 2.32.0 - [Release notes](https://github.com/onsi/ginkgo/releases) - [Changelog](https://github.com/onsi/ginkgo/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/ginkgo/compare/v2.31.0...v2.32.0) Updates `github.com/onsi/gomega` from 1.42.0 to 1.42.1 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.42.0...v1.42.1) Updates `github.com/open-policy-agent/opa` from 1.17.1 to 1.18.2 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.17.1...v1.18.2) Updates `google.golang.org/grpc` from 1.81.1 to 1.82.0 - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.81.1...v1.82.0) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.9 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/jellydator/ttlcache/v3 dependency-version: 3.4.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.22 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.14.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/onsi/ginkgo/v2 dependency-version: 2.32.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/onsi/gomega dependency-version: 1.42.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.18.2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: google.golang.org/grpc dependency-version: 1.82.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Roman Perekhod <2403905@gmail.com>
172 lines
4.8 KiB
Go
172 lines
4.8 KiB
Go
package reflects
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strconv"
|
|
"unsafe"
|
|
)
|
|
|
|
// indirectInterface returns the concrete value in an interface value,
|
|
// or else the zero reflect.Value.
|
|
// That is, if v represents the interface value x, the result is the same as reflect.ValueOf(x):
|
|
// the fact that x was an interface value is forgotten.
|
|
func indirectInterface(v reflect.Value) reflect.Value {
|
|
if v.Kind() != reflect.Interface {
|
|
return v
|
|
}
|
|
if v.IsNil() {
|
|
return emptyValue
|
|
}
|
|
return v.Elem()
|
|
}
|
|
|
|
// Elem returns the value that the interface v contains
|
|
// or that the pointer v points to. otherwise, will return self
|
|
func Elem(v reflect.Value) reflect.Value {
|
|
if v.Kind() == reflect.Pointer || v.Kind() == reflect.Interface {
|
|
return v.Elem()
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Indirect like reflect.Indirect(), but can also indirect reflect.Interface. otherwise, will return self
|
|
func Indirect(v reflect.Value) reflect.Value {
|
|
if v.Kind() == reflect.Pointer || v.Kind() == reflect.Interface {
|
|
return v.Elem()
|
|
}
|
|
return v
|
|
}
|
|
|
|
// UnwrapAny unwrap reflect.Interface value. otherwise, will return self
|
|
func UnwrapAny(v reflect.Value) reflect.Value {
|
|
if v.Kind() == reflect.Interface {
|
|
return v.Elem()
|
|
}
|
|
|
|
if v.IsNil() {
|
|
return emptyValue
|
|
}
|
|
return v
|
|
}
|
|
|
|
// TypeReal returns a ptr type's real type. otherwise, will return self.
|
|
func TypeReal(t reflect.Type) reflect.Type {
|
|
if t.Kind() == reflect.Pointer {
|
|
return t.Elem()
|
|
}
|
|
return t
|
|
}
|
|
|
|
// TypeElem returns the array, slice, chan, map type's element type. otherwise, will return self.
|
|
func TypeElem(t reflect.Type) reflect.Type {
|
|
switch t.Kind() {
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
return t.Elem()
|
|
default:
|
|
return t
|
|
}
|
|
}
|
|
|
|
// Len get reflect value length. allow: intX, uintX, floatX, string, map, array, chan, slice.
|
|
//
|
|
// Note: (u)intX use width. float to string then calc len.
|
|
func Len(v reflect.Value) int {
|
|
v = reflect.Indirect(v)
|
|
|
|
// (u)int use width.
|
|
switch v.Kind() {
|
|
case reflect.String:
|
|
return len([]rune(v.String()))
|
|
case reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
|
|
return v.Len()
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
return len(strconv.FormatInt(int64(v.Uint()), 10))
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return len(strconv.FormatInt(v.Int(), 10))
|
|
case reflect.Float32, reflect.Float64:
|
|
return len(fmt.Sprint(v.Interface()))
|
|
default:
|
|
return -1 // cannot get length
|
|
}
|
|
}
|
|
|
|
// SliceSubKind get sub-elem kind of the array, slice, variadic-var. alias SliceElemKind()
|
|
func SliceSubKind(typ reflect.Type) reflect.Kind {
|
|
return SliceElemKind(typ)
|
|
}
|
|
|
|
// SliceElemKind get sub-elem kind of the array, slice, variadic-var.
|
|
//
|
|
// Usage:
|
|
//
|
|
// SliceElemKind(reflect.TypeOf([]string{"abc"})) // reflect.String
|
|
func SliceElemKind(typ reflect.Type) reflect.Kind {
|
|
if typ.Kind() == reflect.Slice || typ.Kind() == reflect.Array {
|
|
return typ.Elem().Kind()
|
|
}
|
|
return reflect.Invalid
|
|
}
|
|
|
|
// UnexportedValue quickly get unexported value by reflect.Value
|
|
//
|
|
// NOTE: this method is unsafe, use it carefully.
|
|
// should ensure rv is addressable by field.CanAddr()
|
|
//
|
|
// refer: https://stackoverflow.com/questions/42664837/how-to-access-unexported-struct-fields
|
|
func UnexportedValue(rv reflect.Value) any {
|
|
if rv.CanAddr() {
|
|
// create new value from addr, now can be read and set.
|
|
return reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Interface()
|
|
}
|
|
|
|
// If the rv is not addressable this trick won't work, but you can create an addressable copy like this
|
|
rs2 := reflect.New(rv.Type()).Elem()
|
|
rs2.Set(rv)
|
|
rv = rs2.Field(0)
|
|
rv = reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem()
|
|
// Now rv can be read. TIP: Setting will succeed but only affects the temporary copy.
|
|
return rv.Interface()
|
|
}
|
|
|
|
// SetUnexportedValue quickly set unexported field value by reflect
|
|
//
|
|
// NOTE: this method is unsafe, use it carefully.
|
|
// should ensure rv is addressable by field.CanAddr()
|
|
func SetUnexportedValue(rv reflect.Value, value any) {
|
|
reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Set(reflect.ValueOf(value))
|
|
}
|
|
|
|
// SetValue to a `reflect.Value`. will auto convert type if needed.
|
|
func SetValue(rv reflect.Value, val any) error {
|
|
// get a real type of the ptr value
|
|
if rv.Kind() == reflect.Ptr {
|
|
if rv.IsNil() {
|
|
elemTyp := rv.Type().Elem()
|
|
rv.Set(reflect.New(elemTyp))
|
|
}
|
|
|
|
// use elem for set value
|
|
rv = reflect.Indirect(rv)
|
|
}
|
|
|
|
rv1, err := ValueByType(val, rv.Type())
|
|
if err == nil {
|
|
rv.Set(rv1)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// SetRValue to a `reflect.Value`. will direct set value without a type convert.
|
|
func SetRValue(rv, val reflect.Value) {
|
|
if rv.Kind() == reflect.Ptr {
|
|
if rv.IsNil() {
|
|
elemTyp := rv.Type().Elem()
|
|
rv.Set(reflect.New(elemTyp))
|
|
}
|
|
rv = reflect.Indirect(rv)
|
|
}
|
|
|
|
rv.Set(val)
|
|
}
|