-
Notifications
You must be signed in to change notification settings - Fork 60
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
feat(json): add option to write json in multiline mode #213
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | ||
|
||
// Example for: *tagvalue*, *json* | ||
|
||
// This example demonstrates loading an SPDX tag-value file from disk into memory, | ||
// and re-saving it to a different multiline json file on disk. | ||
// Run project: go run exampletvtomultilinejson.go ../sample-docs/tv/hello.spdx example.json | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spdx/tools-golang/json" | ||
"github.com/spdx/tools-golang/tagvalue" | ||
) | ||
|
||
func main() { | ||
|
||
// check that we've received the right number of arguments | ||
args := os.Args | ||
if len(args) != 3 { | ||
fmt.Printf("Usage: %v <spdx-file-in> <json-file-out>\n", args[0]) | ||
fmt.Printf(" Load SPDX tag-value file <spdx-file-in>, and\n") | ||
fmt.Printf(" save it out to multiline JSON <json-file-out>.\n") | ||
return | ||
} | ||
|
||
// open the SPDX file | ||
fileIn := args[1] | ||
r, err := os.Open(fileIn) | ||
if err != nil { | ||
fmt.Printf("Error while opening %v for reading: %v", fileIn, err) | ||
return | ||
} | ||
defer r.Close() | ||
|
||
// try to load the SPDX file's contents as a tag-value file | ||
doc, err := tagvalue.Read(r) | ||
if err != nil { | ||
fmt.Printf("Error while parsing %v: %v", args[1], err) | ||
return | ||
} | ||
|
||
// if we got here, the file is now loaded into memory. | ||
fmt.Printf("Successfully loaded %s\n", args[1]) | ||
|
||
// we can now save it back to disk, using jsonsaver. | ||
|
||
// create a new file for writing | ||
fileOut := args[2] | ||
w, err := os.Create(fileOut) | ||
if err != nil { | ||
fmt.Printf("Error while opening %v for writing: %v", fileOut, err) | ||
return | ||
} | ||
defer w.Close() | ||
|
||
// try to save the document to disk as multiline JSON file | ||
err = json.WriteMultiline(doc, w) | ||
if err != nil { | ||
fmt.Printf("Error while saving %v: %v", fileOut, err) | ||
return | ||
} | ||
|
||
// it worked | ||
fmt.Printf("Successfully saved %s\n", fileOut) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of having a second method for this, we might want to introduce a simple options interface so a user can configure this option as well as others (escaping HTML, for example). This would mean we would have to update the
Write
function. It would look something like (completely untested):... and you would call the
Write
function like:... or if you also wanted to also prevent HTML escaping (which only partially works, unfortunately; won't expand on why here):
What would you think about this approach?
On a side-note, you can just call
json.MarshalIndent(doc, "", " ")
directly in your code for this, but agree it would be nice to have this library provide some of the formatting options.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.
@kzantow Thanks for faster response!
I thought about this way, but i was not sure if you wanted to add arguments to
Write()
function, so that the otherWrite()
functions ( i mean yaml, json, etc...) have same format.I think your solution is better. Tell me if this solution works for you - I'll update this PR.
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.
Since adding options as var args allows users to continue to use the same signature they already have been:
json.Write(doc, writer)
, I think this stays consistent with the other Write functions. If you wanted to add analogous options foryaml.Write
, I wouldn't be opposed to that, but I don't think it's necessary.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.
+1 on adding vargs which wouldn't change the interface, that would work nicely!
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.
Added changes in 755da28