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

add support to parse stdin in jsonlines format #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: go

go:
- "1.8"
- "1.10.x"
- master
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
default: build

installdep:
@go get github.com/golang/lint/golint
@go get golang.org/x/lint/golint
@go get

build: installdep
Expand Down
15 changes: 14 additions & 1 deletion jsonui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -30,6 +31,17 @@ func (p position) getCoordinate(max int) int {
return int(p.prc*float32(max)) - p.margin
}

type cmdLineArgs struct {
linejson bool
}

func parseArgs() cmdLineArgs {
var args cmdLineArgs
flag.BoolVar(&args.linejson, "line", false, "to read a linejson file")
flag.Parse()
return args
}

type viewPosition struct {
x0, y0, x1, y1 position
}
Expand Down Expand Up @@ -74,7 +86,8 @@ var tree treeNode

func main() {
var err error
tree, err = fromReader(os.Stdin)
args := parseArgs()
tree, err = fromReader(os.Stdin, args.linejson)
if err != nil {
log.Panicln(err)
}
Expand Down
5 changes: 5 additions & 0 deletions test_big_line.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"_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"}
{"_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"}
{"_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"}
{"_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"}
{"_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"}
34 changes: 29 additions & 5 deletions tree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -467,10 +468,33 @@ func fromBytes(b []byte) (treeNode, error) {
return newTree(y)
}

func fromReader(r io.Reader) (treeNode, error) {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
func fromReader(r io.Reader, linejson bool) (treeNode, error) {
var rootNode treeNode = nil
var err error = nil
if linejson {
rd := bufio.NewReader(os.Stdin)
rootList := []treeNode{}
for {
line, err := rd.ReadBytes('\n')
if err != nil {
break
}
lineTree, err := fromBytes(line)
if err != nil {
return nil, err
}
rootList = append(rootList, lineTree)
}
rootNode = &listNode{
baseTreeNode{true},
rootList,
}
} else {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
rootNode, err = fromBytes(b)
}
return fromBytes(b)
return rootNode, err
}