From 285540f4746d7145a7a56d6787daa9d5b2a9b3c2 Mon Sep 17 00:00:00 2001 From: Thomas Fossati Date: Thu, 18 Feb 2021 11:53:21 +0000 Subject: [PATCH] Add New() method to Rel (#9) Fixes #8 --- example_test.go | 12 ++++++------ rel.go | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/example_test.go b/example_test.go index 2cf1b04..46e474d 100644 --- a/example_test.go +++ b/example_test.go @@ -18,16 +18,16 @@ func Example_useAPIToBuildPSAEndorsementSoftwareBundle() { _ = tag.AddEntity(*entity) // make links and append them to tag - link, _ := NewLink("example.acme.roadrunner-hw-v1-0-0", Rel{"psa-rot-compound"}) + link, _ := NewLink("example.acme.roadrunner-hw-v1-0-0", *NewRel("psa-rot-compound")) _ = tag.AddLink(*link) - link, _ = NewLink("example.acme.roadrunner-sw-bl-v1-0-0", Rel{RelComponent}) + link, _ = NewLink("example.acme.roadrunner-sw-bl-v1-0-0", *NewRel(RelComponent)) _ = tag.AddLink(*link) - link, _ = NewLink("example.acme.roadrunner-sw-prot-v1-0-0", Rel{RelComponent}) + link, _ = NewLink("example.acme.roadrunner-sw-prot-v1-0-0", *NewRel(RelComponent)) _ = tag.AddLink(*link) - link, _ = NewLink("example.acme.roadrunner-sw-arot-v1-0-0", Rel{RelComponent}) + link, _ = NewLink("example.acme.roadrunner-sw-arot-v1-0-0", *NewRel(RelComponent)) _ = tag.AddLink(*link) // encode tag to JSON @@ -71,7 +71,7 @@ func Example_useAPIToBuildPSAEndorsementSoftwareComponent() { _ = tag.AddPayload(*payload) // make link to the HW RoT - link, _ := NewLink("example.acme.roadrunner-hw-v1-0-0", Rel{"psa-rot-compound"}) + link, _ := NewLink("example.acme.roadrunner-hw-v1-0-0", *NewRel("psa-rot-compound")) _ = tag.AddLink(*link) // encode tag to JSON @@ -102,7 +102,7 @@ func Example_completePrimaryTag() { _ = entity.SetRegID("mycoyote.com") _ = tag.AddEntity(*entity) - link, _ := NewLink("www.gnu.org/licenses/gpl.txt", Rel{"license"}) + link, _ := NewLink("www.gnu.org/licenses/gpl.txt", *NewRel("license")) _ = tag.AddLink(*link) meta := SoftwareMeta{ diff --git a/rel.go b/rel.go index 74ea9d8..6cd88d6 100644 --- a/rel.go +++ b/rel.go @@ -84,6 +84,14 @@ var ( } ) +// NewRel returns a Rel initialized with the supplied value v +func NewRel(v interface{}) *Rel { + if isStringOrCode(v, "rel") != nil { + return nil + } + return &Rel{v} +} + // String returns the value of the Rel receiver as a string func (r Rel) String() string { return codeStringer(r.val, relToString, "rel")