Help us shape the future of Go! Take the 2026 Go Developer Survey

flatmap

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

README

flatmap

flatten and expand maps of interfaces

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultTokenizer = StringTokenizer(".")

DefaultTokenizer is a tokenizer using a dot or fullstop

Functions

This section is empty.

Types

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map is a flatten map

Example
sample := map[string]interface{}{
	"supu": 42,
	"tupu": false,
	"foo":  "bar",
	"aa":   23,
	"a": map[string]interface{}{
		"aa":  23,
		"ac":  23,
		"abc": 23,
		"a": map[string]interface{}{
			"a": map[string]interface{}{
				"b": true,
				"c": 42,
				"d": "tupu",
			},
			"b": true,
			"c": 42,
			"d": "tupu",
		},
		"ab": map[string]interface{}{
			"b": true,
			"c": 42,
			"d": "tupu",
		},
		"b": true,
		"c": 42,
		"d": "tupu",
		"collection": []interface{}{
			map[string]interface{}{
				"b":  false,
				"d":  "aaaa",
				"dx": "foobar",
			},
			map[string]interface{}{
				"b": true,
				"c": 42,
				"d": "tupu",
			},
		},
		"collection2": []interface{}{
			map[string]interface{}{
				"d": []interface{}{
					map[string]interface{}{
						"a": 1,
						"d": []int{1, 2, 3, 4, 5},
					},
					map[string]interface{}{
						"d": []int{1, 2, 3, 4, 5},
					},
					map[string]interface{}{
						"d": []int{1, 2, 3, 4, 5},
					},
				},
			},
			map[string]interface{}{
				"d": []int{1, 2, 3, 4, 5},
			},
			map[string]interface{}{
				"d": []int{1, 2, 3, 4, 5},
			},
		},
	},
}

flatten, _ := Flatten(sample, DefaultTokenizer)

flatten.Del("a.b")
flatten.Del("a.*.a")
flatten.Del("a.collection.1.b")
flatten.Del("a.collection.*.d")
flatten.Del("a.collection2.*.d.*.d")
flatten.Move("tupu", "tuputupu")
flatten.Move("a.c", "a.cb")
flatten.Move("a.a", "acb")
flatten.Del("a.ab")

res := flatten.Expand()

b, _ := json.MarshalIndent(res, "", "\t")
fmt.Println(string(b))
Output:
{
	"a": {
		"aa": 23,
		"abc": 23,
		"ac": 23,
		"cb": 42,
		"collection": [
			{
				"b": false,
				"dx": "foobar"
			},
			{
				"c": 42
			}
		],
		"collection2": [
			{
				"d": [
					{
						"a": 1
					},
					null,
					null
				]
			},
			{
				"d": [
					1,
					2,
					3,
					4,
					5
				]
			},
			{
				"d": [
					1,
					2,
					3,
					4,
					5
				]
			}
		],
		"d": "tupu"
	},
	"aa": 23,
	"acb": {
		"b": true,
		"c": 42,
		"d": "tupu"
	},
	"foo": "bar",
	"supu": 42,
	"tuputupu": false
}

func Flatten

func Flatten(m map[string]interface{}, tokenizer Tokenizer) (*Map, error)

Flatten takes a hierarchy and flatten it using the tokenizer supplied

Example
sample := map[string]interface{}{
	"supu": 42,
	"tupu": false,
	"foo":  "bar",
	"a": map[string]interface{}{
		"b": true,
		"c": 42,
		"d": "tupu",
	},
	"collection": []interface{}{
		map[string]interface{}{
			"b": false,
			"d": "foobar",
		},
		map[string]interface{}{
			"b": true,
			"c": 42,
			"d": "tupu",
		},
	},
}

res, _ := Flatten(sample, DefaultTokenizer)
b, _ := json.MarshalIndent(res.m, "", "\t")
fmt.Println(string(b))
Output:
{
	"a.b": true,
	"a.c": 42,
	"a.d": "tupu",
	"collection.#": 2,
	"collection.0.b": false,
	"collection.0.d": "foobar",
	"collection.1.b": true,
	"collection.1.c": 42,
	"collection.1.d": "tupu",
	"foo": "bar",
	"supu": 42,
	"tupu": false
}
Example (Collection)
sample := map[string]interface{}{
	"a": []interface{}{
		map[string]interface{}{
			"b": []interface{}{
				map[string]interface{}{
					"c": map[string]interface{}{
						"a": 1,
					},
					"aa": 1,
				},
				map[string]interface{}{
					"c": map[string]interface{}{
						"a": 2,
					},
					"aa": 1,
				},
			},
			"aa": 1,
		},
		map[string]interface{}{
			"b": []interface{}{
				map[string]interface{}{
					"c": map[string]interface{}{
						"a": 1,
					},
					"aa": 1,
				},
			},
			"aa": 1,
		},
	},
	"tupu": false,
}
res, _ := Flatten(sample, DefaultTokenizer)

b, _ := json.MarshalIndent(res.m, "", "\t")
fmt.Println(string(b))
Output:
{
	"a.#": 2,
	"a.0.aa": 1,
	"a.0.b.#": 2,
	"a.0.b.0.aa": 1,
	"a.0.b.0.c.a": 1,
	"a.0.b.1.aa": 1,
	"a.0.b.1.c.a": 2,
	"a.1.aa": 1,
	"a.1.b.#": 1,
	"a.1.b.0.aa": 1,
	"a.1.b.0.c.a": 1,
	"tupu": false
}

func (*Map) Del

func (m *Map) Del(prefix string)

Del deletes a key out of the map with the given prefix

func (*Map) Expand

func (m *Map) Expand() map[string]interface{}

Expand expands the Map into a more complex structure. This is the reverse of the Flatten operation.

func (*Map) Move

func (m *Map) Move(original, newKey string)

Move makes changes in the flatten hierarchy moving contents from origin to newKey

type StringTokenizer

type StringTokenizer string

StringTokenizer is a Tokenizer using the string as separator

func (StringTokenizer) Keys

func (s StringTokenizer) Keys(ks string) []string

Keys returns the keys contained in the received token

func (StringTokenizer) Separator

func (s StringTokenizer) Separator() string

Separator returns the separator

func (StringTokenizer) Token

func (s StringTokenizer) Token(ks []string) string

Token returns a token joining all the keys with s as separator

type Tokenizer

type Tokenizer interface {
	Separator() string
	Token([]string) string
	Keys(string) []string
}

Tokenizer is an interface for key manipulators

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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