diff options
author | Brandon Lum <lumjjb@gmail.com> | 2022-06-06 10:42:27 -0400 |
---|---|---|
committer | Brandon Lum <lumjjb@gmail.com> | 2022-06-06 10:42:27 -0400 |
commit | 41d2272711255f5a25e16e3507ec3318bc550189 (patch) | |
tree | ace557ed0ffe3fab18f721744448de5936f23976 /spdx/common/annotation.go | |
parent | a532726dbb7a38d0f714075e9a1f1df4cae60230 (diff) | |
download | spdx-tools-41d2272711255f5a25e16e3507ec3318bc550189.tar.gz |
convert spdx structs to versioned pkgs
Signed-off-by: Brandon Lum <lumjjb@gmail.com>
Diffstat (limited to 'spdx/common/annotation.go')
-rw-r--r-- | spdx/common/annotation.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/spdx/common/annotation.go b/spdx/common/annotation.go new file mode 100644 index 0000000..e77d7b7 --- /dev/null +++ b/spdx/common/annotation.go @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package common + +import ( + "encoding/json" + "fmt" + "strings" +) + +type Annotator struct { + Annotator string + // including AnnotatorType: one of "Person", "Organization" or "Tool" + AnnotatorType string +} + +// UnmarshalJSON takes an annotator in the typical one-line format and parses it into an Annotator struct. +// This function is also used when unmarshalling YAML +func (a *Annotator) UnmarshalJSON(data []byte) error { + // annotator will simply be a string + annotatorStr := string(data) + annotatorStr = strings.Trim(annotatorStr, "\"") + + annotatorFields := strings.SplitN(annotatorStr, ": ", 2) + + if len(annotatorFields) != 2 { + return fmt.Errorf("failed to parse Annotator '%s'", annotatorStr) + } + + a.AnnotatorType = annotatorFields[0] + a.Annotator = annotatorFields[1] + + return nil +} + +// MarshalJSON converts the receiver into a slice of bytes representing an Annotator in string form. +// This function is also used when marshalling to YAML +func (a Annotator) MarshalJSON() ([]byte, error) { + if a.Annotator != "" { + return json.Marshal(fmt.Sprintf("%s: %s", a.AnnotatorType, a.Annotator)) + } + + return []byte{}, nil +} |