-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Evan Lezar <[email protected]>
- Loading branch information
Showing
21 changed files
with
886 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Copyright © 2024 The CDI Authors | ||
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 validator | ||
|
||
import "errors" | ||
|
||
// Validators as constants. | ||
const ( | ||
Default = defaultValidator("default") | ||
Disabled = disabledValidator("disabled") | ||
) | ||
|
||
// ErrInvalid can be returned if CDI spec validation fails. | ||
var ErrInvalid = errors.New("invalid") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module tags.cncf.io/container-device-interface/api/validator | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.7.0 | ||
tags.cncf.io/container-device-interface/specs-go v0.8.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
golang.org/x/mod v0.19.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
replace tags.cncf.io/container-device-interface/specs-go => ../../specs-go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= | ||
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
Copyright © 2024 The CDI Authors | ||
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 validator | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ValidateKind checks the validity of a CDI kind. | ||
// The syntax for a device kind“ is | ||
// | ||
// "<vendor>/<class>" | ||
func ValidateKind(kind string) error { | ||
parts := strings.SplitN(kind, "/", 2) | ||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
return fmt.Errorf("kind %s does not contain a / %w", kind, ErrInvalid) | ||
} | ||
if err := ValidateVendorName(parts[0]); err != nil { | ||
return err | ||
} | ||
if err := ValidateClassName(parts[1]); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateVendorName checks the validity of a vendor name. | ||
// A vendor name may contain the following ASCII characters: | ||
// - upper- and lowercase letters ('A'-'Z', 'a'-'z') | ||
// - digits ('0'-'9') | ||
// - underscore, dash, and dot ('_', '-', and '.') | ||
func ValidateVendorName(vendor string) error { | ||
err := validateVendorOrClassName(vendor) | ||
if err != nil { | ||
err = fmt.Errorf("invalid vendor. %w", err) | ||
} | ||
return err | ||
} | ||
|
||
// ValidateClassName checks the validity of class name. | ||
// A class name may contain the following ASCII characters: | ||
// - upper- and lowercase letters ('A'-'Z', 'a'-'z') | ||
// - digits ('0'-'9') | ||
// - underscore, dash, and dot ('_', '-', and '.') | ||
func ValidateClassName(class string) error { | ||
err := validateVendorOrClassName(class) | ||
if err != nil { | ||
err = fmt.Errorf("invalid class. %w", err) | ||
} | ||
return err | ||
} | ||
|
||
// validateVendorOrClassName checks the validity of vendor or class name. | ||
// A name may contain the following ASCII characters: | ||
// - upper- and lowercase letters ('A'-'Z', 'a'-'z') | ||
// - digits ('0'-'9') | ||
// - underscore, dash, and dot ('_', '-', and '.') | ||
func validateVendorOrClassName(name string) error { | ||
if name == "" { | ||
return fmt.Errorf("empty name") | ||
} | ||
if !IsLetter(rune(name[0])) { | ||
return fmt.Errorf("%q, should start with letter", name) | ||
} | ||
for _, c := range string(name[1 : len(name)-1]) { | ||
switch { | ||
case IsAlphaNumeric(c): | ||
case c == '_' || c == '-' || c == '.': | ||
default: | ||
return fmt.Errorf("invalid character '%c' in name %q", | ||
c, name) | ||
} | ||
} | ||
if !IsAlphaNumeric(rune(name[len(name)-1])) { | ||
return fmt.Errorf("%q, should end with a letter or digit", name) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ValidateDeviceName checks the validity of a device name. | ||
// A device name may contain the following ASCII characters: | ||
// - upper- and lowercase letters ('A'-'Z', 'a'-'z') | ||
// - digits ('0'-'9') | ||
// - underscore, dash, dot, colon ('_', '-', '.', ':') | ||
func ValidateDeviceName(name string) error { | ||
if name == "" { | ||
return fmt.Errorf("invalid (empty) device name") | ||
} | ||
if !IsAlphaNumeric(rune(name[0])) { | ||
return fmt.Errorf("invalid class %q, should start with a letter or digit", name) | ||
} | ||
if len(name) == 1 { | ||
return nil | ||
} | ||
for _, c := range string(name[1 : len(name)-1]) { | ||
switch { | ||
case IsAlphaNumeric(c): | ||
case c == '_' || c == '-' || c == '.' || c == ':': | ||
default: | ||
return fmt.Errorf("invalid character '%c' in device name %q", | ||
c, name) | ||
} | ||
} | ||
if !IsAlphaNumeric(rune(name[len(name)-1])) { | ||
return fmt.Errorf("invalid name %q, should end with a letter or digit", name) | ||
} | ||
return nil | ||
} | ||
|
||
// IsLetter reports whether the rune is a letter. | ||
func IsLetter(c rune) bool { | ||
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') | ||
} | ||
|
||
// IsDigit reports whether the rune is a digit. | ||
func IsDigit(c rune) bool { | ||
return '0' <= c && c <= '9' | ||
} | ||
|
||
// IsAlphaNumeric reports whether the rune is a letter or digit. | ||
func IsAlphaNumeric(c rune) bool { | ||
return IsLetter(c) || IsDigit(c) | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.