Skip to content

Commit

Permalink
add WriteMultiline to save to multiline json format
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed May 15, 2023
1 parent 13245e0 commit 97751c7
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
68 changes: 68 additions & 0 deletions examples/14-tvtomultilinejson/exampletvtomultilinejson.go
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)
}
15 changes: 15 additions & 0 deletions json/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ func Write(doc common.AnyDocument, w io.Writer) error {

return nil
}

// WriteMultiline takes an SPDX Document and an io.Writer, and writes the document to the writer in JSON format with indents (multiline format).
func WriteMultiline(doc common.AnyDocument, w io.Writer) error {
buf, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return err
}

_, err = w.Write(buf)
if err != nil {
return err
}

return nil
}
24 changes: 24 additions & 0 deletions spdx/v2/v2_2/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ func Test_Write(t *testing.T) {
}
}

func Test_MultilineWrite(t *testing.T) {
want := example.Copy()

w := &bytes.Buffer{}

if err := json.WriteMultiline(&want, w); err != nil {
t.Errorf("Write() error = %v", err.Error())
return
}

// we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote
var got spdx.Document
err := json.ReadInto(bytes.NewReader(w.Bytes()), &got)
if err != nil {
t.Errorf("failed to parse written document: %v", err.Error())
return
}

if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
return
}
}

func Test_ShorthandFields(t *testing.T) {
contents := `{
"spdxVersion": "SPDX-2.2",
Expand Down
23 changes: 23 additions & 0 deletions spdx/v2/v2_3/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ func Test_Write(t *testing.T) {
return
}
}
func Test_MultilineWrite(t *testing.T) {
want := example.Copy()

w := &bytes.Buffer{}

if err := json.WriteMultiline(&want, w); err != nil {
t.Errorf("Write() error = %v", err.Error())
return
}

// we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote
var got spdx.Document
err := json.ReadInto(bytes.NewReader(w.Bytes()), &got)
if err != nil {
t.Errorf("failed to parse written document: %v", err.Error())
return
}

if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
return
}
}

func Test_ShorthandFields(t *testing.T) {
contents := `{
Expand Down

0 comments on commit 97751c7

Please sign in to comment.