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)
}
diff --git a/encoding/openapi/openapi_test.go b/encoding/openapi/openapi_test.go
index d5eb721..b085f75 100644
--- a/encoding/openapi/openapi_test.go
+++ b/encoding/openapi/openapi_test.go
@@ -51,6 +51,10 @@
"array.json",
defaultConfig,
}, {
+ "strings.cue",
+ "strings.json",
+ defaultConfig,
+ }, {
"oneof.cue",
"oneof.json",
defaultConfig,
@@ -82,6 +86,9 @@
inst := cue.Build(load.Instances([]string{filename}, nil))[0]
b, err := Gen(inst, tc.config)
+ if err != nil {
+ t.Fatal(err)
+ }
var out = &bytes.Buffer{}
_ = json.Indent(out, b, "", " ")
diff --git a/encoding/openapi/testdata/strings.cue b/encoding/openapi/testdata/strings.cue
new file mode 100644
index 0000000..f0d61fe
--- /dev/null
+++ b/encoding/openapi/testdata/strings.cue
@@ -0,0 +1,9 @@
+import "strings"
+
+MyType: {
+ myString: strings.MinRunes(1) & strings.MaxRunes(5)
+
+ myPattern: =~"foo.*bar"
+
+ myAntiPattern: !~"foo.*bar"
+}
diff --git a/encoding/openapi/testdata/strings.json b/encoding/openapi/testdata/strings.json
new file mode 100644
index 0000000..cff15f4
--- /dev/null
+++ b/encoding/openapi/testdata/strings.json
@@ -0,0 +1,34 @@
+{
+ "openapi": "3.0.0",
+ "info": {},
+ "components": {
+ "schemas": {
+ "MyType": {
+ "type": "object",
+ "required": [
+ "myString",
+ "myPattern",
+ "myAntiPattern"
+ ],
+ "properties": {
+ "myString": {
+ "type": "string",
+ "minLength": 1,
+ "maxLength": 5
+ },
+ "myPattern": {
+ "type": "string",
+ "pattern": "foo.*bar"
+ },
+ "myAntiPattern": {
+ "not": {
+ "type": "string",
+ "pattern": "foo.*bar"
+ },
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file