@@ -26,14 +26,10 @@ import (
26
26
//
27
27
// "<vendor>/<class>=<name>".
28
28
//
29
- // A valid vendor name may contain the following runes:
29
+ // A valid vendor and class name may contain the following runes:
30
30
//
31
31
// 'A'-'Z', 'a'-'z', '0'-'9', '.', '-', '_'.
32
32
//
33
- // A valid class name may contain the following runes:
34
- //
35
- // 'A'-'Z', 'a'-'z', '0'-'9', '-', '_'.
36
- //
37
33
// A valid device name may containe the following runes:
38
34
//
39
35
// 'A'-'Z', 'a'-'z', '0'-'9', '-', '_', '.', ':'
@@ -122,52 +118,51 @@ func ParseQualifier(kind string) (string, string) {
122
118
// - digits ('0'-'9')
123
119
// - underscore, dash, and dot ('_', '-', and '.')
124
120
func ValidateVendorName (vendor string ) error {
125
- if vendor == "" {
126
- return fmt .Errorf ("invalid (empty) vendor name" )
127
- }
128
- if ! IsLetter (rune (vendor [0 ])) {
129
- return fmt .Errorf ("invalid vendor %q, should start with letter" , vendor )
130
- }
131
- for _ , c := range string (vendor [1 : len (vendor )- 1 ]) {
132
- switch {
133
- case IsAlphaNumeric (c ):
134
- case c == '_' || c == '-' || c == '.' :
135
- default :
136
- return fmt .Errorf ("invalid character '%c' in vendor name %q" ,
137
- c , vendor )
138
- }
139
- }
140
- if ! IsAlphaNumeric (rune (vendor [len (vendor )- 1 ])) {
141
- return fmt .Errorf ("invalid vendor %q, should end with a letter or digit" , vendor )
121
+ err := validateVendorOrClassName (vendor )
122
+ if err != nil {
123
+ err = fmt .Errorf ("invalid vendor. %w" , err )
142
124
}
143
-
144
- return nil
125
+ return err
145
126
}
146
127
147
128
// ValidateClassName checks the validity of class name.
148
129
// A class name may contain the following ASCII characters:
149
130
// - upper- and lowercase letters ('A'-'Z', 'a'-'z')
150
131
// - digits ('0'-'9')
151
- // - underscore and dash ('_', '-')
132
+ // - underscore, dash, and dot ('_', '-', and '. ')
152
133
func ValidateClassName (class string ) error {
153
- if class == "" {
154
- return fmt .Errorf ("invalid (empty) device class" )
134
+ err := validateVendorOrClassName (class )
135
+ if err != nil {
136
+ err = fmt .Errorf ("invalid class. %w" , err )
155
137
}
156
- if ! IsLetter (rune (class [0 ])) {
157
- return fmt .Errorf ("invalid class %q, should start with letter" , class )
138
+ return err
139
+ }
140
+
141
+ // validateVendorOrClassName checks the validity of vendor or class name.
142
+ // A name may contain the following ASCII characters:
143
+ // - upper- and lowercase letters ('A'-'Z', 'a'-'z')
144
+ // - digits ('0'-'9')
145
+ // - underscore, dash, and dot ('_', '-', and '.')
146
+ func validateVendorOrClassName (name string ) error {
147
+ if name == "" {
148
+ return fmt .Errorf ("empty name" )
158
149
}
159
- for _ , c := range string (class [1 : len (class )- 1 ]) {
150
+ if ! IsLetter (rune (name [0 ])) {
151
+ return fmt .Errorf ("%q, should start with letter" , name )
152
+ }
153
+ for _ , c := range string (name [1 : len (name )- 1 ]) {
160
154
switch {
161
155
case IsAlphaNumeric (c ):
162
- case c == '_' || c == '-' :
156
+ case c == '_' || c == '-' || c == '.' :
163
157
default :
164
- return fmt .Errorf ("invalid character '%c' in device class %q" ,
165
- c , class )
158
+ return fmt .Errorf ("invalid character '%c' in name %q" ,
159
+ c , name )
166
160
}
167
161
}
168
- if ! IsAlphaNumeric (rune (class [len (class )- 1 ])) {
169
- return fmt .Errorf ("invalid class %q, should end with a letter or digit" , class )
162
+ if ! IsAlphaNumeric (rune (name [len (name )- 1 ])) {
163
+ return fmt .Errorf ("%q, should end with a letter or digit" , name )
170
164
}
165
+
171
166
return nil
172
167
}
173
168
0 commit comments