mapslice

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: MIT Imports: 5 Imported by: 0

README

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(input, nil)
    // header = []string{"key1", "key2", "key3", "key4"}
    // data   = [][]int{{1, 2, 3, 0}, {4, 0, 5, 6}}

    header, data = mapslice.MaplistToTable(input, []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()

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

Documentation

Overview

Interconversion between map list and slice

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MaplistToTable added in v0.2.0

func MaplistToTable[K cmp.Ordered, V comparable](maplist []map[K]V, fixedColumns []K) (tblheader []K, tbldata [][]V)

MaplistToTable : Convert map list to table.

e.g.: [{"key1": 1, "key2": 2}, {"key1": 3, "key2": 4}] => ["key1", "key2"] and [[1, 2], [3, 4]]

Empty elements has a zero value of type V.

Order of the columns is such that the `fixedColumns` are placed first if they exist, followed by the other keys sorted in ascending order. `fixColumns` accepts nil.

func ReadJson added in v0.2.0

func ReadJson[K comparable, V any](r io.Reader) ([]map[K]V, error)

ReadJson : Read JSON format data to map list.

func TableToMaplist added in v0.2.0

func TableToMaplist[K cmp.Ordered, V comparable](tblheader []K, tbldata [][]V, ignoreZero bool) (maplist []map[K]V)

TableToMaplist : Convert table to map list.

e.g.: ["key1", "key2"] and [[1, 2], [3, 4]] => [{"key1": 1, "key2": 2}, {"key1": 3, "key2": 4}]

If the size of the data row is larger than the size of the header, discard the value. If `ignoreZero` is true, do not store zero values.

func WriteJson added in v0.2.0

func WriteJson[K comparable, V any](w io.Writer, maplist []map[K]V) error

WriteJson : Write JSON format data from map list.

Types

This section is empty.

Jump to

Keyboard shortcuts

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