mapUtils

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2020 License: MIT Imports: 5 Imported by: 0

README

Kushuh go map utils

Merge

Merge multiple map[string]interface{} objects together.

Variables

original := map[string]interface{}{
    "key1": "value1",
    "key2": map[string]interface{}{
        "key21": 10,
        "key22": true,
    },
    "key3": map[string]interface{}{
        "key3-1": map[string]interface{}{
            "ultra-nested": []string{},
        },
        "key32": false,
    },
}

replacement1 := map[string]interface{}{
    "key2": "value2",
    "key3": map[string]interface{}{
        "key32": 11,
    },
}

replacement2 := map[string]interface{}{
    "key4": "value4",
}

Function call

import "github.com/Alvarios/kushuh-go-utils/map-utils"

// Add as many replacement objects as you want.
newMap, err := mapUtils.Merge(original, replacement1, replacement2)

Will result in:

map[string]interface{}{
    "key1": "value1",
    "key2": "value2",
    "key3": map[string]interface{}{
        "key31": map[string]interface{}{
            "ultra-nested": []string{},
        }
        "key32": 11,
    },
    "key4": "value4",
}
Flatten

Extract each nested key of a map at top level.

value := map[string]interface{}{
    "key1": "value1",
    "key2": "value2",
    "key3": map[string]interface{}{
        "key31": map[string]interface{}{
            "ultra-nested": []string{},
        }
        "key32": 11,
    },
    "key4": "value4",
}

flattened := mapUtils.Flatten(value)

Will result in:

map[string]interface{}{
    "key1": "value1",
    "key2": "value2",
    "key3.key31.ultra-nested": []string{},
    "key3.key32": 11,
    "key4": "value4",
}

2020 Kushuh - MIT license

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssignPath added in v1.4.0

func AssignPath(v map[string]interface{}, path string, e interface{}) map[string]interface{}

func Flatten added in v1.4.0

func Flatten(params map[string]interface{}, parentKey string) map[string]interface{}

Each nested key will be extracted as a top level key. eg: {k1 : val1, k2: {k21: val21}} is flattened as {k1: val1, k2.k21: val21}

func Merge

func Merge(origin interface{}, invaders ...interface{}) (interface{}, error)

func ToMap added in v1.4.1

func ToMap(v interface{}) (map[string]interface{}, *responses.Error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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