-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
adding ndjson format #218
Changes from 2 commits
455c053
188e01d
8cfd180
c362e55
bcf00e7
b26cb6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ var ( | |
} | ||
schema string | ||
input string | ||
ndjson bool | ||
) | ||
|
||
func init() { | ||
|
@@ -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( | ||
&ndjson, "ndjson", "", false, "change the output format to ndjson") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jf-tech is this what you meant with a command line option or did you want something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about changing it to a long flag only |
||
} | ||
|
||
func openFile(label string, filepath string) (io.ReadCloser, error) { | ||
|
@@ -86,6 +89,11 @@ func doTransform() error { | |
if err != nil { | ||
return "", err | ||
} | ||
|
||
if ndjson { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
return string(b), nil | ||
} | ||
|
||
return strings.Join( | ||
strs.NoErrMapSlice( | ||
strings.Split(jsons.BPJ(string(b)), "\n"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look the code can be a little bit optimized here:
since |
||
|
@@ -95,13 +103,27 @@ func doTransform() error { | |
|
||
record, err := doOne() | ||
if err == io.EOF { | ||
fmt.Println("[]") | ||
if ndjson { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
fmt.Println("") | ||
} else { | ||
fmt.Println("[]") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well this is just written weirdly, why not something inline with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Println writes a new line even with a blank string, should we just leave it blank? |
||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("[\n%s", record) | ||
|
||
start := "[\n%s" | ||
middle := ",\n%s" | ||
end := "\n]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be able to come up with better names or create a method on parser settings that returns a struct that encapsulates these variables There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably I'm fine with the current implementation you proposed. But do need to add unit tests - we tried very hard to keep coverage at 100%. I'm thinking about adding a utility into https://github.com/jf-tech/go-corelib/tree/master/jsons which this omniparser uses extensively, that the utility is a json writer and encapsulates the functionalities you implement here. But that's a later optimization/refactoring. No need for this time. Just unittest coverage. |
||
if ndjson { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stream |
||
start = "%s" | ||
middle = "\n%s" | ||
end = "" | ||
} | ||
|
||
fmt.Printf(start, record) | ||
for { | ||
record, err = doOne() | ||
if err == io.EOF { | ||
|
@@ -110,8 +132,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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought we said changing
ndjson
tostream
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok I changed the command option name but not in code, I'll make that change now