Skip to content

Commit

Permalink
feat: improve builder/accessor pattern (#90)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
Tronje Krop committed Sep 29, 2024
1 parent 35073b3 commit 91e8b5e
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin

# Setup go-make version to use desired build and config scripts.
GOMAKE_DEP ?= github.com/tkrop/[email protected].103
GOMAKE_DEP ?= github.com/tkrop/[email protected].104
INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto
# Request targets from go-make targets target.
TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.19
0.0.20
99 changes: 69 additions & 30 deletions test/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,105 @@ import (
"unsafe"
)

// Error creates an accessor/build for the given error to access and modify its
// unexported fields by field name.
func Error(err error) *Accessor[error] {
return NewAccessor[error](err)
// Builder is a generic interface that allows you to access and modify
// unexported fields of a (pointer) struct by field name.
type Builder[T any] interface {
// Set sets the value of the field with the given name. If the name is empty,
// and of the same type the stored target instance is replaced by the given
// value.
Set(name string, value any) Builder[T]
// Get returns the value of the field with the given name. If the name is
// empty, the stored target instance is returned.
Get(name string) any
// Build returns the created/modified target instance of the builder.
Build() T
}

// Accessor allows you to access and modify unexported fields of a struct.
type Accessor[T any] struct {
target T
// Builder allows you to access and modify unexported fields of a struct.
type builder[T any] struct {
target any
wrapped bool
}

// NewAccessor creates a generic accessor/builder for a given target struct.
// If the target is a pointer to a struct (template), the instance is stored
// and modified. If the target is a struct, a pointer to a new instance of is
// created, since a struct cannot be modified by reflection.
func NewAccessor[T any](target T) *Accessor[T] {
// NewBuilder creates a generic builder for a target struct type. The builder
// allows you to access and modify unexported fields of the struct by field
// name.
func NewBuilder[T any]() Builder[T] {
var target T
return NewAccessor[T](target)
}

// NewAccessor creates a generic builder/accessor for a given target struct.
// The builder allows you to access and modify unexported fields of the struct
// by field name.
//
// If the target is a pointer to a struct (template), the pointer is stored
// and the instance is modified directly. If the target is a struct, it is
// ignored and a new pointer struct is created for modification, since a struct
// cannot be modified directly by reflection.
func NewAccessor[T any](target T) Builder[T] {
value := reflect.ValueOf(target)
if value.Kind() == reflect.Ptr && value.Elem().Kind() == reflect.Struct {
return &Accessor[T]{
target: value.Interface().(T),

if value.Kind() == reflect.Ptr {
// Create a new instance if the pointer is nil.
if value.Elem().Kind() == reflect.Invalid {
target = reflect.New(value.Type().Elem()).Interface().(T)
value = reflect.ValueOf(target)
}
} else if value.Kind() == reflect.Struct {
target = reflect.New(value.Type()).Interface().(T)

return &Accessor[T]{
target: target,
if value.Elem().Kind() == reflect.Struct {
return &builder[T]{
target: target,
}
}
} else if value.Kind() == reflect.Struct {
// Create a new pointer instance for modification.
value = reflect.New(value.Type())
return &builder[T]{
target: value.Interface(),
wrapped: true,
}
}

panic("target must be a struct or pointer to struct")
}

// Set sets the value of the field with the given name. If the name is empty,
// and of the same type the stored target instance is replaced by the given
// value.
func (a *Accessor[T]) Set(name string, value any) *Accessor[T] {
func (b *builder[T]) Set(name string, value any) Builder[T] {
if name != "" {
field := reflect.ValueOf(a.target).Elem().FieldByName(name)
field := reflect.ValueOf(b.target).Elem().FieldByName(name)
// #nosec G103,G115 // This is a safe use of unsafe.Pointer.
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).
Elem().Set(reflect.ValueOf(value))
} else if reflect.TypeOf(a.target) == reflect.TypeOf(value) {
a.target = value.(T)
} else if reflect.TypeOf(b.target) == reflect.TypeOf(value) {
b.target = value
} else {
panic("target must of compatible struct pointer type")
panic("target must be a compatible struct pointer")
}
return a
return b
}

// Get returns the value of the field with the given name. If the name is
// empty, the stored target instance is returned.
func (a *Accessor[T]) Get(name string) any {
func (b *builder[T]) Get(name string) any {
if name != "" {
field := reflect.ValueOf(a.target).Elem().FieldByName(name)
field := reflect.ValueOf(b.target).Elem().FieldByName(name)
// #nosec G103,G115 // This is a safe use of unsafe.Pointer.
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).
Elem().Interface()
} else if a.wrapped {
return reflect.ValueOf(a.target).Elem().Interface()
} else if b.wrapped {
return reflect.ValueOf(b.target).Elem().Interface()
}
return b.target
}

// Build returns the created/modified target instance of the builder/accessor.
func (b *builder[T]) Build() T {
if b.wrapped {
return reflect.ValueOf(b.target).Elem().Interface().(T)
} else {
return b.target.(T)
}
return a.target
}
Loading

0 comments on commit 91e8b5e

Please sign in to comment.