17
17
package parser
18
18
19
19
import (
20
- "fmt"
21
20
"strings"
21
+
22
+ "tags.cncf.io/container-device-interface/api/producer"
22
23
)
23
24
24
25
// QualifiedName returns the qualified name for a device.
@@ -39,7 +40,7 @@ func QualifiedName(vendor, class, name string) string {
39
40
40
41
// IsQualifiedName tests if a device name is qualified.
41
42
func IsQualifiedName (device string ) bool {
42
- _ , _ , _ , err := ParseQualifiedName (device )
43
+ err := producer . ValidateQualifiedName (device )
43
44
return err == nil
44
45
}
45
46
@@ -49,52 +50,15 @@ func IsQualifiedName(device string) bool {
49
50
// class are returned as empty, together with the verbatim input as the
50
51
// name and an error describing the reason for failure.
51
52
func ParseQualifiedName (device string ) (string , string , string , error ) {
52
- vendor , class , name := ParseDevice (device )
53
-
54
- if vendor == "" {
55
- return "" , "" , device , fmt .Errorf ("unqualified device %q, missing vendor" , device )
56
- }
57
- if class == "" {
58
- return "" , "" , device , fmt .Errorf ("unqualified device %q, missing class" , device )
59
- }
60
- if name == "" {
61
- return "" , "" , device , fmt .Errorf ("unqualified device %q, missing device name" , device )
62
- }
63
-
64
- if err := ValidateVendorName (vendor ); err != nil {
65
- return "" , "" , device , fmt .Errorf ("invalid device %q: %w" , device , err )
66
- }
67
- if err := ValidateClassName (class ); err != nil {
68
- return "" , "" , device , fmt .Errorf ("invalid device %q: %w" , device , err )
69
- }
70
- if err := ValidateDeviceName (name ); err != nil {
71
- return "" , "" , device , fmt .Errorf ("invalid device %q: %w" , device , err )
72
- }
73
-
74
- return vendor , class , name , nil
53
+ return producer .ParseFullyQualifiedName (device )
75
54
}
76
55
77
56
// ParseDevice tries to split a device name into vendor, class, and name.
78
57
// If this fails, for instance in the case of unqualified device names,
79
58
// ParseDevice returns an empty vendor and class together with name set
80
59
// to the verbatim input.
81
60
func ParseDevice (device string ) (string , string , string ) {
82
- if device == "" || device [0 ] == '/' {
83
- return "" , "" , device
84
- }
85
-
86
- parts := strings .SplitN (device , "=" , 2 )
87
- if len (parts ) != 2 || parts [0 ] == "" || parts [1 ] == "" {
88
- return "" , "" , device
89
- }
90
-
91
- name := parts [1 ]
92
- vendor , class := ParseQualifier (parts [0 ])
93
- if vendor == "" {
94
- return "" , "" , device
95
- }
96
-
97
- return vendor , class , name
61
+ return producer .ParseDevice (device )
98
62
}
99
63
100
64
// ParseQualifier splits a device qualifier into vendor and class.
@@ -118,11 +82,7 @@ func ParseQualifier(kind string) (string, string) {
118
82
// - digits ('0'-'9')
119
83
// - underscore, dash, and dot ('_', '-', and '.')
120
84
func ValidateVendorName (vendor string ) error {
121
- err := validateVendorOrClassName (vendor )
122
- if err != nil {
123
- err = fmt .Errorf ("invalid vendor. %w" , err )
124
- }
125
- return err
85
+ return producer .ValidateVendorName (vendor )
126
86
}
127
87
128
88
// ValidateClassName checks the validity of class name.
@@ -131,39 +91,7 @@ func ValidateVendorName(vendor string) error {
131
91
// - digits ('0'-'9')
132
92
// - underscore, dash, and dot ('_', '-', and '.')
133
93
func ValidateClassName (class string ) error {
134
- err := validateVendorOrClassName (class )
135
- if err != nil {
136
- err = fmt .Errorf ("invalid class. %w" , err )
137
- }
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" )
149
- }
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 ]) {
154
- switch {
155
- case IsAlphaNumeric (c ):
156
- case c == '_' || c == '-' || c == '.' :
157
- default :
158
- return fmt .Errorf ("invalid character '%c' in name %q" ,
159
- c , name )
160
- }
161
- }
162
- if ! IsAlphaNumeric (rune (name [len (name )- 1 ])) {
163
- return fmt .Errorf ("%q, should end with a letter or digit" , name )
164
- }
165
-
166
- return nil
94
+ return producer .ValidateClassName (class )
167
95
}
168
96
169
97
// ValidateDeviceName checks the validity of a device name.
@@ -172,28 +100,7 @@ func validateVendorOrClassName(name string) error {
172
100
// - digits ('0'-'9')
173
101
// - underscore, dash, dot, colon ('_', '-', '.', ':')
174
102
func ValidateDeviceName (name string ) error {
175
- if name == "" {
176
- return fmt .Errorf ("invalid (empty) device name" )
177
- }
178
- if ! IsAlphaNumeric (rune (name [0 ])) {
179
- return fmt .Errorf ("invalid class %q, should start with a letter or digit" , name )
180
- }
181
- if len (name ) == 1 {
182
- return nil
183
- }
184
- for _ , c := range string (name [1 : len (name )- 1 ]) {
185
- switch {
186
- case IsAlphaNumeric (c ):
187
- case c == '_' || c == '-' || c == '.' || c == ':' :
188
- default :
189
- return fmt .Errorf ("invalid character '%c' in device name %q" ,
190
- c , name )
191
- }
192
- }
193
- if ! IsAlphaNumeric (rune (name [len (name )- 1 ])) {
194
- return fmt .Errorf ("invalid name %q, should end with a letter or digit" , name )
195
- }
196
- return nil
103
+ return producer .ValidateDeviceName (name )
197
104
}
198
105
199
106
// IsLetter reports whether the rune is a letter.
0 commit comments