Giter Site home page Giter Site logo

kitstack / structkit Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 29 KB

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: MIT License

Go 96.49% Makefile 1.75% Shell 1.75%
simple coverage golang golang-library struct tag copy extractor get go

structkit's Introduction

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.

structkit's People

Contributors

kitstack avatar

Watchers

 avatar

structkit's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.