Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1 | // Copyright 2019 CUE Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package openapi |
| 16 | |
| 17 | import ( |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 18 | "fmt" |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 19 | "math" |
| 20 | "path" |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 21 | "regexp" |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 22 | "sort" |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 23 | "strconv" |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 24 | "strings" |
| 25 | |
| 26 | "cuelang.org/go/cue" |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 27 | "cuelang.org/go/cue/ast" |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 28 | "cuelang.org/go/cue/errors" |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 29 | "cuelang.org/go/cue/token" |
Marcel van Lohuizen | eac3ea2 | 2020-03-19 09:55:11 +0100 | [diff] [blame] | 30 | "cuelang.org/go/internal" |
| 31 | "golang.org/x/xerrors" |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | type buildContext struct { |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 35 | inst *cue.Instance |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 36 | instExt *cue.Instance |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 37 | refPrefix string |
| 38 | path []string |
| 39 | |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 40 | expandRefs bool |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 41 | structural bool |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 42 | nameFunc func(inst *cue.Instance, path []string) string |
| 43 | descFunc func(v cue.Value) string |
| 44 | fieldFilter *regexp.Regexp |
Marcel van Lohuizen | 73e3f6b | 2019-08-01 16:10:57 +0200 | [diff] [blame] | 45 | evalDepth int // detect cycles when resolving references |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 46 | |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 47 | schemas *OrderedMap |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 48 | |
| 49 | // Track external schemas. |
| 50 | externalRefs map[string]*externalType |
| 51 | } |
| 52 | |
| 53 | type externalType struct { |
| 54 | ref string |
| 55 | inst *cue.Instance |
| 56 | path []string |
| 57 | value cue.Value |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 60 | type oaSchema = OrderedMap |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 61 | |
| 62 | type typeFunc func(b *builder, a cue.Value) |
| 63 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 64 | func schemas(g *Generator, inst *cue.Instance) (schemas *ast.StructLit, err error) { |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 65 | var fieldFilter *regexp.Regexp |
| 66 | if g.FieldFilter != "" { |
| 67 | fieldFilter, err = regexp.Compile(g.FieldFilter) |
| 68 | if err != nil { |
| 69 | return nil, errors.Newf(token.NoPos, "invalid field filter: %v", err) |
| 70 | } |
| 71 | |
| 72 | // verify that certain elements are still passed. |
| 73 | for _, f := range strings.Split( |
| 74 | "version,title,allOf,anyOf,not,enum,Schema/properties,Schema/items"+ |
| 75 | "nullable,type", ",") { |
| 76 | if fieldFilter.MatchString(f) { |
| 77 | return nil, errors.Newf(token.NoPos, "field filter may not exclude %q", f) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 82 | c := buildContext{ |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 83 | inst: inst, |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 84 | instExt: inst, |
Marcel van Lohuizen | 0a97533 | 2019-07-04 10:43:25 +0200 | [diff] [blame] | 85 | refPrefix: "components/schemas", |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 86 | expandRefs: g.ExpandReferences, |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 87 | structural: g.ExpandReferences, |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 88 | nameFunc: g.ReferenceFunc, |
Marcel van Lohuizen | 4de93e1 | 2019-07-02 20:04:10 +0200 | [diff] [blame] | 89 | descFunc: g.DescriptionFunc, |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 90 | schemas: &OrderedMap{}, |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 91 | externalRefs: map[string]*externalType{}, |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 92 | fieldFilter: fieldFilter, |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | defer func() { |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 96 | switch x := recover().(type) { |
| 97 | case nil: |
| 98 | case *openapiError: |
| 99 | err = x |
| 100 | default: |
| 101 | panic(x) |
| 102 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 103 | }() |
| 104 | |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 105 | // Although paths is empty for now, it makes it valid OpenAPI spec. |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 106 | |
Marcel van Lohuizen | 7e4dc22 | 2019-10-08 13:14:34 +0200 | [diff] [blame] | 107 | i, err := inst.Value().Fields(cue.Definitions(true)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | for i.Next() { |
Marcel van Lohuizen | 3c437bd | 2020-04-01 18:19:30 +0200 | [diff] [blame] | 112 | if !i.IsDefinition() { |
| 113 | continue |
| 114 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 115 | // message, enum, or constant. |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 116 | label := i.Label() |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 117 | if c.isInternal(label) { |
| 118 | continue |
| 119 | } |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 120 | ref := c.makeRef(inst, []string{label}) |
| 121 | if ref == "" { |
| 122 | continue |
| 123 | } |
| 124 | c.schemas.Set(ref, c.build(label, i.Value())) |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // keep looping until a fixed point is reached. |
| 128 | for done := 0; len(c.externalRefs) != done; { |
| 129 | done = len(c.externalRefs) |
| 130 | |
| 131 | // From now on, all references need to be expanded |
| 132 | external := []string{} |
| 133 | for k := range c.externalRefs { |
| 134 | external = append(external, k) |
| 135 | } |
| 136 | sort.Strings(external) |
| 137 | |
| 138 | for _, k := range external { |
| 139 | ext := c.externalRefs[k] |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 140 | c.instExt = ext.inst |
| 141 | last := len(ext.path) - 1 |
| 142 | c.path = ext.path[:last] |
| 143 | name := ext.path[last] |
| 144 | c.schemas.Set(ext.ref, c.build(name, cue.Dereference(ext.value))) |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 145 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 146 | } |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 147 | |
Marcel van Lohuizen | 56509a5 | 2020-03-30 13:29:31 +0200 | [diff] [blame] | 148 | a := c.schemas.Elts |
| 149 | sort.Slice(a, func(i, j int) bool { |
| 150 | x, _, _ := ast.LabelName(a[i].(*ast.Field).Label) |
| 151 | y, _, _ := ast.LabelName(a[j].(*ast.Field).Label) |
| 152 | return x < y |
| 153 | }) |
| 154 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 155 | return (*ast.StructLit)(c.schemas), nil |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 158 | func (c *buildContext) build(name string, v cue.Value) *ast.StructLit { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 159 | return newCoreBuilder(c).schema(nil, name, v) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 162 | // isInternal reports whether or not to include this type. |
| 163 | func (c *buildContext) isInternal(name string) bool { |
| 164 | // TODO: allow a regexp filter in Config. If we have closed structs and |
| 165 | // definitions, this will likely be unnecessary. |
| 166 | return strings.HasSuffix(name, "_value") |
| 167 | } |
| 168 | |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 169 | func (b *builder) failf(v cue.Value, format string, args ...interface{}) { |
| 170 | panic(&openapiError{ |
| 171 | errors.NewMessage(format, args), |
| 172 | b.ctx.path, |
| 173 | v.Pos(), |
| 174 | }) |
| 175 | } |
| 176 | |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 177 | func (b *builder) unsupported(v cue.Value) { |
| 178 | if b.format == "" { |
| 179 | // Not strictly an error, but consider listing it as a warning |
| 180 | // in strict mode. |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func (b *builder) checkArgs(a []cue.Value, n int) { |
| 185 | if len(a)-1 != n { |
| 186 | b.failf(a[0], "%v must be used with %d arguments", a[0], len(a)-1) |
| 187 | } |
| 188 | } |
| 189 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 190 | func (b *builder) schema(core *builder, name string, v cue.Value) *ast.StructLit { |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 191 | oldPath := b.ctx.path |
| 192 | b.ctx.path = append(b.ctx.path, name) |
| 193 | defer func() { b.ctx.path = oldPath }() |
| 194 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 195 | var c *builder |
| 196 | if core == nil && b.ctx.structural { |
| 197 | c = newCoreBuilder(b.ctx) |
| 198 | c.buildCore(v) // initialize core structure |
| 199 | c.coreSchema(name) // build the |
| 200 | } else { |
| 201 | c = newRootBuilder(b.ctx) |
| 202 | c.core = core |
| 203 | } |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 204 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 205 | return c.fillSchema(v) |
| 206 | } |
| 207 | |
| 208 | func (b *builder) getDoc(v cue.Value) { |
| 209 | doc := []string{} |
| 210 | if b.ctx.descFunc != nil { |
| 211 | if str := b.ctx.descFunc(v); str != "" { |
| 212 | doc = append(doc, str) |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 213 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 214 | } else { |
| 215 | for _, d := range v.Doc() { |
| 216 | doc = append(doc, d.Text()) |
| 217 | } |
| 218 | } |
| 219 | if len(doc) > 0 { |
| 220 | str := strings.TrimSpace(strings.Join(doc, "\n\n")) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 221 | b.setSingle("description", ast.NewString(str), true) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 225 | func (b *builder) fillSchema(v cue.Value) *ast.StructLit { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 226 | if b.filled != nil { |
| 227 | return b.filled |
| 228 | } |
| 229 | |
| 230 | b.setValueType(v) |
| 231 | b.format = extractFormat(v) |
Jason Wang | 2b56efa | 2019-08-21 13:35:53 -0700 | [diff] [blame] | 232 | b.deprecated = getDeprecated(v) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 233 | |
| 234 | if b.core == nil || len(b.core.values) > 1 { |
| 235 | isRef := b.value(v, nil) |
| 236 | if isRef { |
| 237 | b.typ = "" |
| 238 | } |
| 239 | |
| 240 | if !isRef && !b.ctx.structural { |
| 241 | b.getDoc(v) |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 242 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 243 | } |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 244 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 245 | schema := b.finish() |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 246 | s := (*ast.StructLit)(schema) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 247 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 248 | simplify(b, s) |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 249 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 250 | sortSchema(s) |
Marcel van Lohuizen | 35abfa7 | 2019-08-12 15:31:53 +0200 | [diff] [blame] | 251 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 252 | b.filled = s |
| 253 | return s |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 254 | } |
| 255 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 256 | func label(d ast.Decl) string { |
| 257 | f := d.(*ast.Field) |
| 258 | s, _, _ := ast.LabelName(f.Label) |
| 259 | return s |
| 260 | } |
| 261 | |
| 262 | func value(d ast.Decl) ast.Expr { |
| 263 | return d.(*ast.Field).Value |
| 264 | } |
| 265 | |
| 266 | func sortSchema(s *ast.StructLit) { |
| 267 | sort.Slice(s.Elts, func(i, j int) bool { |
| 268 | iName := label(s.Elts[i]) |
| 269 | jName := label(s.Elts[j]) |
| 270 | pi := fieldOrder[iName] |
| 271 | pj := fieldOrder[jName] |
Marcel van Lohuizen | 35abfa7 | 2019-08-12 15:31:53 +0200 | [diff] [blame] | 272 | if pi != pj { |
| 273 | return pi > pj |
| 274 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 275 | return iName < jName |
Marcel van Lohuizen | 35abfa7 | 2019-08-12 15:31:53 +0200 | [diff] [blame] | 276 | }) |
| 277 | } |
| 278 | |
| 279 | var fieldOrder = map[string]int{ |
| 280 | "description": 31, |
| 281 | "type": 30, |
| 282 | "format": 29, |
| 283 | "required": 28, |
| 284 | "properties": 27, |
| 285 | "minProperties": 26, |
| 286 | "maxProperties": 25, |
| 287 | "minimum": 24, |
| 288 | "exclusiveMinimum": 23, |
| 289 | "maximum": 22, |
| 290 | "exclusiveMaximum": 21, |
| 291 | "minItems": 18, |
| 292 | "maxItems": 17, |
| 293 | "minLength": 16, |
| 294 | "maxLength": 15, |
| 295 | "items": 14, |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 296 | "enum": 13, |
| 297 | "default": 12, |
Marcel van Lohuizen | 35abfa7 | 2019-08-12 15:31:53 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 300 | func (b *builder) value(v cue.Value, f typeFunc) (isRef bool) { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 301 | count := 0 |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 302 | disallowDefault := false |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 303 | var values cue.Value |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 304 | if b.ctx.expandRefs || b.format != "" { |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 305 | values = cue.Dereference(v) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 306 | count = 1 |
| 307 | } else { |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 308 | dedup := map[string]bool{} |
| 309 | hasNoRef := false |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 310 | for _, v := range appendSplit(nil, cue.AndOp, v) { |
| 311 | // This may be a reference to an enum. So we need to check references before |
| 312 | // dissecting them. |
Marcel van Lohuizen | 065bfd4 | 2019-06-29 12:33:48 +0200 | [diff] [blame] | 313 | switch p, r := v.Reference(); { |
| 314 | case len(r) > 0: |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 315 | ref := b.ctx.makeRef(p, r) |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 316 | if ref == "" { |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 317 | v = cue.Dereference(v) |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 318 | break |
| 319 | } |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 320 | if dedup[ref] { |
| 321 | continue |
| 322 | } |
| 323 | dedup[ref] = true |
| 324 | |
Marcel van Lohuizen | 065bfd4 | 2019-06-29 12:33:48 +0200 | [diff] [blame] | 325 | b.addRef(v, p, r) |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 326 | disallowDefault = true |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 327 | continue |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 328 | } |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 329 | hasNoRef = true |
| 330 | count++ |
| 331 | values = values.Unify(v) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 332 | } |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 333 | isRef = !hasNoRef && len(dedup) == 1 |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if count > 0 { // TODO: implement IsAny. |
Marcel van Lohuizen | eac3ea2 | 2020-03-19 09:55:11 +0100 | [diff] [blame] | 337 | // TODO: perhaps find optimal representation. For now we assume the |
| 338 | // representation as is is already optimized for human consumption. |
| 339 | if values.IncompleteKind()&cue.StructKind != cue.StructKind && !isRef { |
| 340 | values = values.Eval() |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Marcel van Lohuizen | eac3ea2 | 2020-03-19 09:55:11 +0100 | [diff] [blame] | 343 | conjuncts := appendSplit(nil, cue.AndOp, values) |
| 344 | for i, v := range conjuncts { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 345 | switch { |
| 346 | case isConcrete(v): |
| 347 | b.dispatch(f, v) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 348 | if !b.isNonCore() { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 349 | b.set("enum", ast.NewList(b.decode(v))) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 350 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 351 | default: |
Marcel van Lohuizen | eac3ea2 | 2020-03-19 09:55:11 +0100 | [diff] [blame] | 352 | a := appendSplit(nil, cue.OrOp, v) |
| 353 | for i, v := range a { |
| 354 | if _, r := v.Reference(); len(r) == 0 { |
| 355 | a[i] = v.Eval() |
| 356 | } |
| 357 | } |
| 358 | |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 359 | _ = i |
| 360 | // TODO: it matters here whether a conjunct is obtained |
| 361 | // from embedding or normal unification. Fix this at some |
| 362 | // point. |
| 363 | // |
| 364 | // if len(a) > 1 { |
| 365 | // // Filter disjuncts that cannot unify with other conjuncts, |
| 366 | // // and thus can never be satisfied. |
| 367 | // // TODO: there should be generalized simplification logic |
| 368 | // // in CUE (outside of the usual implicit simplifications). |
| 369 | // k := 0 |
| 370 | // outer: |
| 371 | // for _, d := range a { |
| 372 | // for j, w := range conjuncts { |
| 373 | // if i == j { |
| 374 | // continue |
| 375 | // } |
| 376 | // if d.Unify(w).Err() != nil { |
| 377 | // continue outer |
| 378 | // } |
| 379 | // } |
| 380 | // a[k] = d |
| 381 | // k++ |
| 382 | // } |
| 383 | // a = a[:k] |
| 384 | // } |
Marcel van Lohuizen | eac3ea2 | 2020-03-19 09:55:11 +0100 | [diff] [blame] | 385 | switch len(a) { |
| 386 | case 0: |
| 387 | // Conjunct entirely eliminated. |
| 388 | case 1: |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 389 | v = a[0] |
| 390 | if err := v.Err(); err != nil { |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 391 | b.failf(v, "openapi: %v", err) |
| 392 | return |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 393 | } |
| 394 | b.dispatch(f, v) |
Marcel van Lohuizen | eac3ea2 | 2020-03-19 09:55:11 +0100 | [diff] [blame] | 395 | default: |
| 396 | b.disjunction(a, f) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 402 | if v, ok := v.Default(); ok && v.IsConcrete() && !disallowDefault { |
| 403 | // TODO: should we show the empty list default? This would be correct |
| 404 | // but perhaps a bit too pedantic and noisy. |
| 405 | switch { |
| 406 | case v.Kind() == cue.ListKind: |
| 407 | iter, _ := v.List() |
| 408 | if !iter.Next() { |
| 409 | // Don't show default for empty list. |
| 410 | break |
| 411 | } |
| 412 | fallthrough |
| 413 | default: |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 414 | if !b.isNonCore() { |
Marcel van Lohuizen | 3c437bd | 2020-04-01 18:19:30 +0200 | [diff] [blame] | 415 | e := v.Syntax(cue.Concrete(true)).(ast.Expr) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 416 | b.setFilter("Schema", "default", e) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 417 | } |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 418 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 419 | } |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 420 | return isRef |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | func appendSplit(a []cue.Value, splitBy cue.Op, v cue.Value) []cue.Value { |
| 424 | op, args := v.Expr() |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 425 | if op == cue.NoOp && len(args) == 1 { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 426 | // TODO: this is to deal with default value removal. This may change |
| 427 | // whe we completely separate default values from values. |
| 428 | a = append(a, args...) |
| 429 | } else if op != splitBy { |
| 430 | a = append(a, v) |
| 431 | } else { |
| 432 | for _, v := range args { |
| 433 | a = appendSplit(a, splitBy, v) |
| 434 | } |
| 435 | } |
| 436 | return a |
| 437 | } |
| 438 | |
| 439 | func countNodes(v cue.Value) (n int) { |
| 440 | switch op, a := v.Expr(); op { |
| 441 | case cue.OrOp, cue.AndOp: |
| 442 | for _, v := range a { |
| 443 | n += countNodes(v) |
| 444 | } |
| 445 | n += len(a) - 1 |
| 446 | default: |
| 447 | switch v.Kind() { |
| 448 | case cue.ListKind: |
| 449 | for i, _ := v.List(); i.Next(); { |
| 450 | n += countNodes(i.Value()) |
| 451 | } |
| 452 | case cue.StructKind: |
| 453 | for i, _ := v.Fields(); i.Next(); { |
| 454 | n += countNodes(i.Value()) + 1 |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | return n + 1 |
| 459 | } |
| 460 | |
| 461 | // isConcrete reports whether v is concrete and not a struct (recursively). |
| 462 | // structs are not supported as the result of a struct enum depends on how |
| 463 | // conjunctions and disjunctions are distributed. We could consider still doing |
| 464 | // this if we define a normal form. |
| 465 | func isConcrete(v cue.Value) bool { |
| 466 | if !v.IsConcrete() { |
| 467 | return false |
| 468 | } |
| 469 | if v.Kind() == cue.StructKind { |
| 470 | return false // TODO: handle struct kinds |
| 471 | } |
| 472 | for list, _ := v.List(); list.Next(); { |
| 473 | if !isConcrete(list.Value()) { |
| 474 | return false |
| 475 | } |
| 476 | } |
| 477 | return true |
| 478 | } |
| 479 | |
| 480 | func (b *builder) disjunction(a []cue.Value, f typeFunc) { |
| 481 | disjuncts := []cue.Value{} |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 482 | enums := []ast.Expr{} // TODO: unique the enums |
| 483 | nullable := false // Only supported in OpenAPI, not JSON schema |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 484 | |
| 485 | for _, v := range a { |
| 486 | switch { |
| 487 | case v.Null() == nil: |
| 488 | // TODO: for JSON schema, we need to fall through. |
| 489 | nullable = true |
| 490 | |
| 491 | case isConcrete(v): |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 492 | enums = append(enums, b.decode(v)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 493 | |
| 494 | default: |
| 495 | disjuncts = append(disjuncts, v) |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // Only one conjunct? |
| 500 | if len(disjuncts) == 0 || (len(disjuncts) == 1 && len(enums) == 0) { |
| 501 | if len(disjuncts) == 1 { |
| 502 | b.value(disjuncts[0], f) |
| 503 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 504 | if len(enums) > 0 && !b.isNonCore() { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 505 | b.set("enum", ast.NewList(enums...)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 506 | } |
| 507 | if nullable { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 508 | b.setSingle("nullable", ast.NewBool(true), true) // allowed in Structural |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 509 | } |
| 510 | return |
| 511 | } |
| 512 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 513 | anyOf := []ast.Expr{} |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 514 | if len(enums) > 0 { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 515 | anyOf = append(anyOf, b.kv("enum", ast.NewList(enums...))) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 516 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 517 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 518 | if nullable { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 519 | b.setSingle("nullable", ast.NewBool(true), true) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 520 | } |
Marcel van Lohuizen | eac3ea2 | 2020-03-19 09:55:11 +0100 | [diff] [blame] | 521 | |
| 522 | schemas := make([]*ast.StructLit, len(disjuncts)) |
| 523 | for i, v := range disjuncts { |
| 524 | c := newOASBuilder(b) |
| 525 | c.value(v, f) |
| 526 | t := c.finish() |
| 527 | schemas[i] = (*ast.StructLit)(t) |
| 528 | if len(t.Elts) == 0 { |
| 529 | if c.typ == "" { |
| 530 | return |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | for i, v := range disjuncts { |
| 536 | // In OpenAPI schema are open by default. To ensure forward compability, |
| 537 | // we do not represent closed structs with additionalProperties: false |
| 538 | // (this is discouraged and often disallowed by implementions), but |
| 539 | // rather enforce this by ensuring uniqueness of the disjuncts. |
| 540 | // |
| 541 | // TODO: subsumption may currently give false negatives. We are extra |
| 542 | // conservative in these instances. |
| 543 | subsumed := []ast.Expr{} |
| 544 | for j, w := range disjuncts { |
| 545 | if i == j { |
| 546 | continue |
| 547 | } |
| 548 | err := v.Subsume(w, cue.Schema()) |
| 549 | if err == nil || xerrors.Is(err, internal.ErrInexact) { |
| 550 | subsumed = append(subsumed, schemas[j]) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | t := schemas[i] |
| 555 | if len(subsumed) > 0 { |
| 556 | // TODO: elide anyOf if there is only one element. This should be |
| 557 | // rare if originating from oneOf. |
| 558 | exclude := ast.NewStruct("not", |
| 559 | ast.NewStruct("anyOf", ast.NewList(subsumed...))) |
| 560 | if len(t.Elts) == 0 { |
| 561 | t = exclude |
| 562 | } else { |
| 563 | t = ast.NewStruct("allOf", ast.NewList(t, exclude)) |
| 564 | } |
| 565 | } |
| 566 | anyOf = append(anyOf, t) |
| 567 | } |
| 568 | |
| 569 | b.set("oneOf", ast.NewList(anyOf...)) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | func (b *builder) setValueType(v cue.Value) { |
| 573 | if b.core != nil { |
| 574 | return |
| 575 | } |
| 576 | |
Marcel van Lohuizen | d99e32e | 2019-08-26 14:42:14 +0200 | [diff] [blame] | 577 | switch v.IncompleteKind() { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 578 | case cue.BoolKind: |
| 579 | b.typ = "boolean" |
| 580 | case cue.FloatKind, cue.NumberKind: |
| 581 | b.typ = "number" |
| 582 | case cue.IntKind: |
| 583 | b.typ = "integer" |
| 584 | case cue.BytesKind: |
| 585 | b.typ = "string" |
| 586 | case cue.StringKind: |
| 587 | b.typ = "string" |
| 588 | case cue.StructKind: |
| 589 | b.typ = "object" |
| 590 | case cue.ListKind: |
| 591 | b.typ = "array" |
| 592 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | func (b *builder) dispatch(f typeFunc, v cue.Value) { |
| 596 | if f != nil { |
| 597 | f(b, v) |
| 598 | return |
| 599 | } |
| 600 | |
Marcel van Lohuizen | d99e32e | 2019-08-26 14:42:14 +0200 | [diff] [blame] | 601 | switch v.IncompleteKind() { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 602 | case cue.NullKind: |
| 603 | // TODO: for JSON schema we would set the type here. For OpenAPI, |
| 604 | // it must be nullable. |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 605 | b.setSingle("nullable", ast.NewBool(true), true) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 606 | |
| 607 | case cue.BoolKind: |
| 608 | b.setType("boolean", "") |
| 609 | // No need to call. |
| 610 | |
| 611 | case cue.FloatKind, cue.NumberKind: |
| 612 | // TODO: |
| 613 | // Common Name type format Comments |
| 614 | // float number float |
| 615 | // double number double |
| 616 | b.setType("number", "") // may be overridden to integer |
| 617 | b.number(v) |
| 618 | |
| 619 | case cue.IntKind: |
| 620 | // integer integer int32 signed 32 bits |
| 621 | // long integer int64 signed 64 bits |
| 622 | b.setType("integer", "") // may be overridden to integer |
| 623 | b.number(v) |
| 624 | |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 625 | // TODO: for JSON schema, consider adding multipleOf: 1. |
| 626 | |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 627 | case cue.BytesKind: |
| 628 | // byte string byte base64 encoded characters |
| 629 | // binary string binary any sequence of octets |
| 630 | b.setType("string", "byte") |
| 631 | b.bytes(v) |
| 632 | case cue.StringKind: |
| 633 | // date string date As defined by full-date - RFC3339 |
| 634 | // dateTime string date-time As defined by date-time - RFC3339 |
| 635 | // password string password A hint to UIs to obscure input |
| 636 | b.setType("string", "") |
| 637 | b.string(v) |
| 638 | case cue.StructKind: |
| 639 | b.setType("object", "") |
| 640 | b.object(v) |
| 641 | case cue.ListKind: |
| 642 | b.setType("array", "") |
| 643 | b.array(v) |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // object supports the following |
| 648 | // - maxProperties: maximum allowed fields in this struct. |
Marcel van Lohuizen | b05c768 | 2019-07-25 17:51:24 +0200 | [diff] [blame] | 649 | // - minProperties: minimum required fields in this struct. |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 650 | // - patternProperties: [regexp]: schema |
| 651 | // TODO: we can support this once .kv(key, value) allow |
| 652 | // foo [=~"pattern"]: type |
| 653 | // An instance field must match all schemas for which a regexp matches. |
| 654 | // Even though it is not supported in OpenAPI, we should still accept it |
| 655 | // when receiving from OpenAPI. We could possibly use disjunctions to encode |
| 656 | // this. |
| 657 | // - dependencies: what? |
| 658 | // - propertyNames: schema |
| 659 | // every property name in the enclosed schema matches that of |
| 660 | func (b *builder) object(v cue.Value) { |
| 661 | // TODO: discriminator objects: we could theoretically derive discriminator |
| 662 | // objects automatically: for every object in a oneOf/allOf/anyOf, or any |
| 663 | // object composed of the same type, if a property is required and set to a |
| 664 | // constant value for each type, it is a discriminator. |
| 665 | |
Marcel van Lohuizen | 3086ea6 | 2019-08-01 18:05:40 +0200 | [diff] [blame] | 666 | switch op, a := v.Expr(); op { |
| 667 | case cue.CallOp: |
| 668 | name := fmt.Sprint(a[0]) |
| 669 | switch name { |
| 670 | case "struct.MinFields": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 671 | b.checkArgs(a, 1) |
Marcel van Lohuizen | 3086ea6 | 2019-08-01 18:05:40 +0200 | [diff] [blame] | 672 | b.setFilter("Schema", "minProperties", b.int(a[1])) |
| 673 | return |
| 674 | |
| 675 | case "struct.MaxFields": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 676 | b.checkArgs(a, 1) |
Marcel van Lohuizen | 3086ea6 | 2019-08-01 18:05:40 +0200 | [diff] [blame] | 677 | b.setFilter("Schema", "maxProperties", b.int(a[1])) |
| 678 | return |
| 679 | |
| 680 | default: |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 681 | b.unsupported(a[0]) |
| 682 | return |
Marcel van Lohuizen | 3086ea6 | 2019-08-01 18:05:40 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | case cue.NoOp: |
| 686 | // TODO: extract format from specific type. |
| 687 | |
| 688 | default: |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 689 | b.failf(v, "unsupported op %v for object type (%v)", op, v) |
Marcel van Lohuizen | 3086ea6 | 2019-08-01 18:05:40 +0200 | [diff] [blame] | 690 | return |
| 691 | } |
| 692 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 693 | required := []ast.Expr{} |
Marcel van Lohuizen | 7e4dc22 | 2019-10-08 13:14:34 +0200 | [diff] [blame] | 694 | for i, _ := v.Fields(); i.Next(); { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 695 | required = append(required, ast.NewString(i.Label())) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 696 | } |
| 697 | if len(required) > 0 { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 698 | b.setFilter("Schema", "required", ast.NewList(required...)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 699 | } |
| 700 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 701 | var properties *OrderedMap |
| 702 | if b.singleFields != nil { |
| 703 | properties = b.singleFields.getMap("properties") |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 704 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 705 | hasProps := properties != nil |
| 706 | if !hasProps { |
| 707 | properties = &OrderedMap{} |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Marcel van Lohuizen | 439f857 | 2020-03-18 12:07:54 +0100 | [diff] [blame] | 710 | for i, _ := v.Fields(cue.Optional(true), cue.Definitions(true)); i.Next(); { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 711 | label := i.Label() |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 712 | if b.ctx.isInternal(label) { |
| 713 | continue |
| 714 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 715 | var core *builder |
| 716 | if b.core != nil { |
| 717 | core = b.core.properties[label] |
| 718 | } |
| 719 | schema := b.schema(core, label, i.Value()) |
Marcel van Lohuizen | 439f857 | 2020-03-18 12:07:54 +0100 | [diff] [blame] | 720 | switch { |
| 721 | case i.IsDefinition(): |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 722 | ref := b.ctx.makeRef(b.ctx.instExt, append(b.ctx.path, label)) |
| 723 | if ref == "" { |
| 724 | continue |
| 725 | } |
Marcel van Lohuizen | 439f857 | 2020-03-18 12:07:54 +0100 | [diff] [blame] | 726 | b.ctx.schemas.Set(ref, schema) |
| 727 | case !b.isNonCore() || len(schema.Elts) > 0: |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 728 | properties.Set(label, schema) |
| 729 | } |
| 730 | } |
| 731 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 732 | if !hasProps && properties.len() > 0 { |
| 733 | b.setSingle("properties", (*ast.StructLit)(properties), false) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | if t, ok := v.Elem(); ok && (b.core == nil || b.core.items == nil) { |
| 737 | schema := b.schema(nil, "*", t) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 738 | if len(schema.Elts) > 0 { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 739 | b.setSingle("additionalProperties", schema, true) // Not allowed in structural. |
| 740 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // TODO: maxProperties, minProperties: can be done once we allow cap to |
| 744 | // unify with structs. |
| 745 | } |
| 746 | |
| 747 | // List constraints: |
| 748 | // |
| 749 | // Max and min items. |
| 750 | // - maxItems: int (inclusive) |
| 751 | // - minItems: int (inclusive) |
| 752 | // - items (item type) |
| 753 | // schema: applies to all items |
| 754 | // array of schemas: |
| 755 | // schema at pos must match if both value and items are defined. |
| 756 | // - additional items: |
| 757 | // schema: where items must be an array of schemas, intstance elements |
| 758 | // succeed for if they match this value for any value at a position |
| 759 | // greater than that covered by items. |
| 760 | // - uniqueItems: bool |
| 761 | // TODO: support with list.Unique() unique() or comprehensions. |
| 762 | // For the latter, we need equality for all values, which is doable, |
| 763 | // but not done yet. |
| 764 | // |
| 765 | // NOT SUPPORTED IN OpenAPI: |
| 766 | // - contains: |
| 767 | // schema: an array instance is valid if at least one element matches |
| 768 | // this schema. |
| 769 | func (b *builder) array(v cue.Value) { |
Marcel van Lohuizen | 1ceb046 | 2019-07-26 16:26:54 +0200 | [diff] [blame] | 770 | |
| 771 | switch op, a := v.Expr(); op { |
| 772 | case cue.CallOp: |
| 773 | name := fmt.Sprint(a[0]) |
| 774 | switch name { |
| 775 | case "list.UniqueItems": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 776 | b.checkArgs(a, 0) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 777 | b.setFilter("Schema", "uniqueItems", ast.NewBool(true)) |
Marcel van Lohuizen | 1ceb046 | 2019-07-26 16:26:54 +0200 | [diff] [blame] | 778 | return |
| 779 | |
| 780 | case "list.MinItems": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 781 | b.checkArgs(a, 1) |
Marcel van Lohuizen | 1ceb046 | 2019-07-26 16:26:54 +0200 | [diff] [blame] | 782 | b.setFilter("Schema", "minItems", b.int(a[1])) |
| 783 | return |
| 784 | |
| 785 | case "list.MaxItems": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 786 | b.checkArgs(a, 1) |
Marcel van Lohuizen | 1ceb046 | 2019-07-26 16:26:54 +0200 | [diff] [blame] | 787 | b.setFilter("Schema", "maxItems", b.int(a[1])) |
| 788 | return |
| 789 | |
| 790 | default: |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 791 | b.unsupported(a[0]) |
| 792 | return |
Marcel van Lohuizen | 1ceb046 | 2019-07-26 16:26:54 +0200 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | case cue.NoOp: |
| 796 | // TODO: extract format from specific type. |
| 797 | |
| 798 | default: |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 799 | b.failf(v, "unsupported op %v for array type", op) |
Marcel van Lohuizen | 1ceb046 | 2019-07-26 16:26:54 +0200 | [diff] [blame] | 800 | return |
| 801 | } |
| 802 | |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 803 | // Possible conjuncts: |
| 804 | // - one list (CUE guarantees merging all conjuncts) |
| 805 | // - no cap: is unified with list |
| 806 | // - unique items: at most one, but idempotent if multiple. |
| 807 | // There is never a need for allOf or anyOf. Note that a CUE list |
| 808 | // corresponds almost one-to-one to OpenAPI lists. |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 809 | items := []ast.Expr{} |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 810 | count := 0 |
| 811 | for i, _ := v.List(); i.Next(); count++ { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 812 | items = append(items, b.schema(nil, strconv.Itoa(count), i.Value())) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 813 | } |
| 814 | if len(items) > 0 { |
| 815 | // TODO: per-item schema are not allowed in OpenAPI, only in JSON Schema. |
| 816 | // Perhaps we should turn this into an OR after first normalizing |
| 817 | // the entries. |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 818 | b.set("items", ast.NewList(items...)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 819 | // panic("per-item types not supported in OpenAPI") |
| 820 | } |
| 821 | |
| 822 | // TODO: |
| 823 | // A CUE cap can be a set of discontinuous ranges. If we encounter this, |
| 824 | // we can create an allOf(list type, anyOf(ranges)). |
| 825 | cap := v.Len() |
| 826 | hasMax := false |
| 827 | maxLength := int64(math.MaxInt64) |
| 828 | |
| 829 | if n, capErr := cap.Int64(); capErr == nil { |
| 830 | maxLength = n |
| 831 | hasMax = true |
| 832 | } else { |
| 833 | b.value(cap, (*builder).listCap) |
| 834 | } |
| 835 | |
| 836 | if !hasMax || int64(len(items)) < maxLength { |
| 837 | if typ, ok := v.Elem(); ok { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 838 | var core *builder |
| 839 | if b.core != nil { |
| 840 | core = b.core.items |
| 841 | } |
| 842 | t := b.schema(core, "*", typ) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 843 | if len(items) > 0 { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 844 | b.setFilter("Schema", "additionalItems", t) // Not allowed in structural. |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 845 | } else if !b.isNonCore() || len(t.Elts) > 0 { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 846 | b.setSingle("items", t, true) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | func (b *builder) listCap(v cue.Value) { |
| 853 | switch op, a := v.Expr(); op { |
| 854 | case cue.LessThanOp: |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 855 | b.setFilter("Schema", "maxItems", b.inta(a[0], -1)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 856 | case cue.LessThanEqualOp: |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 857 | b.setFilter("Schema", "maxItems", b.inta(a[0], 0)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 858 | case cue.GreaterThanOp: |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 859 | b.setFilter("Schema", "minItems", b.inta(a[0], 1)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 860 | case cue.GreaterThanEqualOp: |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 861 | if b.int64(a[0]) > 0 { |
| 862 | b.setFilter("Schema", "minItems", b.inta(a[0], 0)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 863 | } |
| 864 | case cue.NoOp: |
| 865 | // must be type, so okay. |
| 866 | case cue.NotEqualOp: |
| 867 | i := b.int(a[0]) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 868 | b.setNot("allOff", ast.NewList( |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 869 | b.kv("minItems", i), |
| 870 | b.kv("maxItems", i), |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 871 | )) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 872 | |
| 873 | default: |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 874 | b.failf(v, "unsupported op for list capacity %v", op) |
| 875 | return |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
| 879 | func (b *builder) number(v cue.Value) { |
| 880 | // Multiple conjuncts mostly means just additive constraints. |
| 881 | // Type may be number of float. |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 882 | |
| 883 | switch op, a := v.Expr(); op { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 884 | case cue.LessThanOp: |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 885 | b.setFilter("Schema", "exclusiveMaximum", b.big(a[0])) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 886 | |
| 887 | case cue.LessThanEqualOp: |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 888 | b.setFilter("Schema", "maximum", b.big(a[0])) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 889 | |
| 890 | case cue.GreaterThanOp: |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 891 | b.setFilter("Schema", "exclusiveMinimum", b.big(a[0])) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 892 | |
| 893 | case cue.GreaterThanEqualOp: |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 894 | b.setFilter("Schema", "minimum", b.big(a[0])) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 895 | |
| 896 | case cue.NotEqualOp: |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 897 | i := b.big(a[0]) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 898 | b.setNot("allOff", ast.NewList( |
Marcel van Lohuizen | 5ba7174 | 2019-07-25 18:35:09 +0200 | [diff] [blame] | 899 | b.kv("minimum", i), |
| 900 | b.kv("maximum", i), |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 901 | )) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 902 | |
Marcel van Lohuizen | b05c768 | 2019-07-25 17:51:24 +0200 | [diff] [blame] | 903 | case cue.CallOp: |
| 904 | name := fmt.Sprint(a[0]) |
| 905 | switch name { |
| 906 | case "math.MultipleOf": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 907 | b.checkArgs(a, 1) |
Marcel van Lohuizen | b05c768 | 2019-07-25 17:51:24 +0200 | [diff] [blame] | 908 | b.setFilter("Schema", "multipleOf", b.int(a[1])) |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 909 | |
Marcel van Lohuizen | b05c768 | 2019-07-25 17:51:24 +0200 | [diff] [blame] | 910 | default: |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 911 | b.unsupported(a[0]) |
| 912 | return |
Marcel van Lohuizen | b05c768 | 2019-07-25 17:51:24 +0200 | [diff] [blame] | 913 | } |
| 914 | |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 915 | case cue.NoOp: |
| 916 | // TODO: extract format from specific type. |
| 917 | |
| 918 | default: |
Marcel van Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 919 | b.failf(v, "unsupported op for number %v", op) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
| 923 | // Multiple Regexp conjuncts are represented as allOf all other |
| 924 | // constraints can be combined unless in the even of discontinuous |
| 925 | // lengths. |
| 926 | |
| 927 | // string supports the following options: |
| 928 | // |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 929 | // - maxLength (Unicode codepoints) |
| 930 | // - minLength (Unicode codepoints) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 931 | // - pattern (a regexp) |
| 932 | // |
| 933 | // The regexp pattern is as follows, and is limited to be a strict subset of RE2: |
| 934 | // Ref: https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-3.3 |
| 935 | // |
| 936 | // JSON schema requires ECMA 262 regular expressions, but |
| 937 | // limited to the following constructs: |
| 938 | // - simple character classes: [abc] |
| 939 | // - range character classes: [a-z] |
| 940 | // - complement character classes: [^abc], [^a-z] |
| 941 | // - simple quantifiers: +, *, ?, and lazy versions +? *? ?? |
| 942 | // - range quantifiers: {x}, {x,y}, {x,}, {x}?, {x,y}?, {x,}? |
| 943 | // - begin and end anchors: ^ and $ |
| 944 | // - simple grouping: (...) |
| 945 | // - alteration: | |
| 946 | // This is a subset of RE2 used by CUE. |
| 947 | // |
| 948 | // Most notably absent: |
| 949 | // - the '.' for any character (not sure if that is a doc bug) |
| 950 | // - character classes \d \D [[::]] \pN \p{Name} \PN \P{Name} |
| 951 | // - word boundaries |
| 952 | // - capturing directives. |
| 953 | // - flag setting |
| 954 | // - comments |
| 955 | // |
| 956 | // The capturing directives and comments can be removed without |
| 957 | // compromising the meaning of the regexp (TODO). Removing |
| 958 | // flag setting will be tricky. Unicode character classes, |
| 959 | // boundaries, etc can be compiled into simple character classes, |
| 960 | // although the resulting regexp will look cumbersome. |
| 961 | // |
| 962 | func (b *builder) string(v cue.Value) { |
| 963 | switch op, a := v.Expr(); op { |
| 964 | |
| 965 | case cue.RegexMatchOp, cue.NotRegexMatchOp: |
| 966 | s, err := a[0].String() |
| 967 | if err != nil { |
| 968 | // TODO: this may be an unresolved interpolation or expression. Consider |
| 969 | // whether it is reasonable to treat unevaluated operands as wholes and |
| 970 | // generate a compound regular expression. |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 971 | b.failf(v, "regexp value must be a string: %v", err) |
| 972 | return |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 973 | } |
| 974 | if op == cue.RegexMatchOp { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 975 | b.setFilter("Schema", "pattern", ast.NewString(s)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 976 | } else { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 977 | b.setNot("pattern", ast.NewString(s)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 978 | } |
| 979 | |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 980 | case cue.NoOp, cue.SelectorOp: |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 981 | |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 982 | case cue.CallOp: |
| 983 | name := fmt.Sprint(a[0]) |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 984 | switch name { |
| 985 | case "strings.MinRunes": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 986 | b.checkArgs(a, 1) |
| 987 | b.setFilter("Schema", "minLength", b.int(a[1])) |
| 988 | return |
| 989 | |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 990 | case "strings.MaxRunes": |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 991 | b.checkArgs(a, 1) |
| 992 | b.setFilter("Schema", "maxLength", b.int(a[1])) |
| 993 | return |
| 994 | |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 995 | default: |
Marcel van Lohuizen | 6cea136 | 2019-08-06 19:39:05 +0200 | [diff] [blame] | 996 | b.unsupported(a[0]) |
| 997 | return |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 998 | } |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 999 | |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1000 | default: |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 1001 | b.failf(v, "unsupported op %v for string type", op) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | func (b *builder) bytes(v cue.Value) { |
| 1006 | switch op, a := v.Expr(); op { |
| 1007 | |
| 1008 | case cue.RegexMatchOp, cue.NotRegexMatchOp: |
| 1009 | s, err := a[0].Bytes() |
| 1010 | if err != nil { |
| 1011 | // TODO: this may be an unresolved interpolation or expression. Consider |
| 1012 | // whether it is reasonable to treat unevaluated operands as wholes and |
| 1013 | // generate a compound regular expression. |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 1014 | b.failf(v, "regexp value must be of type bytes: %v", err) |
| 1015 | return |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1018 | e := ast.NewString(string(s)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1019 | if op == cue.RegexMatchOp { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1020 | b.setFilter("Schema", "pattern", e) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1021 | } else { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1022 | b.setNot("pattern", e) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | // TODO: support the following JSON schema constraints |
| 1026 | // - maxLength |
| 1027 | // - minLength |
| 1028 | |
Marcel van Lohuizen | fdd176c | 2019-06-25 18:12:26 +0200 | [diff] [blame] | 1029 | case cue.NoOp, cue.SelectorOp: |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1030 | |
| 1031 | default: |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 1032 | b.failf(v, "unsupported op %v for bytes type", op) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | type builder struct { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1037 | ctx *buildContext |
| 1038 | typ string |
| 1039 | format string |
| 1040 | singleFields *oaSchema |
| 1041 | current *oaSchema |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1042 | allOf []*ast.StructLit |
Jason Wang | 2b56efa | 2019-08-21 13:35:53 -0700 | [diff] [blame] | 1043 | deprecated bool |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1044 | |
| 1045 | // Building structural schema |
| 1046 | core *builder |
| 1047 | kind cue.Kind |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1048 | filled *ast.StructLit |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1049 | values []cue.Value // in structural mode, all values of not and *Of. |
| 1050 | keys []string |
| 1051 | properties map[string]*builder |
| 1052 | items *builder |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | func newRootBuilder(c *buildContext) *builder { |
| 1056 | return &builder{ctx: c} |
| 1057 | } |
| 1058 | |
| 1059 | func newOASBuilder(parent *builder) *builder { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1060 | core := parent |
| 1061 | if parent.core != nil { |
| 1062 | core = parent.core |
| 1063 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1064 | b := &builder{ |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1065 | core: core, |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1066 | ctx: parent.ctx, |
| 1067 | typ: parent.typ, |
| 1068 | format: parent.format, |
| 1069 | } |
| 1070 | return b |
| 1071 | } |
| 1072 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1073 | func (b *builder) isNonCore() bool { |
| 1074 | return b.core != nil |
| 1075 | } |
| 1076 | |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1077 | func (b *builder) setType(t, format string) { |
| 1078 | if b.typ == "" { |
| 1079 | b.typ = t |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 1080 | if format != "" { |
| 1081 | b.format = format |
| 1082 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | func setType(t *oaSchema, b *builder) { |
| 1087 | if b.typ != "" { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1088 | if b.core == nil || (b.core.typ != b.typ && !b.ctx.structural) { |
| 1089 | if !t.exists("type") { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1090 | t.Set("type", ast.NewString(b.typ)) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | if b.format != "" { |
| 1095 | if b.core == nil || b.core.format != b.format { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1096 | t.Set("format", ast.NewString(b.format)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 1101 | // setFilter is like set, but allows the key-value pair to be filtered. |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1102 | func (b *builder) setFilter(schema, key string, v ast.Expr) { |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 1103 | if re := b.ctx.fieldFilter; re != nil && re.MatchString(path.Join(schema, key)) { |
| 1104 | return |
| 1105 | } |
| 1106 | b.set(key, v) |
| 1107 | } |
| 1108 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1109 | // setSingle sets a value of which there should only be one. |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1110 | func (b *builder) setSingle(key string, v ast.Expr, drop bool) { |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1111 | if b.singleFields == nil { |
| 1112 | b.singleFields = &OrderedMap{} |
| 1113 | } |
| 1114 | if b.singleFields.exists(key) { |
| 1115 | if !drop { |
| 1116 | b.failf(cue.Value{}, "more than one value added for key %q", key) |
| 1117 | } |
| 1118 | } |
| 1119 | b.singleFields.Set(key, v) |
| 1120 | } |
| 1121 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1122 | func (b *builder) set(key string, v ast.Expr) { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1123 | if b.current == nil { |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 1124 | b.current = &OrderedMap{} |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1125 | b.allOf = append(b.allOf, (*ast.StructLit)(b.current)) |
Marcel van Lohuizen | 865f159 | 2019-07-02 19:43:45 +0200 | [diff] [blame] | 1126 | } else if b.current.exists(key) { |
| 1127 | b.current = &OrderedMap{} |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1128 | b.allOf = append(b.allOf, (*ast.StructLit)(b.current)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1129 | } |
Marcel van Lohuizen | c89f5a6 | 2019-07-04 10:41:18 +0200 | [diff] [blame] | 1130 | b.current.Set(key, v) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1131 | } |
| 1132 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1133 | func (b *builder) kv(key string, value ast.Expr) *ast.StructLit { |
| 1134 | return ast.NewStruct(key, value) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1137 | func (b *builder) setNot(key string, value ast.Expr) { |
| 1138 | b.add(ast.NewStruct("not", b.kv(key, value))) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1139 | } |
| 1140 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1141 | func (b *builder) finish() *ast.StructLit { |
| 1142 | var t *OrderedMap |
| 1143 | |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1144 | if b.filled != nil { |
| 1145 | return b.filled |
| 1146 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1147 | switch len(b.allOf) { |
| 1148 | case 0: |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1149 | t = &OrderedMap{} |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1150 | |
| 1151 | case 1: |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1152 | t = (*OrderedMap)(b.allOf[0]) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1153 | |
| 1154 | default: |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1155 | exprs := []ast.Expr{} |
| 1156 | for _, s := range b.allOf { |
| 1157 | exprs = append(exprs, s) |
| 1158 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1159 | t = &OrderedMap{} |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1160 | t.Set("allOf", ast.NewList(exprs...)) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1161 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1162 | if b.singleFields != nil { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1163 | b.singleFields.Elts = append(b.singleFields.Elts, t.Elts...) |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1164 | t = b.singleFields |
| 1165 | } |
Jason Wang | 2b56efa | 2019-08-21 13:35:53 -0700 | [diff] [blame] | 1166 | if b.deprecated { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1167 | t.Set("deprecated", ast.NewBool(true)) |
Jason Wang | 2b56efa | 2019-08-21 13:35:53 -0700 | [diff] [blame] | 1168 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 1169 | setType(t, b) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1170 | sortSchema((*ast.StructLit)(t)) |
| 1171 | return (*ast.StructLit)(t) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1172 | } |
| 1173 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1174 | func (b *builder) add(t *ast.StructLit) { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1175 | b.allOf = append(b.allOf, t) |
| 1176 | } |
| 1177 | |
| 1178 | func (b *builder) addConjunct(f func(*builder)) { |
| 1179 | c := newOASBuilder(b) |
| 1180 | f(c) |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1181 | b.add((*ast.StructLit)(c.finish())) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
Marcel van Lohuizen | 065bfd4 | 2019-06-29 12:33:48 +0200 | [diff] [blame] | 1184 | func (b *builder) addRef(v cue.Value, inst *cue.Instance, ref []string) { |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 1185 | name := b.ctx.makeRef(inst, ref) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1186 | b.addConjunct(func(b *builder) { |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1187 | b.set("$ref", ast.NewString(path.Join("#", b.ctx.refPrefix, name))) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1188 | }) |
Marcel van Lohuizen | ef90d5c | 2019-06-29 14:05:25 +0200 | [diff] [blame] | 1189 | |
| 1190 | if b.ctx.inst != inst { |
| 1191 | b.ctx.externalRefs[name] = &externalType{ |
| 1192 | ref: name, |
| 1193 | inst: inst, |
| 1194 | path: ref, |
| 1195 | value: v, |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | func (b *buildContext) makeRef(inst *cue.Instance, ref []string) string { |
| 1201 | a := make([]string, 0, len(ref)+3) |
| 1202 | if b.nameFunc != nil { |
| 1203 | a = append(a, b.nameFunc(inst, ref)) |
| 1204 | } else { |
| 1205 | a = append(a, ref...) |
| 1206 | } |
Marcel van Lohuizen | 439f857 | 2020-03-18 12:07:54 +0100 | [diff] [blame] | 1207 | return strings.Join(a, ".") |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1208 | } |
| 1209 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1210 | func (b *builder) int64(v cue.Value) int64 { |
| 1211 | v, _ = v.Default() |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1212 | i, err := v.Int64() |
| 1213 | if err != nil { |
Marcel van Lohuizen | ebf960e | 2019-06-25 17:48:03 +0200 | [diff] [blame] | 1214 | b.failf(v, "could not retrieve int: %v", err) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1215 | } |
| 1216 | return i |
| 1217 | } |
| 1218 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1219 | func (b *builder) intExpr(i int64) ast.Expr { |
| 1220 | return &ast.BasicLit{ |
| 1221 | Kind: token.INT, |
| 1222 | Value: fmt.Sprint(i), |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1223 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1224 | } |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 1225 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 1226 | func (b *builder) int(v cue.Value) ast.Expr { |
| 1227 | return b.intExpr(b.int64(v)) |
| 1228 | } |
| 1229 | |
| 1230 | func (b *builder) inta(v cue.Value, offset int64) ast.Expr { |
| 1231 | return b.intExpr(b.int64(v) + offset) |
| 1232 | } |
| 1233 | |
| 1234 | func (b *builder) decode(v cue.Value) ast.Expr { |
| 1235 | v, _ = v.Default() |
| 1236 | return v.Syntax().(ast.Expr) |
| 1237 | } |
| 1238 | |
| 1239 | func (b *builder) big(v cue.Value) ast.Expr { |
| 1240 | v, _ = v.Default() |
| 1241 | return v.Syntax().(ast.Expr) |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 1242 | } |