Skip to content

Commit

Permalink
add Urn getter method to Air struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rfletchr committed Aug 15, 2024
1 parent 0aebb08 commit d075c57
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions air.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions air_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

}
}

0 comments on commit d075c57

Please sign in to comment.