flatten

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2019 License: MIT Imports: 4 Imported by: 101

README

flatten

GoDoc Build Status

Flatten makes flat, one-dimensional maps from arbitrarily nested ones.

It turns map keys into compound names, in four default styles: dotted (a.b.1.c), path-like (a/b/1/c), Rails (a[b][1][c]), or with underscores (a_b_1_c). Alternatively, you can pass a custom style.

It takes input as either JSON strings or Go structures. It knows how to traverse these JSON types: objects/maps, arrays and scalars.

You can flatten JSON strings.

nested := `{
  "one": {
    "two": [
      "2a",
      "2b"
    ]
  },
  "side": "value"
}`

flat, err := flatten.FlattenString(nested, "", flatten.DotStyle)

// output: `{ "one.two.0": "2a", "one.two.1": "2b", "side": "value" }`

Or Go maps directly.

nested := map[string]interface{}{
   "a": "b",
   "c": map[string]interface{}{
       "d": "e",
       "f": "g",
   },
   "z": 1.4567,
}

flat, err := flatten.Flatten(nested, "", flatten.RailsStyle)

// output:
// map[string]interface{}{
//  "a":    "b",
//  "c[d]": "e",
//  "c[f]": "g",
//  "z":    1.4567,
// }

Let's try a custom style, with the first example above.

emdash := flatten.SeparatorStyle{Middle: "--"}
flat, err := flatten.FlattenString(nested, "", emdash)

// output: `{ "one--two--0": "2a", "one--two--1": "2b", "side": "value" }`

See godoc for API.

Documentation

Overview

Flatten makes flat, one-dimensional maps from arbitrarily nested ones.

It turns map keys into compound names, in four default styles: dotted (`a.b.1.c`), path-like (`a/b/1/c`), Rails (`a[b][1][c]`), or with underscores (`a_b_1_c`). Alternatively, you can pass a custom style.

It takes input as either JSON strings or Go structures. It knows how to traverse these JSON types: objects/maps, arrays and scalars.

You can flatten JSON strings.

nested := `{
  "one": {
    "two": [
      "2a",
      "2b"
    ]
  },
  "side": "value"
}`

flat, err := flatten.FlattenString(nested, "", flatten.DotStyle)

// output: `{ "one.two.0": "2a", "one.two.1": "2b", "side": "value" }`

Or Go maps directly.

nested := map[string]interface{}{
	"a": "b",
	"c": map[string]interface{}{
		"d": "e",
		"f": "g",
	},
	"z": 1.4567,
}

flat, err := flatten.Flatten(nested, "", flatten.RailsStyle)

// output:
// map[string]interface{}{
//	"a":    "b",
//	"c[d]": "e",
//	"c[f]": "g",
//	"z":    1.4567,
// }

Let's try a custom style, with the first example above.

emdash := flatten.SeparatorStyle{Middle: "--"}
flat, err := flatten.FlattenString(nested, "", emdash)

// output: `{ "one--two--0": "2a", "one--two--1": "2b", "side": "value" }`

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{}, prefix string, 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.

func FlattenString

func FlattenString(nestedstr, prefix string, style SeparatorStyle) (string, error)

FlattenString generates a flat JSON map from a nested one. 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