Generic mapStringInterface tool for extracting of data for CSV output

generic mapStringInterface tool for extracting of data for CSV output

TL;DR

use this A LITTLE BIT like the pandas library 
but for map[string]interface{} to perform basic data extraction

here is some EXAMPLE code:

package main

import (
	"encoding/json"
	"log"
)

var randomRubbishJSON = `{
	"_id": "617c120ab7a3fa5dd968346d",
	"index": 0,
	"guid": "f3a4d826-30d6-4811-ba08-cda75b221f2b",
	"isActive": true,
	"balance": "$1,241.70",
	"picture": "http://placehold.it/32x32",
	"age": 37,
	"eyeColor": "brown",
	"name": "Susie Noble",
	"gender": "female",
	"company": "ZILLA",
	"email": "[email protected]",
	"phone": "+1 (925) 507-2761",
	"address": "266 Legion Street, Cressey, Illinois, 8982",
	"about": "Aliquip ut veniam mollit duis non minim ad amet est ex id fugiat consequat. Amet labore nulla esse adipisicing velit fugiat aute fugiat in Lorem aliquip et exercitation. Proident laborum aliqua laborum esse Lorem minim. Velit do aute cillum laborum sit excepteur minim fugiat cillum laborum ex enim. Consequat minim nisi adipisicing eu deserunt esse id id anim esse consequat sunt cillum deserunt. Sit commodo aute adipisicing irure nulla in aliquip pariatur eiusmod consectetur velit. Ea anim nulla cillum eu fugiat.\r\n",
	"registered": "2017-08-29T12:01:21 -01:00",
	"latitude": -39.228822,
	"longitude": 136.128045,
	"tags": [
	  "consequat",
	  "magna",
	  "exercitation",
	  "eu",
	  "minim",
	  "duis",
	  "qui"
	],
	"friends": [
	  {
		"id": 0,
		"name": "Diane Martinez"
	  },
	  {
		"id": 1,
		"name": "Keller Schneider"
	  },
	  {
		"id": 2,
		"name": "Imogene Kemp"
	  }
	],
	"greeting": "Hello, Susie Noble! You have 7 unread messages.",
	"favoriteFruit": "strawberry"
  }`
func main() {
	exampleInput := make(map[string]interface{})
	err := json.Unmarshal([]byte(randomRubbishJSON), &exampleInput)
	if err != nil {
		log.Println(err)
		return
	}
	d := Decoder{}
	d.Decode(exampleInput)
	d.Print()
}

what is output of d.Print() ?

Name: root
Root: 
field: [phone greeting latitude _id balance email longitude favoriteFruit age about registered name gender company guid isActive picture index eyeColor address]
array: [tags]
sub: [friends]

so now let's do something useful:

func main() {
	exampleInput := make(map[string]interface{})
	err := json.Unmarshal([]byte(randomRubbishJSON), &exampleInput)
	if err != nil {
		log.Println(err)
		return
	}
	root := Decoder{}
	root.Decode(exampleInput)
	friends := root.Get("friends")
	for i := range friends {
		fmt.Printf("%s,%s,%s,%s\n", root.Field("name"), friends[i].Name, friends[i].Field("id"), friends[i].Field("name"))
	}
}

output:
Susie Noble,friends,0,Diane Martinez
Susie Noble,friends,1,Keller Schneider
Susie Noble,friends,2,Imogene Kemp

with this decoder you can basically create any CSV dataset you want with the right instructions,

todo:
handle more types
more test
linting & general refactoring

CREATE A FRONT-END WEBAPP that leverages the fact 
that you don't need to curate structs for everything

Have fun!

  • Tom
Owner
tbal999
FaaS Cloud Engineer
tbal999
Similar Resources

Generic-list-go - Go container/list but with generics

generic-list-go Go container/list but with generics. The code is based on contai

Dec 7, 2022

Nune-go - High-performance numerical engine based on generic tensors

Nune (v0.1) Numerical engine is a library for performing numerical computation i

Nov 9, 2022

Colorize (highlight) `go build` command output

Colorize (highlight) `go build` command output

colorgo colorgo is a wrapper to go command that colorizes output from go build and go test. Installation go get -u github.com/songgao/colorgo Usage c

Dec 18, 2022

Go library for Common Lisp format style output

format This library has the goal to bring the Common Lisp format directive to Go. This is work-in-progress, see the summary implementation table below

Jul 7, 2020

A cli for fetching the status and full output of CircleCI jobs.

CCI A cli for fetching the status and full output of CircleCI jobs. Install go install github.com/tmessi/cci/cci@latest Usage cci is designed to have

Oct 29, 2021

rsync wrapper (or output parser) that pushes metrics to prometheus

rsync-prom An rsync wrapper (or output parser) that pushes metrics to prometheus. This allows you to then build dashboards and alerting for your rsync

Dec 11, 2022

Run The World. Command aggregator output. Define many services watch them in one place.

Run The World. Command aggregator output. Define many services watch them in one place.

Feb 2, 2022

Parse a shell script and output all export declarations in an easy to read format

Find Exports Parse a shell script and output all export declarations in an easy to read format. Usage Example $ findexports ~/.bashrc PATH=$PATH:/usr/

Jan 13, 2022

🏃‍♂️ A new way to execute commands and manipulate command output in Go

🏃‍♂️ A new way to execute commands and manipulate command output in Go

Nov 11, 2022
Generic - Golang generic example

泛型 场景 假设需要写一个列表总数计算的函数,根据不同数据类型,我们可能分别要写 SumInts(data []int),SumFloats(data []fl

Jan 27, 2022
import csv into existing table postgresql or cockroachdb

import csv into existing table postgresql or cockroachdb

Nov 1, 2021
Converts grouped transactions in a ZKB transaction CSV (incl. details) to single transactions

ZKB Converter Converts grouped transactions in a ZKB transaction CSV (incl. deta

Dec 26, 2021
a generic object pool for golang

Go Commons Pool The Go Commons Pool is a generic object pool for Golang, direct rewrite from Apache Commons Pool. Features Support custom PooledObject

Jan 5, 2023
Optimistic rollup tech, minimal and generic.

Opti Optimistic rollup tech, minimal and generic. VERY experimental, just exploratory code, question is: 1:1 EVM rollup with interactive fraud proof p

Aug 30, 2022
Generic slices for Go 1.8+

Slice A simple package that makes working with slices a little bit easier with the help of generics. Install go get github.com/twharmon/slice Example

Nov 1, 2022
Generic tools for go 1.18+

Gtools Generic tools for go 1.18+ FT (func tools) Provide func tools over iterators Iterators for functions like Filter, Map, Reduce, etc solve 3 main

Jan 12, 2022
Ecsgo - Cache friendly, Multi threading Entity Component System in Go (with Generic)

ECSGo ECSGo is an Entity Component System(ECS) in Go. This is made with Generic

Oct 19, 2022
Go-generic - A collection of experiments using Go Generics coming out in Go 1.18

Go Generic - experiments with Go 1.18 beta Data structures: iter.Iter[T any] - l

Aug 15, 2022
Nune - High-performance numerical engine based on generic tensors

Nune (v0.1) Numerical engine is a library for performing numerical computation i

Nov 9, 2022