unflatten

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2020 License: MIT Imports: 1 Imported by: 7

README

unflatten

This library can "flatten" and "unflatten" a hierarchy stored in a map[string]interface{}.

GitHub Actions status Go Report Card Documentation Coverage Status

usage

var m = map[string]interface{}{
	"cpu.usage.0.user": map[string]interface{}{
		"value": 2.3,
	},
	"cpu.usage.0.system": map[string]interface{}{
		"value": 1.2,
	},
}

tree := Unflatten(m, func(k string) []string { return strings.Split(k, ".") })

contributions

Thanks to Andrew Leap for rewriting this library and reminding me I need to use functions more in golang.

License

This code is Copyright (c) 2014 Mark Wolfe and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flatten

func Flatten(m map[string]interface{}, tokenizer func([]string) string) map[string]interface{}

Flatten take a hierarchy and flatten it using the tokenizer supplied

func JoinWithDot added in v1.0.1

func JoinWithDot(ks []string) string

JoinWithDot used to join tokens using a dot or fullstop

func SplitByDot added in v1.0.1

func SplitByDot(k string) []string

SplitByDot split the supplied keys by dot or fullstop character

func Unflatten

func Unflatten(m map[string]interface{}, tf TokenizerFunc) map[string]interface{}

Unflatten This function will unflatten a map with keys which are comprised of multiple tokens which are segmented by the tokenizer function.

Types

type TokenizerFunc

type TokenizerFunc func(string) []string

TokenizerFunc This function is used to tokenize the keys in the flattened data structure.

The following example uses strings.Split to tokenize based on .

func(k string) []string { return strings.Split(k, ".") }

Jump to

Keyboard shortcuts

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