Bumps the minor-and-patch group with 8 updates in the / directory: | Package | From | To | | --- | --- | --- | | [github.com/gookit/config/v2](https://github.com/gookit/config) | `2.2.8` | `2.2.9` | | [github.com/jellydator/ttlcache/v3](https://github.com/jellydator/ttlcache) | `3.4.0` | `3.4.1` | | [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) | `1.8.21` | `1.8.22` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.14.2` | `2.14.3` | | [github.com/onsi/ginkgo/v2](https://github.com/onsi/ginkgo) | `2.31.0` | `2.32.0` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.42.0` | `1.42.1` | | [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) | `1.17.1` | `1.18.2` | | [google.golang.org/grpc](https://github.com/grpc/grpc-go) | `1.81.1` | `1.82.0` | Updates `github.com/gookit/config/v2` from 2.2.8 to 2.2.9 - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.8...v2.2.9) Updates `github.com/jellydator/ttlcache/v3` from 3.4.0 to 3.4.1 - [Release notes](https://github.com/jellydator/ttlcache/releases) - [Commits](https://github.com/jellydator/ttlcache/compare/v3.4.0...v3.4.1) Updates `github.com/kovidgoyal/imaging` from 1.8.21 to 1.8.22 - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.21...v1.8.22) Updates `github.com/nats-io/nats-server/v2` from 2.14.2 to 2.14.3 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.14.2...v2.14.3) Updates `github.com/onsi/ginkgo/v2` from 2.31.0 to 2.32.0 - [Release notes](https://github.com/onsi/ginkgo/releases) - [Changelog](https://github.com/onsi/ginkgo/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/ginkgo/compare/v2.31.0...v2.32.0) Updates `github.com/onsi/gomega` from 1.42.0 to 1.42.1 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.42.0...v1.42.1) Updates `github.com/open-policy-agent/opa` from 1.17.1 to 1.18.2 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.17.1...v1.18.2) Updates `google.golang.org/grpc` from 1.81.1 to 1.82.0 - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.81.1...v1.82.0) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.9 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/jellydator/ttlcache/v3 dependency-version: 3.4.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.22 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.14.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/onsi/ginkgo/v2 dependency-version: 2.32.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/onsi/gomega dependency-version: 1.42.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.18.2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: google.golang.org/grpc dependency-version: 1.82.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Roman Perekhod <2403905@gmail.com>
Structs
structs Provide some extends util functions for struct. eg: convert, tag parse, struct data init
structs.Aliases- implemented a simple string alias map.- Convert a struct to
map[string]anydata - Quickly init struct default values by field "default" tag.
- Quickly set struct field values by map data
- Parse a struct and collect tags, and parse tag value
- And more util functions ...
Install
go get github.com/gookit/goutil/structs
Go docs
Usage
Convert to map
structs.ToMap() can be quickly convert a struct value to map[string]any
Examples:
type User1 struct {
Name string `json:"name"`
Age int `json:"age"`
city string
}
u1 := &User1{
Name: "inhere",
Age: 34,
city: "somewhere",
}
mp := structs.ToMap(u1)
dump.P(mp)
Output:
map[string]interface {} { #len=2
"age": int(34),
"name": string("inhere"), #len=6
},
Init default values
structs.InitDefaults Quickly init struct default value by field "default" tag.
Examples:
type Extra struct {
City string `default:"chengdu"`
Github string `default:"https://github.com/inhere"`
}
type User struct {
Name string `default:"inhere"`
Age int `default:"30"`
Extra Extra `default:""` // NEED mark tag
}
u := &User{}
_ = structs.InitDefaults(u, &structs.InitOptions{})
dump.P(u)
Output:
&structs_test.User {
Name: string("inhere"), #len=6
Age: int(30),
Extra: structs_test.Extra {
City: string("chengdu"), #len=7
Github: string("https://github.com/inhere"), #len=25
},
},
Set values from map
data := map[string]interface{}{
"name": "inhere",
"age": 234,
"tags": []string{"php", "go"},
"city": "chengdu",
}
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Tags []string `json:"tags"`
City string `json:"city"`
}
u := &User{}
err := structs.SetValues(u, data)
dump.P(u)
Output:
&structs_test.User {
Name: string("inhere"), #len=6
Age: int(234),
Tags: []string [ #len=2
string("php"), #len=3
string("go"), #len=2
],
City: string("chengdu"), #len=7
},
Tags collect and parse
Parse a struct for collect tags, and parse tag value
Examples:
// eg: "desc;required;default;shorts"
type MyCmd struct {
Name string `flag:"set your name;false;INHERE;n"`
}
c := &MyCmd{}
p := structs.NewTagParser("flag")
sepStr := ";"
defines := []string{"desc", "required", "default", "shorts"}
p.ValueFunc = structs.ParseTagValueDefine(sepStr, defines)
goutil.MustOK(p.Parse(c))
dump.P(p.Tags())
Output:
map[string]maputil.SMap { #len=1
"Name": maputil.SMap { #len=1
"flag": string("set your name;false;INHERE;n"), #len=28
},
},
Parse tag value
info, _ := p.Info("Name", "flag")
dump.P(info)
Output:
maputil.SMap { #len=4
"desc": string("set your name"), #len=13
"required": string("false"), #len=5
"default": string("INHERE"), #len=6
"shorts": string("n"), #len=1
},
Functions API
func InitDefaults(ptr any, optFns ...InitOptFunc) error
func MustToMap(st any, optFns ...MapOptFunc) map[string]interface{}
func ParseReflectTags(rt reflect.Type, tagNames []string) (map[string]maputil.SMap, error)
func ParseTagValueDefault(field, tagVal string) (mp maputil.SMap, err error)
func ParseTagValueNamed(field, tagVal string, keys ...string) (mp maputil.SMap, err error)
func ParseTags(st any, tagNames []string) (map[string]maputil.SMap, error)
func SetValues(ptr any, data map[string]any, optFns ...SetOptFunc) error
func StructToMap(st any, optFns ...MapOptFunc) (map[string]interface{}, error)
func ToMap(st any, optFns ...MapOptFunc) map[string]interface{}
func TryToMap(st any, optFns ...MapOptFunc) (map[string]interface{}, error)
type Aliases struct{ ... }
func NewAliases(checker func(alias string)) *Aliases
type Data struct{ ... }
func NewData() *Data
type InitOptFunc func(opt *InitOptions)
type InitOptions struct{ ... }
type LiteData struct{ ... }
type MapOptFunc func(opt *MapOptions)
type MapOptions struct{ ... }
type SMap struct{ ... }
type SetOptFunc func(opt *SetOptions)
type SetOptions struct{ ... }
type TagParser struct{ ... }
func NewTagParser(tagNames ...string) *TagParser
type TagValFunc func(field, tagVal string) (maputil.SMap, error)
func ParseTagValueDefine(sep string, defines []string) TagValFunc
type Value struct{ ... }
func NewValue(val any) *Value
Testings
go test -v ./structs/...
Test limit by regexp:
go test -v -run ^TestSetByKeys ./structs/...