Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding ndjson format #218

Merged
merged 6 commits into from
Oct 9, 2023
Merged
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
31 changes: 26 additions & 5 deletions cli/cmd/transformCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
}
schema string
input string
stream bool
)

func init() {
Expand All @@ -39,6 +40,8 @@ func init() {

transformCmd.Flags().StringVarP(
&input, "input", "i", "", "input file (optional; if not specified, stdin/pipe is used)")
transformCmd.Flags().BoolVarP(
&stream, "stream", "", false, "if specified, each record will be a standalone/full JSON blob and printed out immediately once transform is done")
}

func openFile(label string, filepath string) (io.ReadCloser, error) {
Expand Down Expand Up @@ -86,22 +89,40 @@ func doTransform() error {
if err != nil {
return "", err
}

s := string(b)
if stream {
return s, nil
}

return strings.Join(
strs.NoErrMapSlice(
strings.Split(jsons.BPJ(string(b)), "\n"),
strings.Split(jsons.BPJ(s), "\n"),
func(s string) string { return "\t" + s }),
"\n"), nil
}

record, err := doOne()
if err == io.EOF {
fmt.Println("[]")
if !stream {
fmt.Println("[]")
}
return nil
}
if err != nil {
return err
}
fmt.Printf("[\n%s", record)

lparen := "[\n%s"
delim := ",\n%s"
rparen := "\n]"
if stream {
lparen = "%s"
delim = "\n%s"
rparen = ""
}

fmt.Printf(lparen, record)
for {
record, err = doOne()
if err == io.EOF {
Expand All @@ -110,8 +131,8 @@ func doTransform() error {
if err != nil {
return err
}
fmt.Printf(",\n%s", record)
fmt.Printf(delim, record)
}
fmt.Println("\n]")
fmt.Println(rparen)
return nil
}
Loading