chanced / labeler

Go struct tags for marshaling and unmarshaling map[string]string
MIT License
12 stars 0 forks source link

Support flattening sub-structs #7

Open livingsilver94 opened 3 years ago

livingsilver94 commented 3 years ago

I'd like to use this library for marshaling and unmarshaling Redis hashes. Since Redis hashes are very similar to map[string]string, they don't support nested values, so it'd be useful to have a flattening mechanism, e.g.:

type MyStruct struct {
    OuterField string `label:"out"`
    InnerField  Inner  `label:"inner"`
}

type Inner struct {
    Field1 string `label:"field1"`
    Field2 string `label:"field2"`
}

Would result in:

{
    "out": "its value",
    "inner.field1": "again, its value",
    "inner.field2": "yeah you guessed it",
}

I chose the hierarchy separator to be a dot, but you could use whatever you want, or even make it customizable. Probably, it's needed to introduce the inline tag, like in the json package.

chanced commented 3 years ago

That's a cool idea. I haven't touched this package in a while, I'd have to look through it to see how easily I could accomplish that. This package has been taking a up a lot of my OSS time, but I'll see if I can't squeeze in some time for this at some point in the near future.

I'll happily take a pull req if you want to take a swing at it.

livingsilver94 commented 3 years ago

I'll give it a try, but no promises ;)

chanced commented 3 years ago

Hopefully the code isn't too big of a mess. It was my first project in Go. :)