flat

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: MIT Imports: 5 Imported by: 2

README

go-starter

CI

Go project template repository

Work flow

  1. Create a new repository from go-starter
  2. Use make rename to change go mod name
  3. Create a dev branch from main
  4. Make changes
  5. Commit code
  6. Merge pull requests
  7. Create tag
  8. Git Action auto generate CHANGELOG.md and create release

Features

  • xxxx
  • xxxx
  • xxxx

Install

go get github.com/sraphs/flat

Usage

  1. xxxx
  2. xxxx
  3. xxxx

Contributing

We alway welcome your contributions 👏

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. Create Pull Request

CHANGELOG

See Releases

License

MIT © sraph.com

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

func Unflatten(src map[string]interface{}) (dst map[string]interface{})
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

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) Flatten

func (opt Option) Flatten(src interface{}) map[string]interface{}

func (*Option) GetSeparator

func (o *Option) GetSeparator() string

func (Option) Unflatten

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

unflatten

Jump to

Keyboard shortcuts

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