-
Notifications
You must be signed in to change notification settings - Fork 259
/
module_full_name.go
183 lines (163 loc) · 4.82 KB
/
module_full_name.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufmodule
import (
"errors"
"fmt"
"strings"
"github.com/bufbuild/buf/private/pkg/netext"
)
// ModuleFullName represents the full name of the Module, including its registry, owner, and name.
type ModuleFullName interface {
// String returns "registry/owner/name".
fmt.Stringer
// Registry returns the hostname of the BSR instance that this Module is contained within.
Registry() string
// Owner returns the name of the user or organization that owns this Module.
Owner() string
// Name returns the name of the Module.
Name() string
isModuleFullName()
}
// NewModuleFullName returns a new ModuleFullName for the given components.
func NewModuleFullName(
registry string,
owner string,
name string,
) (ModuleFullName, error) {
return newModuleFullName(
registry,
owner,
name,
)
}
// ParseModuleFullName parses a ModuleFullName from a string in the form "registry/owner/name".
func ParseModuleFullName(moduleFullNameString string) (ModuleFullName, error) {
// parseModuleFullNameComponents returns ParseErrors.
registry, owner, name, err := parseModuleFullNameComponents(moduleFullNameString)
if err != nil {
return nil, err
}
if err := validateModuleFullNameParameters(registry, owner, name); err != nil {
return nil, &ParseError{
typeString: "module name",
input: moduleFullNameString,
err: err,
}
}
// We don't rely on constructors for ParseErrors.
return NewModuleFullName(registry, owner, name)
}
// ModuleFullNameEqual returns true if the ModuleFullNames are equal.
func ModuleFullNameEqual(one ModuleFullName, two ModuleFullName) bool {
if (one == nil) != (two == nil) {
return false
}
if one == nil {
return true
}
return one.String() == two.String()
}
// HasModuleFullName is any type that has a ModuleFullName() function.
type HasModuleFullName interface {
// ModuleFullName returns the ModuleullName.
//
// May be empty.
ModuleFullName() ModuleFullName
}
// ModuleFullNameStringToValue maps the values that implement HasModuleFullName to a map
// from ModuleFullName string to the unique value that has this ModuleFullName.
//
// If any value has a nil ModuleFullName, this value is not added to the map. Therefore,
// for types that potentially have a nil ModuleFullName, you cannot reply on this function
// returning a map of the same length as the input values.
//
// Returns error if there are values with duplicate ModuleFullNames.
func ModuleFullNameStringToUniqueValue[T HasModuleFullName, S ~[]T](values S) (map[string]T, error) {
m := make(map[string]T, len(values))
for _, value := range values {
moduleFullName := value.ModuleFullName()
if moduleFullName == nil {
continue
}
existingValue, ok := m[moduleFullName.String()]
if ok {
return nil, fmt.Errorf(
"duplicate module names in input: %q, %q",
existingValue.ModuleFullName().String(),
moduleFullName.String(),
)
}
m[moduleFullName.String()] = value
}
return m, nil
}
// *** PRIVATE ***
type moduleFullName struct {
registry string
owner string
name string
}
func newModuleFullName(
registry string,
owner string,
name string,
) (*moduleFullName, error) {
if err := validateModuleFullNameParameters(registry, owner, name); err != nil {
return nil, err
}
return &moduleFullName{
registry: registry,
owner: owner,
name: name,
}, nil
}
func (m *moduleFullName) Registry() string {
return m.registry
}
func (m *moduleFullName) Owner() string {
return m.owner
}
func (m *moduleFullName) Name() string {
return m.name
}
func (m *moduleFullName) String() string {
return m.registry + "/" + m.owner + "/" + m.name
}
func (*moduleFullName) isModuleFullName() {}
func validateModuleFullNameParameters(
registry string,
owner string,
name string,
) error {
if registry == "" {
return errors.New("registry is empty")
}
if _, err := netext.ValidateHostname(registry); err != nil {
return fmt.Errorf("registry %q is not a valid hostname: %w", registry, err)
}
if owner == "" {
return errors.New("owner is empty")
}
if strings.Contains(owner, "/") {
return fmt.Errorf("owner %q cannot contain slashes", owner)
}
if name == "" {
return errors.New("name is empty")
}
if strings.Contains(name, "/") {
return fmt.Errorf("name %q cannot contain slashes", name)
}
return nil
}