Files
opencloud/vendor/github.com/gookit/goutil/structs/wrapper.go
dependabot[bot] 89a7d171ee build(deps): bump github.com/gookit/config/v2 from 2.2.6 to 2.2.7
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.6 to 2.2.7.
- [Release notes](https://github.com/gookit/config/releases)
- [Commits](https://github.com/gookit/config/compare/v2.2.6...v2.2.7)

---
updated-dependencies:
- dependency-name: github.com/gookit/config/v2
  dependency-version: 2.2.7
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-08-15 14:15:33 +00:00

76 lines
1.6 KiB
Go

package structs
import (
"errors"
"reflect"
)
// Wrapper struct for read or set field value
type Wrapper struct {
// src any // source struct
// raw reflect.Value of source struct
rv reflect.Value
// FieldTagName field name for read/write value. default tag: json
FieldTagName string
// caches for field rv and name and tag name TODO
fieldNames []string //lint:ignore U1000 for unused
fvCacheMap map[string]reflect.Value //lint:ignore U1000 for unused
}
// Wrap quick create a struct wrapper
func Wrap(src any) *Wrapper { return NewWrapper(src) }
// NewWrapper create a struct wrapper
func NewWrapper(src any) *Wrapper {
return WrapValue(reflect.ValueOf(src))
}
// WrapValue create a struct wrapper
func WrapValue(rv reflect.Value) *Wrapper {
rv = reflect.Indirect(rv)
if rv.Kind() != reflect.Struct {
panic("must be provider an struct value")
}
return &Wrapper{rv: rv}
}
// Get field value by name, name allows to use dot syntax.
func (r *Wrapper) Get(name string) any {
val, ok := r.Lookup(name)
if !ok {
return nil
}
return val
}
// Lookup field value by name, name allows to use dot syntax.
func (r *Wrapper) Lookup(name string) (val any, ok bool) {
fv := r.rv.FieldByName(name)
if !fv.IsValid() {
return
}
if fv.CanInterface() {
return fv.Interface(), true
}
return
}
// Set field value by name. name allows using dot syntax.
func (r *Wrapper) Set(name string, val any) error {
fv := r.rv.FieldByName(name)
if !fv.IsValid() {
return errors.New("field " + name + " not found")
}
if !fv.CanSet() {
return errors.New("can not set value for field: " + name)
}
fv.Set(reflect.ValueOf(val))
return nil
}