Skip to content

Commit

Permalink
encoding/jsonschema: simplify matchN expressions with only one possib…
Browse files Browse the repository at this point in the history
…ility

When there's only one possibility for `oneOf`, `allOf` or `anyOf`, the
`matchN` call is redundant.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I51061df21904b2a95590a3985618da8f754ee1f7
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202560
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
  • Loading branch information
rogpeppe committed Oct 15, 2024
1 parent 1a4b0d0 commit 1508ef8
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 14 deletions.
10 changes: 10 additions & 0 deletions encoding/jsonschema/constraints_combinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func constraintAllOf(key string, n cue.Value, s *state) {
// as that's a known-impossible assertion?
if len(a) > 0 {
s.knownTypes &= knownTypes
if len(a) == 1 {
// Only one possibility. Use that.
s.all.add(n, a[0])
return
}
s.all.add(n, ast.NewCall(
ast.NewIdent("matchN"),
// TODO it would be nice to be able to use a special sentinel "all" value
Expand Down Expand Up @@ -140,6 +145,11 @@ func constraintOneOf(key string, n cue.Value, s *state) {
s.allowedTypes &= types
if len(a) > 0 && hasSome {
s.knownTypes &= knownTypes
if len(a) == 1 {
// Only one possibility. Use that.
s.all.add(n, a[0])
return
}
s.all.add(n, ast.NewCall(
ast.NewIdent("matchN"),
&ast.BasicLit{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@
],
"valid": true,
"skip": {
"v2": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:1:1\n generated.cue:2:8\n instance.json:1:1\n",
"v3": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:1:1\n generated.cue:2:8\n instance.json:1:1\n"
"v2": "5 errors in empty disjunction:\nconflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:1\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\n",
"v3": "conflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\nincompatible list lengths (1 and 2):\n instance.json:1:1\n"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@
],
"valid": true,
"skip": {
"v2": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:2:8\n instance.json:1:1\n",
"v3": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:2:8\n instance.json:1:1\n"
"v2": "5 errors in empty disjunction:\nconflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:1\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\n",
"v3": "conflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\nincompatible list lengths (1 and 2):\n instance.json:1:1\n"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@
],
"valid": true,
"skip": {
"v2": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:2:8\n instance.json:1:1\n",
"v3": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:2:8\n instance.json:1:1\n"
"v2": "5 errors in empty disjunction:\nconflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:1\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\n",
"v3": "conflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\nincompatible list lengths (1 and 2):\n instance.json:1:1\n"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@
],
"valid": true,
"skip": {
"v2": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:2:8\n instance.json:1:1\n",
"v3": "invalid value [1,null] (does not satisfy matchN(1, [null | bool | number | string | [int] | {}])): 0 matched, expected 1:\n generated.cue:2:1\n generated.cue:2:8\n instance.json:1:1\n"
"v2": "5 errors in empty disjunction:\nconflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:1\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\n",
"v3": "conflicting values [1,null] and {...} (mismatched types list and struct):\n generated.cue:2:41\n instance.json:1:1\nconflicting values bool and [1,null] (mismatched types bool and list):\n generated.cue:2:8\n instance.json:1:1\nconflicting values null and [1,null] (mismatched types null and list):\n generated.cue:2:1\n instance.json:1:1\nconflicting values number and [1,null] (mismatched types number and list):\n generated.cue:2:15\n instance.json:1:1\nconflicting values string and [1,null] (mismatched types string and list):\n generated.cue:2:24\n instance.json:1:1\nincompatible list lengths (1 and 2):\n instance.json:1:1\n"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/external/tests/draft7/ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,8 @@
]
},
"skip": {
"v2": "extract error: cannot compile resulting schema: expected operand, found ']':\n generated.cue:3:14\n",
"v3": "extract error: cannot compile resulting schema: expected operand, found ']':\n generated.cue:3:14\n"
"v2": "extract error: cannot compile resulting schema: package \"example.com/ref/if\" imported but not defined in :\n generated.cue:1:8\n",
"v3": "extract error: cannot compile resulting schema: package \"example.com/ref/if\" imported but not defined in :\n generated.cue:1:8\n"
},
"tests": [
{
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/txtar/typeexcluded.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ e3?: list.UniqueItems() & [_, ...] & [...strings.MinRunes(1)]
e4?: [...string]
e5?: int & >=0
e6?: [...=~"^[A-Za-z0-9 _.-]+$"]
e7?: matchN(>=1, [true, matchN(1, [{
e7?: matchN(>=1, [true, {
disableFix?: bool
...
}]) & {
} & {
ignoreMediaFeatureNames?: list.UniqueItems() & [_, ...] & [...string]
...
}])
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/txtar/unsupported.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
_

#ref: matchN(1, [matchN(1, [matchN(0, [string]) & {
#ref: matchN(1, [matchN(0, [string]) & {
...
}]), null]) & (null | {
}, null]) & (null | {
branches?: {
...
}
Expand Down

0 comments on commit 1508ef8

Please sign in to comment.