pkg/strings: add MinRunes and MaxRunes
Also adds respective conversions for the OpenAPI
encoding.
cue/builtins.go is generated using go generate
Change-Id: I3b709cb56bd59fbe08a0376d4b4760b9b21a2e3a
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2625
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/openapi/build.go b/encoding/openapi/build.go
index eaa0df4..8387c72 100644
--- a/encoding/openapi/build.go
+++ b/encoding/openapi/build.go
@@ -15,6 +15,7 @@
package openapi
import (
+ "fmt"
"math"
"math/big"
"path"
@@ -612,8 +613,8 @@
// string supports the following options:
//
-// - maxLenght (Unicode codepoints)
-// - minLenght (Unicode codepoints)
+// - maxLength (Unicode codepoints)
+// - minLength (Unicode codepoints)
// - pattern (a regexp)
//
// The regexp pattern is as follows, and is limited to be a strict subset of RE2:
@@ -658,7 +659,7 @@
return
}
if op == cue.RegexMatchOp {
- b.setFilter("schema", "pattern", s)
+ b.setFilter("Schema", "pattern", s)
} else {
b.setNot("pattern", s)
}
@@ -670,6 +671,22 @@
case cue.NoOp, cue.SelectorOp:
// TODO: determine formats from specific types.
+ case cue.CallOp:
+ name := fmt.Sprint(a[0])
+ field := ""
+ switch name {
+ case "strings.MinRunes":
+ field = "minLength"
+ case "strings.MaxRunes":
+ field = "maxLength"
+ default:
+ b.failf(v, "builtin %v not supported in OpenAPI", name)
+ }
+ if len(a) != 2 {
+ b.failf(v, "builtin %v may only be used with single argument", name)
+ }
+ b.setFilter("Schema", field, b.int(a[1]))
+
default:
b.failf(v, "unsupported op %v for string type", op)
}