Skip to content

StructKit is a simple tool that allows you to copy specific fields from a new struct and retrieve values from a struct or slice

License

Notifications You must be signed in to change notification settings

kitstack/structkit

Repository files navigation

Go GitHub go.mod Go version Go Report Card codecov License Github tag

🚀 Overview

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)

Copy

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}}
}

Get

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
}

Set

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}]
}

💪 Benchmark

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

🤝 Contributions

Contributors to the package are encouraged to help improve the code.

About

StructKit is a simple tool that allows you to copy specific fields from a new struct and retrieve values from a struct or slice

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published