mapulator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2021 License: MIT Imports: 1 Imported by: 0

README

mapulator / Map Manipulator

Used for set value of a field in nested key map, this repo was inspired by sjson and jsonparser

Installation

go get github.com/AldiRvn/mapulator

Usage:

value := "Chris Casey"
splitBySpace := strings.Split(value, " ")
newMap := map[string]interface{}{}

path := "user"
mapulator.Set(newMap, path+".age", 48)
fmt.Println(newMap) // map[user:map[age:48]]
mapulator.Set(newMap, path+".active", true)
fmt.Println(newMap) // map[user:map[active:true age:48]]
mapulator.Set(newMap, path+".hobby", []string{"Fishing", "Drawing"})
fmt.Println(newMap) // map[user:map[active:true age:48 hobby:[Fishing Drawing]]]

path += ".name"
mapulator.Set(newMap, path, map[string]interface{}{
  "first": splitBySpace[0],
  "last":  splitBySpace[len(splitBySpace)-1],
})
fmt.Println(newMap) // map[user:map[active:true age:48 hobby:[Fishing Drawing] name:map[first:Chris last:Casey]]]
mapulator.Set(newMap, path+".full", map[string]interface{}{
  "length": len(value),
  "value":  value,
})
fmt.Println(newMap) // map[user: ... full:map[length:11 value:Chris Casey] last:Casey]]]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Set

func Set(target map[string]interface{}, path string, value interface{}) map[string]interface{}

Godoc Set

func SetNew added in v0.1.0

func SetNew(path string, value interface{}) map[string]interface{}

Godoc SetNew

Types

This section is empty.

Jump to

Keyboard shortcuts

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