https://github.com/corpix/flatstructs
Provides helpers to flatten nested structures in go
https://github.com/corpix/flatstructs
go-packages golang structs
Last synced: 10 days ago
JSON representation
Provides helpers to flatten nested structures in go
- Host: GitHub
- URL: https://github.com/corpix/flatstructs
- Owner: corpix
- License: mit
- Created: 2017-02-27T08:35:28.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-21T22:01:21.000Z (almost 8 years ago)
- Last Synced: 2025-08-15T04:42:23.046Z (7 months ago)
- Topics: go-packages, golang, structs
- Language: Go
- Size: 147 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
flatstructs
-----------
[](https://travis-ci.org/corpix/flatstructs)
Provides helpers to flatten nested structures in go.
## Example
``` go
package main
import (
"fmt"
"github.com/corpix/flatstructs"
)
type Scope struct {
Request
Connection
}
type Request struct {
Headers
}
type Headers struct {
UserAgent string
Referer string
}
type Connection struct {
Host string
Port int
}
type Event struct {
ID int
Source string
Scope
}
func main() {
event := &Event{
ID: 1,
Source: "system",
Scope: Scope{
Request: Request{
Headers: Headers{
UserAgent: "curl",
Referer: "http://example.com",
},
},
Connection: Connection{
Host: "127.0.0.1",
Port: 1337,
},
},
}
keys, err := flatstructs.Keys(event)
if err != nil {
panic(err)
}
values, err := flatstructs.Values(event)
if err != nil {
panic(err)
}
fmt.Println("flatstructs.Keys(event) -> ", keys)
fmt.Println("flatstructs.Values(event) -> ", values)
}
```
Now save this code into `snippet.go` and:
``` shell
go run ./snippet.go
flatstructs.Keys(event) -> [ID Source ScopeRequestHeadersUserAgent ScopeRequestHeadersReferer ScopeConnectionHost ScopeConnectionPort]
flatstructs.Values(event) -> [1 system curl http://example.com 127.0.0.1 1337]
```
## Limitations
> Some of them are not limitations actually but it is worth to mention them here.
* If getting `Values()` and somewhere in the chain there is a slice of `struct`'s it will be returned untouched
* Map's is also returned untouched when getting `Values()` and `Keys()` is not going inside map's
## Customization
### Builder
There is things you could customize:
* Key delimiter
* Key name
* Key tag name
This customization's are available via custom `Builder`:
``` go
builder := flatstructs.NewBuilder("key", ".")
builder.Keys(...)
builder.Values(...)
builder.Map(...)
```