flatten

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Copyright (c) 2016 Jeremy Wohl Hacked together from sources obtained at https://github.com/jeremywohl/flatten

Index

Constants

This section is empty.

Variables

View Source
var (
	// Separate nested key components with dots, e.g. "a.b.1.c.d"
	DotStyle = SeparatorStyle{Middle: "."}

	// Separate with path-like slashes, e.g. a/b/1/c/d
	PathStyle = SeparatorStyle{Middle: "/"}

	// Separate ala Rails, e.g. "a[b][c][1][d]"
	RailsStyle = SeparatorStyle{Before: "[", After: "]"}

	// Separate with underscores, e.g. "a_b_1_c_d"
	UnderscoreStyle = SeparatorStyle{Middle: "_"}
)

Default styles

View Source
var NotValidInputError = errors.New("Not a valid input: map or slice")

Nested input must be a map or slice

View Source
var NotValidJsonInputError = errors.New("Not a valid input, must be a map")

JSON nested input must be a map

Functions

func Flatten

func Flatten(nested map[string]interface{}, style SeparatorStyle) (map[string]interface{}, error)

Flatten generates a flat map from a nested one. The original may include values of type map, slice and scalar, but not struct. Keys in the flat map will be a compound of descending map keys and slice iterations. The presentation of keys is set by style. A prefix is joined to each key.

Types

type SeparatorStyle

type SeparatorStyle struct {
	Before string // Prepend to key
	Middle string // Add between keys
	After  string // Append to key
}

The style of keys. If there is an input with two nested keys "f" and "g", with "f" at the root,

{ "f": { "g": ... } }

the output will be the concatenation

f{Middle}{Before}g{After}...

Any struct element may be blank. If you use Middle, you will probably leave Before & After blank, and vice-versa. See examples in flatten_test.go and the "Default styles" here.

Jump to

Keyboard shortcuts

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