flat

package module
v0.0.0-...-2f04c6e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2022 License: MIT Imports: 5 Imported by: 0

README

flat

flatten/unflatten nested map or struct(only flatten support struct).

Usage

package flat_test

import (
	"fmt"
	"time"

	"github.com/sraphs/go/x/flat"
)

func ExampleFlatten() {
	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 := flat.Flatten(c, opt)

	fmt.Println(flattened["server.http.addr"])
	fmt.Println(flattened["server.http.timeout"])

	// Output:
	// 0.0.0.0:8000
	// 2s
}

func ExampleUnflatten() {
	m := map[string]interface{}{
		"server.http.addr":    "0.0.0.0:8000",
		"server.http.timeout": "2s",
	}

	opt := flat.Option{
		Separator: ".",
		Case:      flat.CaseLower,
	}

	unflattened := flat.Unflatten(m, opt)

	fmt.Sprintln(unflattened)

	// Output:
}

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

This section is empty.

Functions

func Flatten

func Flatten(src interface{}, opt Option) map[string]interface{}
Example
package main

import (
	"fmt"
	"time"

	"github.com/sraphs/go/x/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 := flat.Flatten(c, opt)

	fmt.Println(flattened["server.http.addr"])
	fmt.Println(flattened["server.http.timeout"])

}
Output:
0.0.0.0:8000
2s

func Unflatten

func Unflatten(src map[string]interface{}, opt Option) (dst map[string]interface{})

unflatten

Example
package main

import (
	"fmt"

	"github.com/sraphs/go/x/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 := flat.Unflatten(m, opt)

	fmt.Sprintln(unflattened)

}

Types

type Case

type Case int32
const (
	CaseNone Case = iota
	CaseLower
	CaseUpper
	CaseCamel
	CaseSnake
	CasePascal
)

type Option

type Option struct {
	Case      Case   // "", "lower" or "upper", defaults to "" to no case.
	Separator string // "-" | "_" | "." etc..., defaults to "."
}

func (*Option) GetSeparator

func (o *Option) GetSeparator() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL