Skip to content

Commit

Permalink
✨ Support negative integers in marker syntax
Browse files Browse the repository at this point in the history
This adds support for negative integers in the marker syntax parser.
This is useful for markers like `+kubebuilder:validation:Minimum`.

Signed-off-by: knight42 <anonymousknight96@gmail.com>
  • Loading branch information
knight42 committed Aug 19, 2020
1 parent 6519034 commit 81b2062
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func init() {
type Maximum int

// +controllertools:marker:generateHelp:category="CRD validation"
// Minimum specifies the minimum numeric value that this field can have.
// Minimum specifies the minimum numeric value that this field can have. Negative integers are supported.
type Minimum int

// +controllertools:marker:generateHelp:category="CRD validation"
Expand Down
19 changes: 17 additions & 2 deletions pkg/markers/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,13 @@ func guessType(scanner *sc.Scanner, raw string, allowSlice bool) *Argument {
}
}

// then, integers...
if !probablyString {
if nextTok := subScanner.Scan(); nextTok == sc.Int {
nextTok := subScanner.Scan()
if nextTok == '-' {
nextTok = subScanner.Scan()
}
if nextTok == sc.Int {
return &Argument{Type: IntType}
}
}
Expand Down Expand Up @@ -481,11 +486,21 @@ func (a *Argument) parse(scanner *sc.Scanner, raw string, out reflect.Value, inS
for tok := scanner.Scan(); tok != sc.EOF; tok = scanner.Scan() {
}
case IntType:
nextChar := scanner.Peek()
isNegative := false
if nextChar == '-' {
isNegative = true
scanner.Scan() // eat the '-'
}
if !expect(scanner, sc.Int, "integer") {
return
}
// TODO(directxman12): respect the size when parsing
val, err := strconv.Atoi(scanner.TokenText())
text := scanner.TokenText()
if isNegative {
text = "-" + text
}
val, err := strconv.Atoi(text)
if err != nil {
scanner.Error(scanner, fmt.Sprintf("unable to parse integer: %v", err))
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/markers/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ var _ = Describe("Parsing", func() {
It("should support double-quoted strings", argParseTestCase{arg: Argument{Type: StringType}, raw: `"some; string, \nhere"`, output: "some; string, \nhere"}.Run)
It("should support raw strings", argParseTestCase{arg: Argument{Type: StringType}, raw: "`some; string, \\nhere`", output: `some; string, \nhere`}.Run)
It("should support integers", argParseTestCase{arg: Argument{Type: IntType}, raw: "42", output: 42}.Run)
XIt("should support negative integers", argParseTestCase{arg: Argument{Type: IntType}, raw: "-42", output: -42}.Run)
It("should support negative integers", argParseTestCase{arg: Argument{Type: IntType}, raw: "-42", output: -42}.Run)
It("should support false booleans", argParseTestCase{arg: Argument{Type: BoolType}, raw: "false", output: false}.Run)
It("should support true booleans", argParseTestCase{arg: Argument{Type: BoolType}, raw: "true", output: true}.Run)

Expand All @@ -194,7 +194,7 @@ var _ = Describe("Parsing", func() {
It("should support double-quoted strings", argParseTestCase{arg: anyArg, raw: `"some; string, \nhere"`, output: "some; string, \nhere"}.Run)
It("should support raw strings", argParseTestCase{arg: anyArg, raw: "`some; string, \\nhere`", output: `some; string, \nhere`}.Run)
It("should support integers", argParseTestCase{arg: anyArg, raw: "42", output: 42}.Run)
XIt("should support negative integers", argParseTestCase{arg: anyArg, raw: "-42", output: -42}.Run)
It("should support negative integers", argParseTestCase{arg: anyArg, raw: "-42", output: -42}.Run)
It("should support false booleans", argParseTestCase{arg: anyArg, raw: "false", output: false}.Run)
It("should support true booleans", argParseTestCase{arg: anyArg, raw: "true", output: true}.Run)

Expand Down

0 comments on commit 81b2062

Please sign in to comment.