Skip to content

AldiRvn/mapulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mapulator / Map Manipulator

Go Report Card
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]]]

About

Map Manipulator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages