Skip to content
This repository was archived by the owner on Dec 13, 2025. It is now read-only.

Commit 2d748ee

Browse files
author
Thomas von Dein
committed
add unit test, json indentation, rename json obj vars
1 parent 914095e commit 2d748ee

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

lib/printer.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,32 +295,31 @@ func printShellData(writer io.Writer, data *Tabdata) {
295295
}
296296

297297
func printJsonData(writer io.Writer, data *Tabdata) {
298-
hashlist := make([]map[string]any, len(data.entries))
298+
objlist := make([]map[string]any, len(data.entries))
299299

300300
if len(data.entries) > 0 {
301301
for i, entry := range data.entries {
302-
hash := make(map[string]any, len(entry))
302+
obj := make(map[string]any, len(entry))
303303

304304
for idx, value := range entry {
305305
num, err := strconv.Atoi(value)
306306
if err == nil {
307-
hash[data.headers[idx]] = num
307+
obj[data.headers[idx]] = num
308308
} else {
309-
hash[data.headers[idx]] = value
309+
obj[data.headers[idx]] = value
310310
}
311311
}
312312

313-
hashlist[i] = hash
313+
objlist[i] = obj
314314
}
315315
}
316316

317-
jsonstr, err := json.Marshal(&hashlist)
317+
jsonstr, err := json.MarshalIndent(&objlist, "", " ")
318318

319319
if err != nil {
320320
log.Fatal(err)
321321
}
322322

323-
//repr.Println(jsonstr)
324323
output(writer, string(jsonstr))
325324
}
326325

lib/printer_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@ ceta,33d12h,9,06/Jan/2008 15:04:05 -0700`,
125125
NAME="beta" DURATION="1d10h5m1s" COUNT="33" WHEN="3/1/2014"
126126
NAME="alpha" DURATION="4h35m" COUNT="170" WHEN="2013-Feb-03"
127127
NAME="ceta" DURATION="33d12h" COUNT="9" WHEN="06/Jan/2008 15:04:05 -0700"`,
128+
},
129+
{
130+
name: "json",
131+
mode: cfg.Json,
132+
numberize: false,
133+
expect: `[
134+
{
135+
"COUNT": 33,
136+
"DURATION": "1d10h5m1s",
137+
"NAME": "beta",
138+
"WHEN": "3/1/2014"
139+
},
140+
{
141+
"COUNT": 170,
142+
"DURATION": "4h35m",
143+
"NAME": "alpha",
144+
"WHEN": "2013-Feb-03"
145+
},
146+
{
147+
"COUNT": 9,
148+
"DURATION": "33d12h",
149+
"NAME": "ceta",
150+
"WHEN": "06/Jan/2008 15:04:05 -0700"
151+
}
152+
]`,
128153
},
129154
{
130155
name: "yaml",

0 commit comments

Comments
 (0)