diff options
author | Sadaf Ebrahimi <sadafebrahimi@google.com> | 2023-01-26 18:29:15 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-01-26 18:29:15 +0000 |
commit | b925feafab4bd54a2d608e4621a5210bc67d67c6 (patch) | |
tree | 6ea4841a079772cb06df5ec2cf99637f8d53805b /spdx/common/annotation.go | |
parent | 5f73058e15df15ab2a35ca0c9d506b4fd0a83de7 (diff) | |
parent | 8dba382b59616b372f0ee6f95fe66c942bb790e0 (diff) | |
download | spdx-tools-b925feafab4bd54a2d608e4621a5210bc67d67c6.tar.gz |
Upgrade spdx-tools to v0.4.0 am: 75f88b4895 am: f12cf92582 am: 8dba382b59
Original change: https://android-review.googlesource.com/c/platform/external/spdx-tools/+/2401868
Change-Id: I14274d30850f3af3184815b3a3820b1f744d0a1f
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.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 +} |