https://github.com/ahnlabcloudmatelabs/gflatten
Golang for map and struct flatten library
https://github.com/ahnlabcloudmatelabs/gflatten
Last synced: 6 months ago
JSON representation
Golang for map and struct flatten library
- Host: GitHub
- URL: https://github.com/ahnlabcloudmatelabs/gflatten
- Owner: ahnlabcloudmatelabs
- License: mit
- Created: 2023-07-24T03:50:04.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-25T08:58:57.000Z (almost 3 years ago)
- Last Synced: 2025-04-01T11:11:18.675Z (about 1 year ago)
- Language: Go
- Size: 4.88 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# gflatten
by Cloudmate
---


---
## About
Golang for map and struct flatten library
## Install
```sh
go get -u github.com/cloudmatelabs/gflatten
```
## Usage
### Insert Map
```go
import "github.com/cloudmatelabs/gflatten"
src := map[string]any{
"foo": []any{
"bar", "baz",
},
"foobar": map[string]any{
"foo": []any{
"baz",
map[string]any{
"bar": map[string]any{
"baz": "foobar",
},
},
},
},
}
option := gflatten.Option{
ParameterDelimiter: ".",
ArrayWrap: gflatten.WRAP.SQUARE_BRACKET,
}
/* postgres style
option := gflatten.Option{
ParameterDelimiter: "->",
ArrayDelimiter: "->",
ParameterWrap: gflatten.WRAP.SINGLE_QUOTE,
}
*/
/* mysql style
option := gflatten.Option{
Prefix: "$",
ParameterDelimiter: ".",
ArrayWrap: gflatten.WRAP.SQUARE_BRACKET,
}
*/
dest, err := gflatten.Flatten(src, option)
/*
dest = map[string]any{
"foo[0]": "bar",
"foo[1]": "baz",
"foobar.foo[0]": "baz",
"foobar.foo[1].bar.baz": "foobar",
}
*/
```
### Insert Struct
```go
import "github.com/cloudmatelabs/gflatten"
type bar struct {
Baz string `json:"baz"`
}
type foobar struct {
Foo []bar `json:"foo"`
}
type input struct {
Foo []string `json:"foo"`
Foobar foobar `json:"foobar"`
}
src := input{
Foo: []string{"bar", "baz"},
Foobar: foobar{
Foo: []bar{
{Baz: "baz"},
{Baz: "foobar"},
},
},
}
option := gflatten.Option{
ParameterDelimiter: ".",
ArrayWrap: gflatten.WRAP.SQUARE_BRACKET,
}
dest, err := gflatten.Flatten(src, option)
/*
dest = map[string]any{
"foo[0]": "bar",
"foo[1]": "baz",
"foobar.foo[0].baz": "baz",
"foobar.foo[1].baz": "foobar",
}
*/
```
## Option
- Prefix(optional)
- ParameterDelimiter(optional)
- ArrayDelimiter(optional)
- ParameterWrap(optional) -> gflatten.WRAP
- ArrayWrap(optional) -> gflatten.WRAP
### gflatten.WRAP
- SQUARE_BRACKET: []
- SINGLE_QUOTE: ''
- DOUBLE_QUOTE: ""
- BACKTICK: ``
- NONE