Skip to content

bugph0bia/go-mapslice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-mapslice

Interconversion between map list and table

Input/Output Data Examples

Mainly handles data in the following two formats.

Map list (JSON-like)

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,},
    ...
]

Table (CSV-like)

Data in table format, such as a simple CSV file.

[
    ["key1, "key2"],
    [1, 2],
    [3, 4],
]

Usage

  • Functions using generics.
  • Map keys and slice header elements must satisfy cmp.Ordered.
  • Map values and slice data elements must satisfy comparable.

Map list to table

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 accepts nil.

Table to Map list

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 JSON String

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 JSON String

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)
    }
}

About

Interconversion between map list and table

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages