Skip to content

illarion/flat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flat

Minimalistic implementation of the Flatten(map[string]interface{}) and Unflatten(map[string]interface{}) functions.

Description

Flatten converts the nested map into a flat map, where the keys are the paths to the values in the original map. The path is a dot-separated list of keys. The values are the values of the original map.

Unflatten converts the flat map into a nested map, where the keys are the paths to the values in the original map. The path is a dot-separated list of keys. The values are the values of the original map.

Both functions use javascript style to represent arrays in the path. For example, the path a.b[0].c will be converted into the following structure:

map[string]interface{}{
    "a": map[string]interface{}{
        "b": []interface{}{
            map[string]interface{}{
                "c": <value>,
            },
        },
    },
}

Usage

package main

import (
    "fmt"
    "github.com/illarion/flat"
    "encoding/json"
)

func main() {
    var src map[string]interface{}

    json.Parse([]byte(`{"a": {"b": [{"c": 1}, {"c": 2}]}}`), &src)

    flattened := flat.Flatten(src, &flat.Options{
        Delimiter: ".",
    })
    fmt.Sprintf("%#v", flattened)

    unflat := flat.Unflatten(flattened, &flat.Options{
        Delimiter: ".",
    })
    fmt.Sprintf("%#v", unflattened)
}

About

Minimalistic flatten / unflatten for go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages