Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ahnlabcloudmatelabs/gflatten

Golang for map and struct flatten library
https://github.com/ahnlabcloudmatelabs/gflatten

Last synced: about 1 month ago
JSON representation

Golang for map and struct flatten library

Awesome Lists containing this project

README

        

![cloudmate logo](https://avatars.githubusercontent.com/u/69299682?s=200&v=4)

# gflatten

by Cloudmate

---

![Cloudmate](https://img.shields.io/badge/Cloudmate-FFFFFF?style=for-the-badge&logoColor=black&logo=data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pgo8IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDIwMDEwOTA0Ly9FTiIKICJodHRwOi8vd3d3LnczLm9yZy9UUi8yMDAxL1JFQy1TVkctMjAwMTA5MDQvRFREL3N2ZzEwLmR0ZCI+CjxzdmcgdmVyc2lvbj0iMS4wIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiB3aWR0aD0iMjAwLjAwMDAwMHB0IiBoZWlnaHQ9IjIwMC4wMDAwMDBwdCIgdmlld0JveD0iMCAwIDIwMC4wMDAwMDAgMjAwLjAwMDAwMCIKIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaWRZTWlkIG1lZXQiPgoKPGcgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoMC4wMDAwMDAsMjAwLjAwMDAwMCkgc2NhbGUoMC4xMDAwMDAsLTAuMTAwMDAwKSIKZmlsbD0iIzAwMDAwMCIgc3Ryb2tlPSJub25lIj4KPHBhdGggZD0iTTg3NyAxODkwIGMtMTU0IC0yOCAtMzE5IC0xMDQgLTQzNCAtMjAwIC0xNDMgLTEyMCAtMjY4IC0zMzAgLTI5MAotNDg2IC03IC01MSAtMjEgLTM3IDE5NyAtMTg4IDkxIC02MyAyMDUgLTE0MyAyNTUgLTE3OCAyMTIgLTE0OSAyODcgLT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yMSAxNjIgMTMwIDc4IDc2IDE0MCAxMzAgMTUyIDEzMCAxMSAwIDgyIC00NCAxNTkgLTk4IDc3IC01NCAyMDYgLTE0NCAyODYKLTE5OSA4MCAtNTYgMTUxIC0xMDkgMTU5IC0xMTcgMTEgLTE0IDkgLTI3IC0xNCAtODYgLTc2IC0xOTMgLTI0OCAtMzYzIC00NDUKLTQzOSAtMjkxIC0xMTIgLTY0NCAtMTQgLTg1NCAyMzYgLTQ5IDU5IC0xMzAgMjEzIC0xNDIgMjY5IC0xMiA2MCAtODEgODkKLTExMyA0OCAtNDYgLTYzIDc1IC0zMTQgMjIyIC00NTkgMzAxIC0yOTcgNzY0IC0zNDkgMTA5OCAtMTI0IDgzIDU2IDE4OSAxNTkKMjQ0IDIzNiA3OCAxMDcgMTY0IDMxOCAxNDkgMzYzIC03IDIwIC03NDAgNTMwIC03NjIgNTMwIC0xMCAwIC05NiAtNzYgLTE5MQotMTY4eiIvPgo8cGF0aCBkPSJNMTMwOCA5NDIgYy05IC00IC04MiAtNzEgLTE2MiAtMTUwIC0xMzIgLTEzMCAtMTQ2IC0xNDcgLTE0NiAtMTc4IDAKLTQyIDM1IC02NCA3NSAtNDggMzcgMTQgMzE1IDI5MyAzMTUgMzE2IDAgMTkgLTQyIDY5IC01NyA2NyAtNCAwIC0xNiAtMyAtMjUKLTd6Ii8+CjxwYXRoIGQ9Ik0xMjgzIDY5OCBjLTczIC03MyAtMTM2IC0xNDAgLTEzOSAtMTUwIC0xMCAtMzIgMjIgLTY4IDU5IC02OCAyOCAwCjUwIDE4IDE2NSAxMzEgMTA3IDEwNyAxMzIgMTM3IDEzMiAxNjAgMCAzMCAtMzEgNTkgLTY0IDU5IC0xMSAwIC03MyAtNTQgLTE1MwotMTMyeiIvPgo8L2c+Cjwvc3ZnPgo=)
![Golang](https://img.shields.io/badge/Go-00ADD8?style=for-the-badge&logo=go&logoColor=white)

---

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