Skip to content
This repository was archived by the owner on Apr 11, 2022. It is now read-only.

Commit e4de5f0

Browse files
committed
add support to parse stdin in jsonlines format
stdin can be a stream ofnew line seperated jsons controlled by a -line cmd line flag
1 parent 66c25ef commit e4de5f0

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

jsonui.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"io/ioutil"
67
"log"
@@ -30,6 +31,17 @@ func (p position) getCoordinate(max int) int {
3031
return int(p.prc*float32(max)) - p.margin
3132
}
3233

34+
type cmdLineArgs struct {
35+
linejson bool
36+
}
37+
38+
func parseArgs() cmdLineArgs {
39+
var args cmdLineArgs
40+
flag.BoolVar(&args.linejson, "line", false, "to read a linejson file")
41+
flag.Parse()
42+
return args
43+
}
44+
3345
type viewPosition struct {
3446
x0, y0, x1, y1 position
3547
}
@@ -74,7 +86,8 @@ var tree treeNode
7486

7587
func main() {
7688
var err error
77-
tree, err = fromReader(os.Stdin)
89+
args := parseArgs()
90+
tree, err = fromReader(os.Stdin, args.linejson)
7891
if err != nil {
7992
log.Panicln(err)
8093
}

test_big_line.jsonl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{"_id": "5aad46661d83f1d4478a70d6", "index": 0, "guid": "533e17ee-ecbb-4540-ab01-18b1fbff229b", "isActive": false, "balance": "$1,572.83", "picture": "http://placehold.it/32x32", "age": 33, "eyeColor": "green", "name": "Lee Potts", "gender": "female", "company": "HOMELUX", "email": "leepotts@homelux.com", "phone": "+1 (860) 473-2707", "address": "321 Krier Place, Kennedyville, South Dakota, 2338", "about": "Aute deserunt consectetur labore consequat in amet laboris dolor amet aute anim adipisicing incididunt. Nulla nisi culpa deserunt reprehenderit ut cillum pariatur labore velit. Commodo magna anim sit amet officia. Et ea et cillum reprehenderit officia incididunt enim ea fugiat. Sit aliquip id Lorem et nulla aliqua minim exercitation aliquip Lorem.\r\n", "registered": "2017-01-06T04:10:33 -01:00", "latitude": 89.450692, "longitude": 171.363255, "tags": ["esse", "minim", "ullamco", "amet", "laboris", "aliqua", "eu"], "friends": [{"id": 0, "name": "Bernadette Dodson"}, {"id": 1, "name": "Francesca Oconnor"}, {"id": 2, "name": "Joni Pierce"}], "greeting": "Hello, Lee Potts! You have 2 unread messages.", "favoriteFruit": "apple"}
2+
{"_id": "5aad466678c7582cf847cb3d", "index": 1, "guid": "ca7902b9-97ff-45f6-b518-58a5b415f88a", "isActive": false, "balance": "$2,476.45", "picture": "http://placehold.it/32x32", "age": 29, "eyeColor": "green", "name": "Anderson Black", "gender": "male", "company": "KENEGY", "email": "andersonblack@kenegy.com", "phone": "+1 (832) 577-3506", "address": "877 Conduit Boulevard, Kenwood, Alaska, 9413", "about": "Esse ad duis fugiat laboris velit aliqua eiusmod in anim anim mollit. Nulla ad eu officia culpa deserunt sit Lorem fugiat incididunt commodo incididunt. Velit veniam laboris magna ipsum deserunt magna.\r\n", "registered": "2014-08-26T10:27:19 -02:00", "latitude": 87.57076, "longitude": -135.016354, "tags": ["officia", "officia", "laborum", "ullamco", "ipsum", "consequat", "ad"], "friends": [{"id": 0, "name": "Tamra Brewer"}, {"id": 1, "name": "Krystal Moore"}, {"id": 2, "name": "Rosalyn Ward"}], "greeting": "Hello, Anderson Black! You have 4 unread messages.", "favoriteFruit": "strawberry"}
3+
{"_id": "5aad4666afaf5db20037e534", "index": 2, "guid": "94c7e87d-a7b6-45db-b20a-99e290e22754", "isActive": true, "balance": "$2,494.85", "picture": "http://placehold.it/32x32", "age": 37, "eyeColor": "blue", "name": "Jordan Mcgowan", "gender": "female", "company": "VALPREAL", "email": "jordanmcgowan@valpreal.com", "phone": "+1 (817) 542-3680", "address": "238 Taaffe Place, Clarksburg, Nevada, 6185", "about": "Dolore duis adipisicing commodo incididunt laboris elit in fugiat sint et proident ut deserunt. Nulla laboris laboris magna magna enim consequat adipisicing nisi fugiat nulla. Mollit quis reprehenderit qui nulla incididunt. Lorem eu aliquip consequat elit laboris ea. Adipisicing aliqua aute reprehenderit ex ex ipsum ut quis cupidatat. Pariatur veniam voluptate commodo ea ex ea dolore. Velit quis nisi sit eu incididunt proident fugiat duis dolore ipsum in esse incididunt dolore.\r\n", "registered": "2017-09-16T05:43:31 -02:00", "latitude": -11.044863, "longitude": 90.12262, "tags": ["cupidatat", "minim", "exercitation", "Lorem", "enim", "sunt", "culpa"], "friends": [{"id": 0, "name": "Alfreda Dawson"}, {"id": 1, "name": "Arlene Wyatt"}, {"id": 2, "name": "Silvia Howell"}], "greeting": "Hello, Jordan Mcgowan! You have 10 unread messages.", "favoriteFruit": "apple"}
4+
{"_id": "5aad4666a7f7621eb94274cb", "index": 3, "guid": "182084be-4c18-4d8f-8362-93896df1a9b1", "isActive": false, "balance": "$3,661.29", "picture": "http://placehold.it/32x32", "age": 28, "eyeColor": "blue", "name": "Dennis Randolph", "gender": "male", "company": "GUSHKOOL", "email": "dennisrandolph@gushkool.com", "phone": "+1 (843) 596-3413", "address": "704 Juliana Place, Edinburg, Idaho, 1851", "about": "Quis aliqua commodo ex quis proident ex eu fugiat aliqua enim. Dolore adipisicing id amet qui ut pariatur. Nulla anim consequat et duis culpa. Aute occaecat fugiat mollit excepteur id officia irure incididunt enim amet. Dolore in in eu incididunt labore mollit nisi laborum pariatur occaecat. Ipsum enim ea fugiat sit ipsum id ex cupidatat ut dolor eiusmod.\r\n", "registered": "2017-11-13T08:35:36 -01:00", "latitude": 63.17661, "longitude": 109.150388, "tags": ["id", "laborum", "ullamco", "laboris", "fugiat", "sint", "aliquip"], "friends": [{"id": 0, "name": "Rutledge William"}, {"id": 1, "name": "Terrie Tucker"}, {"id": 2, "name": "Latasha Miranda"}], "greeting": "Hello, Dennis Randolph! You have 4 unread messages.", "favoriteFruit": "banana"}
5+
{"_id": "5aad46661f6b5001b0225a05", "index": 4, "guid": "5ebbe09d-5bb3-4b51-82b3-cea6a01f58f2", "isActive": false, "balance": "$3,480.46", "picture": "http://placehold.it/32x32", "age": 39, "eyeColor": "blue", "name": "Desiree Mcintosh", "gender": "female", "company": "HOMETOWN", "email": "desireemcintosh@hometown.com", "phone": "+1 (967) 571-2881", "address": "535 Ryder Street, Utting, Kentucky, 2105", "about": "Excepteur veniam et consequat tempor tempor consectetur aliqua minim magna nisi commodo laboris aliqua. Anim ex elit ad id aliquip amet officia ad cillum dolor mollit sint. Qui eu irure aute cupidatat sit enim nulla est consectetur dolore est irure duis.\r\n", "registered": "2014-09-22T04:36:59 -02:00", "latitude": -40.113604, "longitude": -175.139057, "tags": ["consectetur", "tempor", "laboris", "cupidatat", "nisi", "laborum", "nulla"], "friends": [{"id": 0, "name": "Susan Heath"}, {"id": 1, "name": "Morse Rosales"}, {"id": 2, "name": "Sanchez Watson"}], "greeting": "Hello, Desiree Mcintosh! You have 2 unread messages.", "favoriteFruit": "strawberry"}

tree.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -467,10 +468,33 @@ func fromBytes(b []byte) (treeNode, error) {
467468
return newTree(y)
468469
}
469470

470-
func fromReader(r io.Reader) (treeNode, error) {
471-
b, err := ioutil.ReadAll(os.Stdin)
472-
if err != nil {
473-
return nil, err
471+
func fromReader(r io.Reader, linejson bool) (treeNode, error) {
472+
var rootNode treeNode = nil
473+
var err error = nil
474+
if linejson {
475+
rd := bufio.NewReader(os.Stdin)
476+
rootList := []treeNode{}
477+
for {
478+
line, err := rd.ReadBytes('\n')
479+
if err != nil {
480+
break
481+
}
482+
lineTree, err := fromBytes(line)
483+
if err != nil {
484+
return nil, err
485+
}
486+
rootList = append(rootList, lineTree)
487+
}
488+
rootNode = &listNode{
489+
baseTreeNode{true},
490+
rootList,
491+
}
492+
} else {
493+
b, err := ioutil.ReadAll(os.Stdin)
494+
if err != nil {
495+
return nil, err
496+
}
497+
rootNode, err = fromBytes(b)
474498
}
475-
return fromBytes(b)
499+
return rootNode, err
476500
}

0 commit comments

Comments
 (0)