Interconversion between map list and table
Mainly handles data in the following two formats.
Data in JSON format where the top level is a list, used in log files in JSON format and in Select
results from a Data Base.
[
{"key1": 1, "key2": 2,},
{"key1": 3, "key2": 4,},
...
]
Data in table format, such as a simple CSV file.
[
["key1, "key2"],
[1, 2],
[3, 4],
]
- Functions using generics.
- Map keys and slice header elements must satisfy
cmp.Ordered
. - Map values and slice data elements must satisfy
comparable
.
import "github.com/bugph0bia/go-mapslice"
func main() {
maplist := []map[string]int{
{
"key1": 1,
"key2": 2,
"key3": 3,
},
{
"key1": 4,
"key3": 5,
"key4": 6,
},
}
header, data := mapslice.MaplistToTable(maplist, nil)
// header = []string{"key1", "key2", "key3", "key4"}
// data = [][]int{{1, 2, 3, 0}, {4, 0, 5, 6}}
header, data = mapslice.MaplistToTable(maplist, []string{"key3"})
// header = []string{"key3", "key1", "key2", "key4"}
// data = [][]int{{3, 1, 2, 0}, {5, 4, 0, 6}}
}
- Empty elements has a zero value of type V.
- Order of the columns is such that the
fixColumns
(second argument) are placed first if they exist, followed by the other keys sorted in ascending order.fixColumns
acceptsnil
.
import "github.com/bugph0bia/go-mapslice"
func main() {
header := []string{"key1", "key2", "key3", "key4"}
data := [][]int{{1, 2, 3, 0}, {4, 0, 5, 6}}
maplist := TableToMaplist(header, data, false)
// maplist = []map[string]int{
// {
// "key1": 1,
// "key2": 2,
// "key3": 3,
// "key4": 0,
// },
// {
// "key1": 4,
// "key2": 0,
// "key3": 5,
// "key4": 6,
// },
// }
maplist := TableToMaplist(header, data, true)
// maplist = []map[string]int{
// {
// "key1": 1,
// "key2": 2,
// "key3": 3,
// },
// {
// "key1": 4,
// "key3": 5,
// "key4": 6,
// },
// }
}
- If the size of the data row is larger than the size of the header, discard the value.
- If
ignoreZero
(second argument) is true, do not store zero values.
Read instead of json.Unmarshal
.
import (
"os"
"github.com/bugph0bia/go-mapslice"
)
func main() {
// foo.json
// --------
// [
// {"key1": 1, "key2": 2, "key3": 3},
// {"key1": 4, "key2": 5, "key3": 6}
// ]
f, err := os.Open("foo.json")
if err != nil {
panic(err)
}
defer f.Close()
var maplist []map[string]int
// Read as string key and int value
maplist, err := ReadJson[string, int](f)
if err != nil {
panic(err)
}
}
Write instead of json.Marshal
.
import (
"os"
"github.com/bugph0bia/go-mapslice"
)
func main() {
maplist := []map[string]int{
{
"key1": 1,
"key2": 2,
"key3": 3,
},
{
"key1": 4,
"key2": 5,
"key3": 6,
},
}
f, err := os.Create("foo.json")
if err != nil {
panic(err)
}
defer f.Close()
// Generics type parameter can be omitted because the type is implicitly
// determined by `maplist`.
err := WriteJson(f, maplist) // Meaning as: WriteJson[string, int](f, maplist)
if err != nil {
panic(err)
}
}