StructKit is simple tool for :
- Copy specific fields a new struct.
- Tag copy
- Get value of specific field.
- Slice
- Struct
- Pointer
- Set value of specific field.
- Slice (Append, Replace or Update)
- Struct
- Pointer
- Coverage
- 100% tested code ๐ค
- Benchmark
- Get (Optimized with cache)
package main
import (
"github.com/kitstack/structkit"
"log"
)
type Foo struct {
Counter int `json:"int"`
Value string `json:"value"`
Struct Bar `json:"struct"`
Slice []Bar `json:"Slice"`
}
type Bar struct {
Value string `json:"value"`
}
func main() {
payload := Foo{Value: "foo", Struct: Bar{Value: "bar"}}
log.Printf("%v", structkit.Copy(payload, "Value", "Struct.Value")) // {foo {bar}}
}
package main
import (
"github.com/kitstack/structkit"
"log"
)
type Foo struct {
Counter int `json:"int"`
Value string `json:"value"`
Struct Bar `json:"struct"`
Slice []Bar `json:"Slice"`
}
type Bar struct {
Value string `json:"value"`
}
func main() {
payload := Foo{Value: "foo", Struct: Bar{Value: "bar"}, Slice: []Bar{{Value: "baz"}}}
log.Printf("%v", structkit.Get(payload, "Value")) // foo
log.Printf("%v", structkit.Get(payload, "Struct/Value", &structkit.Option{Delimiter: "/"})) // bar
log.Printf("%v", structkit.Get(payload, "Slice.[0].Value")) // baz
}
package main
import (
"github.com/kitstack/structkit"
"log"
)
type Foo struct {
Value string
Struct Bar
StructP *Bar
Slice []Bar
}
type Bar struct {
Value string
}
func main() {
payload := Foo{}
err := structkit.Set(&payload, "Value", "foo")
if err != nil {
panic(err)
}
log.Print(payload.Value) // foo
err = structkit.Set(&payload, "Struct.Value", "bar")
if err != nil {
panic(err)
}
log.Print(payload.Struct.Value) // bar
err = structkit.Set(&payload, "StructP.Value", "bar")
if err != nil {
panic(err)
}
log.Print(payload.StructP.Value) // bar
err = structkit.Set(&payload, "Slice.[*]", Bar{Value: "bar"})
if err != nil {
panic(err)
}
log.Print(payload.Slice) // [{bar}]
err = structkit.Set(&payload, "Slice.[0].Value", "bar updated")
if err != nil {
panic(err)
}
log.Print(payload.Slice) // [{bar updated}]
}
goos: darwin
goarch: arm64
pkg: github.com/kitstack/structkit
BenchmarkGet
BenchmarkGet-10 6346312 194.0 ns/op
BenchmarkGetEmbeddedValue
BenchmarkGetEmbeddedValue-10 5543814 209.5 ns/op
BenchmarkGetEmbeddedSliceValue
BenchmarkGetEmbeddedSliceValue-10 4526838 262.4 ns/op
Contributors to the package are encouraged to help improve the code.