this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/jsonschema: minor improvements to Generate

This generates slightly more idiomatic JSON Schema
by avoiding an `items` keyword when it's not needed
and by using `type: "null"` rather than `const: "null"`.
The latter in particular will make it easier to fold
multiple `type` constraints in `anyOf` to be folded
into a single multiple-type constraint.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I201acd032b60e928632e090c10a5d80fe6c5edf3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1224282
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+13 -10
+6 -1
encoding/jsonschema/generate.go
··· 337 337 case cue.CallOp: 338 338 return g.makeCallItem(v, args) 339 339 } 340 - if isConcreteScalar(v) { 340 + if isConcreteScalar(v) && !v.IsNull() { 341 341 if err := v.Err(); err != nil { 342 342 g.addError(v, fmt.Errorf("error found in schema: %v", err)) 343 343 return &itemFalse{} ··· 681 681 } 682 682 if ellipsis.Exists() { 683 683 items.rest = g.makeItem(ellipsis) 684 + if _, ok := items.rest.(*itemTrue); ok { 685 + // Additional items are allowed by default anyway, 686 + // so no need to include them explicitly. 687 + items.rest = nil 688 + } 684 689 } else { 685 690 a.elems = append(a.elems, &itemLengthBounds{ 686 691 constraint: cue.LessThanEqualOp,
+1 -1
encoding/jsonschema/testdata/external/tests/draft2020-12/format.json
··· 353 353 "valid": true, 354 354 "skip": { 355 355 "v3": "6 errors in empty disjunction:\nconflicting values \"06/19/1963\" and [...] (mismatched types string and list):\n generated.cue:5:1\n generated.cue:5:52\n instance.json:1:1\nconflicting values \"06/19/1963\" and bool (mismatched types string and bool):\n generated.cue:5:1\n generated.cue:5:8\n instance.json:1:1\nconflicting values \"06/19/1963\" and null (mismatched types string and null):\n generated.cue:5:1\n instance.json:1:1\nconflicting values \"06/19/1963\" and number (mismatched types string and number):\n generated.cue:5:1\n generated.cue:5:15\n instance.json:1:1\nconflicting values \"06/19/1963\" and {...} (mismatched types string and struct):\n generated.cue:5:1\n generated.cue:5:60\n instance.json:1:1\ninvalid value \"06/19/1963\" (does not satisfy time.Format(\"2006-01-02\")): error in call to time.Format: invalid time \"06/19/1963\":\n generated.cue:5:24\n generated.cue:1:1\n generated.cue:5:36\n instance.json:1:1\n", 356 - "v3-roundtrip": "conflicting values \"06/19/1963\" and [..._] (mismatched types string and list):\n instance.json:1:1\nconflicting values \"06/19/1963\" and bool (mismatched types string and bool):\n instance.json:1:1\nconflicting values \"06/19/1963\" and null (mismatched types string and null):\n instance.json:1:1\nconflicting values \"06/19/1963\" and number (mismatched types string and number):\n instance.json:1:1\nconflicting values \"06/19/1963\" and {...} (mismatched types string and struct):\n instance.json:1:1\ninvalid value \"06/19/1963\" (does not satisfy time.Format(\"2006-01-02\")): error in call to time.Format: invalid time \"06/19/1963\":\n instance.json:1:1\ninvalid value \"06/19/1963\" (does not satisfy matchN): 0 matched, expected \u003e=1:\n instance.json:1:1\n" 356 + "v3-roundtrip": "conflicting values \"06/19/1963\" and [...] (mismatched types string and list):\n instance.json:1:1\nconflicting values \"06/19/1963\" and bool (mismatched types string and bool):\n instance.json:1:1\nconflicting values \"06/19/1963\" and null (mismatched types string and null):\n instance.json:1:1\nconflicting values \"06/19/1963\" and number (mismatched types string and number):\n instance.json:1:1\nconflicting values \"06/19/1963\" and {...} (mismatched types string and struct):\n instance.json:1:1\ninvalid value \"06/19/1963\" (does not satisfy time.Format(\"2006-01-02\")): error in call to time.Format: invalid time \"06/19/1963\":\n instance.json:1:1\ninvalid value \"06/19/1963\" (does not satisfy matchN): 0 matched, expected \u003e=1:\n instance.json:1:1\n" 357 357 } 358 358 } 359 359 ]
+1 -1
encoding/jsonschema/testdata/generate/basic.txtar
··· 49 49 } 50 50 t7: true 51 51 t8: { 52 - const: null 52 + type: "null" 53 53 } 54 54 } 55 55 }
+2 -3
encoding/jsonschema/testdata/generate/const.txtar
··· 60 60 const: 2 61 61 } 62 62 null: { 63 - const: null 63 + type: "null" 64 64 } 65 65 string: { 66 66 const: "something" ··· 85 85 ./datatest/tests.cue:28:13 86 86 -- out/generate-v3/errorNonNull -- 87 87 errorNonNull.data.null: conflicting values 1 and null (mismatched types int and null): 88 - 1:159 89 88 ./datatest/tests.cue:32:14 90 89 -- out/generate-v3/errorNonString -- 91 90 errorNonString.data.string: conflicting values "something" and "other": 92 - 1:183 91 + 1:184 93 92 ./datatest/tests.cue:36:16 94 93 -- out/generate-v3/ok1 --
+3 -4
encoding/jsonschema/testdata/generate/lists.txtar
··· 56 56 maxLength: 0 57 57 } 58 58 emptyOpenList: { 59 - type: "array" 60 - items: true 59 + type: "array" 61 60 } 62 61 mixedTuple: { 63 62 type: "array" ··· 104 103 ./datatest/tests.cue:14:27 105 104 -- out/generate-v3/badTupleLength -- 106 105 badTupleLength.data.tuple: incompatible list lengths (1 and 3): 107 - 1:437 106 + 1:424 108 107 -- out/generate-v3/badTupleType -- 109 108 badTupleType.data.tuple.0: conflicting values 1 and string (mismatched types int and string): 110 109 ./datatest/tests.cue:24:16 111 110 badTupleType.data.tuple.1: conflicting values "bar" and int (mismatched types string and int): 112 - 1:471 111 + 1:458 113 112 ./datatest/tests.cue:24:19 114 113 -- out/generate-v3/ok1 -- 115 114 -- out/generate-v3/ok2 --