Skip to content

Commit

Permalink
adding ndjson format
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-sherpa committed Sep 6, 2023
1 parent 79a540b commit 455c053
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
27 changes: 23 additions & 4 deletions cli/cmd/transformCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func doTransform() error {
if err != nil {
return "", err
}

if schema.Header().ParserSettings.NDJSON {
return string(b), nil
}

return strings.Join(
strs.NoErrMapSlice(
strings.Split(jsons.BPJ(string(b)), "\n"),
Expand All @@ -95,13 +100,27 @@ func doTransform() error {

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

start := "[\n%s"
middle := ",\n%s"
end := "\n]"
if schema.Header().ParserSettings.NDJSON {
start = "%s"
middle = "\n%s"
end = ""
}

fmt.Printf(start, record)
for {
record, err = doOne()
if err == io.EOF {
Expand All @@ -110,8 +129,8 @@ func doTransform() error {
if err != nil {
return err
}
fmt.Printf(",\n%s", record)
fmt.Printf(middle, record)
}
fmt.Println("\n]")
fmt.Println(end)
return nil
}
1 change: 1 addition & 0 deletions header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ParserSettings struct {
Version string `json:"version,omitempty"`
FileFormatType string `json:"file_format_type,omitempty"`
Encoding *string `json:"encoding,omitempty"`
NDJSON bool `json:"ndjson,omitempty"`
}

const (
Expand Down
3 changes: 2 additions & 1 deletion validation/parserSettings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion validation/parserSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"encoding": {
"type": "string",
"enum": [ "utf-8", "iso-8859-1", "windows-1252" ]
}
},
"ndjson": { "type": "boolean" }
},
"required": [ "version", "file_format_type" ],
"additionalProperties": false
Expand Down

0 comments on commit 455c053

Please sign in to comment.