|
17 | 17 | package cdi
|
18 | 18 |
|
19 | 19 | import (
|
20 |
| - "fmt" |
21 |
| - "strings" |
| 20 | + "github.com/container-orchestrated-devices/container-device-interface/pkg/parser" |
22 | 21 | )
|
23 | 22 |
|
24 | 23 | // QualifiedName returns the qualified name for a device.
|
25 | 24 | // The syntax for a qualified device names is
|
26 |
| -// "<vendor>/<class>=<name>". |
| 25 | +// |
| 26 | +// "<vendor>/<class>=<name>". |
| 27 | +// |
27 | 28 | // A valid vendor name may contain the following runes:
|
28 |
| -// 'A'-'Z', 'a'-'z', '0'-'9', '.', '-', '_'. |
| 29 | +// |
| 30 | +// 'A'-'Z', 'a'-'z', '0'-'9', '.', '-', '_'. |
| 31 | +// |
29 | 32 | // A valid class name may contain the following runes:
|
30 |
| -// 'A'-'Z', 'a'-'z', '0'-'9', '-', '_'. |
| 33 | +// |
| 34 | +// 'A'-'Z', 'a'-'z', '0'-'9', '-', '_'. |
| 35 | +// |
31 | 36 | // A valid device name may containe the following runes:
|
32 |
| -// 'A'-'Z', 'a'-'z', '0'-'9', '-', '_', '.', ':' |
| 37 | +// |
| 38 | +// 'A'-'Z', 'a'-'z', '0'-'9', '-', '_', '.', ':' |
| 39 | +// |
| 40 | +// Deprecated: use parser.QualifiedName instead |
33 | 41 | func QualifiedName(vendor, class, name string) string {
|
34 |
| - return vendor + "/" + class + "=" + name |
| 42 | + return parser.QualifiedName(vendor, class, name) |
35 | 43 | }
|
36 | 44 |
|
37 | 45 | // IsQualifiedName tests if a device name is qualified.
|
| 46 | +// |
| 47 | +// Deprecated: use parser.IsQualifiedName instead |
38 | 48 | func IsQualifiedName(device string) bool {
|
39 |
| - _, _, _, err := ParseQualifiedName(device) |
40 |
| - return err == nil |
| 49 | + return parser.IsQualifiedName(device) |
41 | 50 | }
|
42 | 51 |
|
43 | 52 | // ParseQualifiedName splits a qualified name into device vendor, class,
|
44 | 53 | // and name. If the device fails to parse as a qualified name, or if any
|
45 | 54 | // of the split components fail to pass syntax validation, vendor and
|
46 | 55 | // class are returned as empty, together with the verbatim input as the
|
47 | 56 | // name and an error describing the reason for failure.
|
| 57 | +// |
| 58 | +// Deprecated: use parser.ParseQualifiedName instead |
48 | 59 | func ParseQualifiedName(device string) (string, string, string, error) {
|
49 |
| - vendor, class, name := ParseDevice(device) |
50 |
| - |
51 |
| - if vendor == "" { |
52 |
| - return "", "", device, fmt.Errorf("unqualified device %q, missing vendor", device) |
53 |
| - } |
54 |
| - if class == "" { |
55 |
| - return "", "", device, fmt.Errorf("unqualified device %q, missing class", device) |
56 |
| - } |
57 |
| - if name == "" { |
58 |
| - return "", "", device, fmt.Errorf("unqualified device %q, missing device name", device) |
59 |
| - } |
60 |
| - |
61 |
| - if err := ValidateVendorName(vendor); err != nil { |
62 |
| - return "", "", device, fmt.Errorf("invalid device %q: %w", device, err) |
63 |
| - } |
64 |
| - if err := ValidateClassName(class); err != nil { |
65 |
| - return "", "", device, fmt.Errorf("invalid device %q: %w", device, err) |
66 |
| - } |
67 |
| - if err := ValidateDeviceName(name); err != nil { |
68 |
| - return "", "", device, fmt.Errorf("invalid device %q: %w", device, err) |
69 |
| - } |
70 |
| - |
71 |
| - return vendor, class, name, nil |
| 60 | + return parser.ParseQualifiedName(device) |
72 | 61 | }
|
73 | 62 |
|
74 | 63 | // ParseDevice tries to split a device name into vendor, class, and name.
|
75 | 64 | // If this fails, for instance in the case of unqualified device names,
|
76 | 65 | // ParseDevice returns an empty vendor and class together with name set
|
77 | 66 | // to the verbatim input.
|
| 67 | +// |
| 68 | +// Deprecated: use parser.ParseDevice instead |
78 | 69 | func ParseDevice(device string) (string, string, string) {
|
79 |
| - if device == "" || device[0] == '/' { |
80 |
| - return "", "", device |
81 |
| - } |
82 |
| - |
83 |
| - parts := strings.SplitN(device, "=", 2) |
84 |
| - if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
85 |
| - return "", "", device |
86 |
| - } |
87 |
| - |
88 |
| - name := parts[1] |
89 |
| - vendor, class := ParseQualifier(parts[0]) |
90 |
| - if vendor == "" { |
91 |
| - return "", "", device |
92 |
| - } |
93 |
| - |
94 |
| - return vendor, class, name |
| 70 | + return parser.ParseDevice(device) |
95 | 71 | }
|
96 | 72 |
|
97 | 73 | // ParseQualifier splits a device qualifier into vendor and class.
|
98 | 74 | // The syntax for a device qualifier is
|
99 |
| -// "<vendor>/<class>" |
| 75 | +// |
| 76 | +// "<vendor>/<class>" |
| 77 | +// |
100 | 78 | // If parsing fails, an empty vendor and the class set to the
|
101 | 79 | // verbatim input is returned.
|
| 80 | +// |
| 81 | +// Deprecated: use parser.ParseQualifier instead |
102 | 82 | func ParseQualifier(kind string) (string, string) {
|
103 |
| - parts := strings.SplitN(kind, "/", 2) |
104 |
| - if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
105 |
| - return "", kind |
106 |
| - } |
107 |
| - return parts[0], parts[1] |
| 83 | + return parser.ParseQualifier(kind) |
108 | 84 | }
|
109 | 85 |
|
110 | 86 | // ValidateVendorName checks the validity of a vendor name.
|
111 | 87 | // A vendor name may contain the following ASCII characters:
|
112 | 88 | // - upper- and lowercase letters ('A'-'Z', 'a'-'z')
|
113 | 89 | // - digits ('0'-'9')
|
114 | 90 | // - underscore, dash, and dot ('_', '-', and '.')
|
| 91 | +// |
| 92 | +// Deprecated: use parser.ValidateVendorName instead |
115 | 93 | func ValidateVendorName(vendor string) error {
|
116 |
| - if vendor == "" { |
117 |
| - return fmt.Errorf("invalid (empty) vendor name") |
118 |
| - } |
119 |
| - if !isLetter(rune(vendor[0])) { |
120 |
| - return fmt.Errorf("invalid vendor %q, should start with letter", vendor) |
121 |
| - } |
122 |
| - for _, c := range string(vendor[1 : len(vendor)-1]) { |
123 |
| - switch { |
124 |
| - case isAlphaNumeric(c): |
125 |
| - case c == '_' || c == '-' || c == '.': |
126 |
| - default: |
127 |
| - return fmt.Errorf("invalid character '%c' in vendor name %q", |
128 |
| - c, vendor) |
129 |
| - } |
130 |
| - } |
131 |
| - if !isAlphaNumeric(rune(vendor[len(vendor)-1])) { |
132 |
| - return fmt.Errorf("invalid vendor %q, should end with a letter or digit", vendor) |
133 |
| - } |
134 |
| - |
135 |
| - return nil |
| 94 | + return parser.ValidateVendorName(vendor) |
136 | 95 | }
|
137 | 96 |
|
138 | 97 | // ValidateClassName checks the validity of class name.
|
139 | 98 | // A class name may contain the following ASCII characters:
|
140 | 99 | // - upper- and lowercase letters ('A'-'Z', 'a'-'z')
|
141 | 100 | // - digits ('0'-'9')
|
142 | 101 | // - underscore and dash ('_', '-')
|
| 102 | +// |
| 103 | +// Deprecated: use parser.ValidateClassName instead |
143 | 104 | func ValidateClassName(class string) error {
|
144 |
| - if class == "" { |
145 |
| - return fmt.Errorf("invalid (empty) device class") |
146 |
| - } |
147 |
| - if !isLetter(rune(class[0])) { |
148 |
| - return fmt.Errorf("invalid class %q, should start with letter", class) |
149 |
| - } |
150 |
| - for _, c := range string(class[1 : len(class)-1]) { |
151 |
| - switch { |
152 |
| - case isAlphaNumeric(c): |
153 |
| - case c == '_' || c == '-': |
154 |
| - default: |
155 |
| - return fmt.Errorf("invalid character '%c' in device class %q", |
156 |
| - c, class) |
157 |
| - } |
158 |
| - } |
159 |
| - if !isAlphaNumeric(rune(class[len(class)-1])) { |
160 |
| - return fmt.Errorf("invalid class %q, should end with a letter or digit", class) |
161 |
| - } |
162 |
| - return nil |
| 105 | + return parser.ValidateClassName(class) |
163 | 106 | }
|
164 | 107 |
|
165 | 108 | // ValidateDeviceName checks the validity of a device name.
|
166 | 109 | // A device name may contain the following ASCII characters:
|
167 | 110 | // - upper- and lowercase letters ('A'-'Z', 'a'-'z')
|
168 | 111 | // - digits ('0'-'9')
|
169 | 112 | // - underscore, dash, dot, colon ('_', '-', '.', ':')
|
| 113 | +// |
| 114 | +// Deprecated: use parser.ValidateDeviceName instead |
170 | 115 | func ValidateDeviceName(name string) error {
|
171 |
| - if name == "" { |
172 |
| - return fmt.Errorf("invalid (empty) device name") |
173 |
| - } |
174 |
| - if !isAlphaNumeric(rune(name[0])) { |
175 |
| - return fmt.Errorf("invalid class %q, should start with a letter or digit", name) |
176 |
| - } |
177 |
| - if len(name) == 1 { |
178 |
| - return nil |
179 |
| - } |
180 |
| - for _, c := range string(name[1 : len(name)-1]) { |
181 |
| - switch { |
182 |
| - case isAlphaNumeric(c): |
183 |
| - case c == '_' || c == '-' || c == '.' || c == ':': |
184 |
| - default: |
185 |
| - return fmt.Errorf("invalid character '%c' in device name %q", |
186 |
| - c, name) |
187 |
| - } |
188 |
| - } |
189 |
| - if !isAlphaNumeric(rune(name[len(name)-1])) { |
190 |
| - return fmt.Errorf("invalid name %q, should end with a letter or digit", name) |
191 |
| - } |
192 |
| - return nil |
193 |
| -} |
194 |
| - |
195 |
| -func isLetter(c rune) bool { |
196 |
| - return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') |
197 |
| -} |
198 |
| - |
199 |
| -func isDigit(c rune) bool { |
200 |
| - return '0' <= c && c <= '9' |
201 |
| -} |
202 |
| - |
203 |
| -func isAlphaNumeric(c rune) bool { |
204 |
| - return isLetter(c) || isDigit(c) |
| 116 | + return parser.ValidateDeviceName(name) |
205 | 117 | }
|
0 commit comments