Documentation
¶
Overview ¶
Package flat provides flatten and unflatten method for map
package flat providers flatten/unflatten nested map or struct(only flatten support struct).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultOption = &Option{
Separator: ".",
}
Functions ¶
func Flatten ¶
func Flatten(src interface{}) map[string]interface{}
Example ¶
package main import ( "fmt" "time" "github.com/sraphs/flat" ) func main() { type ServerHttp struct { Network string Addr string Timeout time.Duration } type ServerGrpc struct { Addr string Timeout time.Duration } type Server struct { Http ServerHttp } type Bootstrap struct { Server Server } c := Bootstrap{ Server: Server{ Http: ServerHttp{ Addr: "0.0.0.0:8000", Timeout: 2 * time.Second, }, }, } opt := flat.Option{ Separator: ".", Case: flat.CaseLower, } flattened := opt.Flatten(c) fmt.Println(flattened["server.http.addr"]) fmt.Println(flattened["server.http.timeout"]) }
Output: 0.0.0.0:8000 2s
func Unflatten ¶
Example ¶
package main import ( "fmt" "github.com/sraphs/flat" ) func main() { m := map[string]interface{}{ "server.http.addr": "0.0.0.0:8000", "server.http.timeout": "2s", } opt := flat.Option{ Separator: ".", Case: flat.CaseLower, } unflattened := opt.Unflatten(m) fmt.Sprintln(unflattened) }
Output:
Types ¶
Click to show internal directories.
Click to hide internal directories.