-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathidentifier.go
60 lines (47 loc) · 1.14 KB
/
identifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package replicate
import (
"errors"
"fmt"
"strings"
)
var (
ErrInvalidIdentifier = errors.New("invalid identifier, it must be in the format \"owner/name\" or \"owner/name:version\"")
)
// Identifier represents a reference to a Replicate model with an optional version.
type Identifier struct {
// Owner is the username of the model owner.
Owner string
// Name is the name of the model.
Name string
// Version is the version of the model.
Version *string
}
func ParseIdentifier(identifier string) (*Identifier, error) {
parts := strings.Split(identifier, "/")
if len(parts) != 2 {
return nil, ErrInvalidIdentifier
}
var name, owner string
var version *string
owner = parts[0]
name = parts[1]
subparts := strings.Split(name, ":")
if len(subparts) > 1 {
name = subparts[0]
version = &subparts[1]
}
if owner == "" || name == "" {
return nil, ErrInvalidIdentifier
}
return &Identifier{
Owner: owner,
Name: name,
Version: version,
}, nil
}
func (i *Identifier) String() string {
if i.Version == nil {
return fmt.Sprintf("%s/%s", i.Owner, i.Name)
}
return fmt.Sprintf("%s/%s:%s", i.Owner, i.Name, *i.Version)
}