Skip to content

Commit 81b2062

Browse files
committed
✨ Support negative integers in marker syntax
This adds support for negative integers in the marker syntax parser. This is useful for markers like `+kubebuilder:validation:Minimum`. Signed-off-by: knight42 <[email protected]>
1 parent 6519034 commit 81b2062

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

pkg/crd/markers/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func init() {
106106
type Maximum int
107107

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

112112
// +controllertools:marker:generateHelp:category="CRD validation"

pkg/markers/parse.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,13 @@ func guessType(scanner *sc.Scanner, raw string, allowSlice bool) *Argument {
340340
}
341341
}
342342

343+
// then, integers...
343344
if !probablyString {
344-
if nextTok := subScanner.Scan(); nextTok == sc.Int {
345+
nextTok := subScanner.Scan()
346+
if nextTok == '-' {
347+
nextTok = subScanner.Scan()
348+
}
349+
if nextTok == sc.Int {
345350
return &Argument{Type: IntType}
346351
}
347352
}
@@ -481,11 +486,21 @@ func (a *Argument) parse(scanner *sc.Scanner, raw string, out reflect.Value, inS
481486
for tok := scanner.Scan(); tok != sc.EOF; tok = scanner.Scan() {
482487
}
483488
case IntType:
489+
nextChar := scanner.Peek()
490+
isNegative := false
491+
if nextChar == '-' {
492+
isNegative = true
493+
scanner.Scan() // eat the '-'
494+
}
484495
if !expect(scanner, sc.Int, "integer") {
485496
return
486497
}
487498
// TODO(directxman12): respect the size when parsing
488-
val, err := strconv.Atoi(scanner.TokenText())
499+
text := scanner.TokenText()
500+
if isNegative {
501+
text = "-" + text
502+
}
503+
val, err := strconv.Atoi(text)
489504
if err != nil {
490505
scanner.Error(scanner, fmt.Sprintf("unable to parse integer: %v", err))
491506
return

pkg/markers/parse_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var _ = Describe("Parsing", func() {
174174
It("should support double-quoted strings", argParseTestCase{arg: Argument{Type: StringType}, raw: `"some; string, \nhere"`, output: "some; string, \nhere"}.Run)
175175
It("should support raw strings", argParseTestCase{arg: Argument{Type: StringType}, raw: "`some; string, \\nhere`", output: `some; string, \nhere`}.Run)
176176
It("should support integers", argParseTestCase{arg: Argument{Type: IntType}, raw: "42", output: 42}.Run)
177-
XIt("should support negative integers", argParseTestCase{arg: Argument{Type: IntType}, raw: "-42", output: -42}.Run)
177+
It("should support negative integers", argParseTestCase{arg: Argument{Type: IntType}, raw: "-42", output: -42}.Run)
178178
It("should support false booleans", argParseTestCase{arg: Argument{Type: BoolType}, raw: "false", output: false}.Run)
179179
It("should support true booleans", argParseTestCase{arg: Argument{Type: BoolType}, raw: "true", output: true}.Run)
180180

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

0 commit comments

Comments
 (0)