encoding/openapi: support time types
Also recognize unsupported builtins for
their type.
Issue #56
Change-Id: Ic900829144d0130db412c1202f82df4e3a56c236
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2724
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/openapi/types.go b/encoding/openapi/types.go
index 3995055..e30a4ba 100644
--- a/encoding/openapi/types.go
+++ b/encoding/openapi/types.go
@@ -15,6 +15,8 @@
package openapi
import (
+ "fmt"
+
"github.com/cockroachdb/apd/v2"
"cuelang.org/go/cue"
@@ -32,7 +34,15 @@
"string": "string",
"bytes": "binary",
- // TODO: date, date-time, password.
+ "time.Time": "dateTime",
+ "time.Time ()": "dateTime",
+ `time.Format ("2006-01-02")`: "date",
+
+ // TODO: if a format is more strict (e.g. using zeros instead of nines
+ // for fractional seconds), we could still use this as an approximation.
+ `time.Format ("2006-01-02T15:04:05.999999999Z07:00")`: "dateTime",
+
+ // TODO: password.
}
func extractFormat(v cue.Value) string {
@@ -45,7 +55,11 @@
if err != nil {
return ""
}
- return cueToOpenAPI[string(b)]
+ if s, ok := cueToOpenAPI[string(b)]; ok {
+ return s
+ }
+ s := fmt.Sprint(v)
+ return cueToOpenAPI[s]
}
func simplify(b *builder, t *OrderedMap) {