-
Notifications
You must be signed in to change notification settings - Fork 9
/
package.go
97 lines (76 loc) · 2.2 KB
/
package.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package schema
import (
"fmt"
"strings"
"namespacelabs.dev/foundation/internal/fnerrors"
)
func MakePackageSingleRef(pkg PackageName) *PackageRef {
return &PackageRef{
PackageName: pkg.GetPackageName(),
}
}
func MakePackageRef(pkg PackageName, name string) *PackageRef {
return &PackageRef{
PackageName: pkg.String(),
Name: name,
}
}
type PackageNameLike interface {
GetPackageName() string
ErrorLocation() string
}
// Parses from a canonical string representation.
func ParsePackageRef(owner PackageNameLike, ref string) (*PackageRef, error) {
if ref == "" {
return nil, fnerrors.NewWithLocation(owner, "empty package refs are not permitted")
}
parts := strings.Split(ref, ":")
if len(parts) > 2 {
return nil, fnerrors.NewWithLocation(owner, "invalid package ref %q", ref)
}
pr := &PackageRef{}
if parts[0] == "" {
// Ref is of form ":foo", which implicitly references a name in the owning package.
pr.PackageName = owner.GetPackageName()
} else {
pr.PackageName = parts[0]
}
if len(parts) > 1 {
pr.Name = parts[1]
}
return pr, nil
}
func StrictParsePackageRef(ref string) (*PackageRef, error) {
parts := strings.Split(ref, ":")
if len(parts) != 2 {
return nil, fnerrors.New("invalid package ref %q", ref)
}
return &PackageRef{PackageName: parts[0], Name: parts[1]}, nil
}
func (n *PackageRef) AsPackageName() PackageName {
return MakePackageName(n.GetPackageName())
}
func (n *PackageRef) Equals(other *PackageRef) bool {
return n.Compare(other) == 0
}
func (n *PackageRef) Compare(other *PackageRef) int {
if n.GetPackageName() != other.GetPackageName() {
return strings.Compare(n.GetPackageName(), other.GetPackageName())
}
return strings.Compare(n.GetName(), other.GetName())
}
func (n *PackageRef) Canonical() string {
if n.GetName() == "" {
return n.GetPackageName()
}
return n.GetPackageName() + ":" + n.GetName()
}
func (n *PackageRef) ErrorLocation() string {
if n.Name == "" {
return n.PackageName
}
return fmt.Sprintf("%s: %s", n.PackageName, n.Name)
}