structkit

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2023 License: MIT Imports: 6 Imported by: 0

README

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/lab210-dev/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/lab210-dev/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/lab210-dev/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/lab210-dev/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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy added in v1.0.3

func Copy(source any, fields ...string) any

func Get added in v1.0.3

func Get(source any, field string, opt ...*Option) any

func Set added in v1.0.4

func Set(source any, field string, value any) (err error)

Types

type Option added in v1.0.3

type Option struct {
	Delimiter string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL