https://github.com/gontainer/reflectpro
A simple and elegant interface for GO's reflect.
https://github.com/gontainer/reflectpro
go golang reflect reflection reflection-api reflection-library
Last synced: 4 months ago
JSON representation
A simple and elegant interface for GO's reflect.
- Host: GitHub
- URL: https://github.com/gontainer/reflectpro
- Owner: gontainer
- License: mit
- Created: 2024-03-02T22:35:01.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-28T19:40:20.000Z (almost 2 years ago)
- Last Synced: 2024-06-21T14:22:58.180Z (over 1 year ago)
- Topics: go, golang, reflect, reflection, reflection-api, reflection-library
- Language: Go
- Homepage:
- Size: 75.2 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-with-stars - reflectpro - 10-01 | (Reflection / HTTP Clients)
- awesome-go - reflectpro - Callers, copiers, getters and setters for go. (Reflection / HTTP Clients)
- fucking-awesome-go - reflectpro - Callers, copiers, getters and setters for go. (Reflection / HTTP Clients)
- awesome-go-cn - reflectpro
- awesome-go - reflectpro - Callers, copiers, getters and setters for go. (Reflection / HTTP Clients)
README
[](https://pkg.go.dev/github.com/gontainer/reflectpro)
[](https://github.com/gontainer/reflectpro/actions/workflows/tests.yml)
[](https://coveralls.io/github/gontainer/reflectpro?branch=main)
[](https://goreportcard.com/report/github.com/gontainer/reflectpro)
[](https://sonarcloud.io/summary/new_code?id=gontainer_reflectpro)
# Reflectpro
Simple, elegant, and intuitive [callers](caller), [copiers](copier), [getters](getter) and [setters](setter).
## Examples
## Caller
In the following example, we have a pointer to `any` that stores a `struct`,
instead of having a direct pointer to a `struct`.
The receiver is a pointer, so eventually we cannot call the given method.
`Caller` handles that by creating a pointer to a copy of that value.
```go
type Person struct {
name string
age int
}
func (p *Person) SetName(n string) {
p.name = n
}
func Example() {
var p any
p := &Person{age: 25}
_, _ = caller.CallMethod(p, "SetName", []any{"Mary"}, false)
fmt.Printf("%+v\n", p)
// Output: &{name:Mary age:25}
}
```
## Copier
```go
var (
from = []any{int(1), uint(2), float32(3), float64(4)}
to []uint64
)
_ = copier.Copy(from, &to, true) // convert
fmt.Printf("%#v\n", to)
// Output: []uint64{0x1, 0x2, 0x3, 0x4}
```
## Getter
In the following example, we read an unexported field of the given struct.
```go
person := struct {
name string
}{
name: "Mary",
}
v, _ := getter.Get(person, "name")
fmt.Println(v)
// Output: Mary
```
## Setter
In the following example, we have a pointer to `any` that stores a `struct`,
instead of having a direct pointer to a `struct`. Since it is an `unaddressable value`,
the `reflect` package from the standard library does not allow assigning a new value to this field.
`Setter` handles that by creating an addressable copy.
```go
var person any
person = struct {
name string
}{}
_ = setter.Set(&person, "name", "Mary", false)
fmt.Println(person)
// Output: {Mary}
```