Skip to content

Commit e9753df

Browse files
committed
swapped jsoncols simple column build for encoding/csv output
1 parent a3f92f1 commit e9753df

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

TODO.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
# Action Items
33

4+
## Bug
5+
6+
47
## Next
58

6-
+ [x] csvcols -col option should not be a boolean, it should take a range like other csv cli
79
+ [ ] csvrows would output a range of rows (e.g. [2:] would be all rows but the first row)
810
+ [ ] csv utilities to support integer ranges notation for columns and rows references, E.g. "1,3:4,7,10:" or all
911

@@ -44,15 +46,14 @@
4446
+ strip punctuation
4547
+ rename keys
4648
+ [ ] csvrotate would take a CSV file as import and output columns as rows
47-
+ [ ] json2csv would convert a 2d JSON array to CSV output, it would comvert a JSON object/map to a column of keys next to a column of values
48-
+ E.g. `cat data.json | json2csv`
4949
+ [ ] smartcat would function like cat but with support for ranges of lines (e.g. show me last 20 lines: smartcat -start=0 -end="-20" file.txt; cat starting with 10th line: smartcat -start=10 file.txt)
5050
+ [ ] allow prefix line number with a specific delimiter (E.g. comma would let you cat a CSV file adding row numbers as first column)
5151
+ [ ] show lines with prefix, suffix, containing or regxp
5252
+ [ ] show lines without prefix, suffix, containing or regexp
5353

5454
## Completed
5555

56+
+ [x] csvcols -col option should not be a boolean, it should take a range like other csv cli
5657
+ [x] utilities should use starting index of 1 instead of zero as humans refer to column 1 when intending to work on the first column
5758
+ [x] for all cli the -delimiter option should support special characters like \t, \n, \r
5859
+ [x] csvfind would accept CSV input from stdin and output rows with matching column values

cmds/jsoncols/jsoncols.go

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
package main
99

1010
import (
11+
"encoding/csv"
1112
"encoding/json"
1213
"flag"
1314
"fmt"
1415
"io/ioutil"
1516
"os"
1617
"path"
17-
"strings"
1818

1919
// Caltech Library Packages
2020
"github.com/caltechlibrary/cli"
@@ -89,7 +89,6 @@ Would yield
8989
delimiter = ","
9090
expressions []string
9191
permissive bool
92-
quote bool
9392
)
9493

9594
func handleError(err error, exitCode int) {
@@ -119,7 +118,6 @@ func init() {
119118
flag.BoolVar(&runInteractive, "r", false, "run interactively")
120119
flag.BoolVar(&runInteractive, "repl", false, "run interactively")
121120
flag.StringVar(&delimiter, "d", delimiter, "set the delimiter for multi-field output")
122-
flag.BoolVar(&quote, "quote", false, "if dilimiter is found in column value add quotes")
123121
flag.BoolVar(&permissive, "permissive", false, "suppress error messages")
124122
flag.BoolVar(&permissive, "quiet", false, "suppress error messages")
125123
}
@@ -189,38 +187,37 @@ func main() {
189187
}
190188

191189
// For each dotpath expression return a result
192-
for i, qry := range expressions {
193-
if i > 0 {
194-
fmt.Fprintf(out, "%s", delimiter)
195-
}
196-
if qry == "." {
197-
fmt.Fprintf(out, "%s", buf)
198-
} else {
199-
result, err := dotpath.Eval(qry, data)
200-
if err == nil {
201-
switch result.(type) {
202-
case string:
203-
if quote == true && strings.Contains(result.(string), delimiter) == true {
204-
fmt.Fprintf(out, "%q", result)
205-
} else {
206-
fmt.Fprintf(out, "%s", result)
207-
}
208-
case json.Number:
209-
fmt.Fprintf(out, "%s", result.(json.Number).String())
210-
default:
211-
src, err := json.Marshal(result)
212-
if err != nil {
213-
handleError(err, 1)
214-
}
215-
if quote == true {
216-
fmt.Fprintf(out, "%q", src)
217-
} else {
218-
fmt.Fprintf(out, "%s", src)
219-
}
190+
row := []string{}
191+
for _, qry := range expressions {
192+
result, err := dotpath.Eval(qry, data)
193+
if err == nil {
194+
switch result.(type) {
195+
case string:
196+
row = append(row, result.(string))
197+
case json.Number:
198+
row = append(row, result.(json.Number).String())
199+
default:
200+
src, err := json.Marshal(result)
201+
if err != nil {
202+
handleError(err, 1)
220203
}
221-
} else {
222-
handleError(err, 1)
204+
row = append(row, fmt.Sprintf("%s", src))
223205
}
206+
} else {
207+
handleError(err, 1)
224208
}
225209
}
210+
211+
// Setup the CSV output
212+
w := csv.NewWriter(out)
213+
if delimiter != "" {
214+
w.Comma = datatools.NormalizeDelimiterRune(delimiter)
215+
}
216+
if err := w.Write(row); err != nil {
217+
handleError(err, 1)
218+
}
219+
w.Flush()
220+
if err := w.Error(); err != nil {
221+
handleError(err, 1)
222+
}
226223
}

0 commit comments

Comments
 (0)