From d075c5795602565045414624eb2c311813d752fd Mon Sep 17 00:00:00 2001 From: Robert Fletcher Date: Thu, 15 Aug 2024 15:39:39 +0100 Subject: [PATCH] add Urn getter method to Air struct --- air.go | 29 +++++++++++++++++++++++++++++ air_test.go | 6 ++++++ 2 files changed, 35 insertions(+) diff --git a/air.go b/air.go index f5b6a42..da401d7 100644 --- a/air.go +++ b/air.go @@ -16,6 +16,35 @@ type Air struct { Version string Layer string Format string + urn string +} + +// Get the AIR URN that this struct represents. +func (a *Air) Urn() string { + if a.urn != "" { + return a.urn + } + + var builder strings.Builder + builder.WriteString(fmt.Sprintf("urn:air:%s:%s:%s:%s", a.Ecosystem, a.Type, a.Source, a.Id)) + + if a.Version != "" { + builder.WriteByte('@') + builder.WriteString(a.Version) + } + + if a.Layer != "" { + builder.WriteByte(':') + builder.WriteString(a.Layer) + } + + if a.Format != "" { + builder.WriteByte('.') + builder.WriteString(a.Format) + } + + a.urn = builder.String() + return a.urn } // Create a new AirResource from a string e.g. urn:air:sd1:model:civitai:2421@43533 diff --git a/air_test.go b/air_test.go index d147af3..f2744a9 100644 --- a/air_test.go +++ b/air_test.go @@ -69,8 +69,14 @@ func TestValidAirValues(t *testing.T) { if err != nil { panic(err) } + if *parsedResult != expectedResult { t.Fatalf("expected: %v got: %v", expectedResult, parsedResult) } + + if parsedResult.Urn() != urn { + t.Fatalf("urn mismatch. expect: '%s' got '%s'", urn, parsedResult.Urn()) + } + } }