Skip to content

Commit

Permalink
encoding/jsonschema: implement "not"
Browse files Browse the repository at this point in the history
This change implements the "not" keyword using `matchN`.
No test regressions were encountered when updating the external test data.

One regression was encountered when updating the regular
test data, and filed as a comment under issue #3422.
This required updating the test data to get the test to pass.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ib2537af7424895843eebc1b915bc643894637afa
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200530
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe committed Sep 3, 2024
1 parent 32865bc commit e7175ba
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 928 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -624,15 +624,33 @@ import "strings"

#path: #globs

#ref: null | {
#ref: matchN(1, [matchN(3, [matchN(0, [null | bool | number | string | [...] | {
branches!: _
"branches-ignore"!: _
...
}]) & {
...
}, matchN(0, [null | bool | number | string | [...] | {
tags!: _
"tags-ignore"!: _
...
}]) & {
...
}, matchN(0, [null | bool | number | string | [...] | {
paths!: _
"paths-ignore"!: _
...
}]) & {
...
}]), null]) & (null | {
branches?: #branch
"branches-ignore"?: #branch
tags?: #branch
"tags-ignore"?: #branch
paths?: #path
"paths-ignore"?: #path
...
}
})

#shell: matchN(>=1, [string, "bash" | "pwsh" | "python" | "sh" | "cmd" | "powershell"])

Expand Down
1 change: 1 addition & 0 deletions encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var constraints = []*constraint{
p2("minLength", constraintMinLength),
p3("minimum", constraintMinimum),
p2("multipleOf", constraintMultipleOf),
p3("not", constraintNot),
p3("oneOf", constraintOneOf),
p2("nullable", constraintNullable),
p2("pattern", constraintPattern),
Expand Down
12 changes: 12 additions & 0 deletions encoding/jsonschema/constraints_combinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,15 @@ func constraintOneOf(key string, n cue.Value, s *state) {
// TODO: oneOf({a:x}, {b:y}, ..., not(anyOf({a:x}, {b:y}, ...))),
// can be translated to {} | {a:x}, {b:y}, ...
}

func constraintNot(key string, n cue.Value, s *state) {
subSchema := s.schema(n)
s.all.add(n, ast.NewCall(
ast.NewIdent("matchN"),
&ast.BasicLit{
Kind: token.INT,
Value: "0",
},
ast.NewList(subSchema),
))
}
12 changes: 6 additions & 6 deletions encoding/jsonschema/external_teststats.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Generated by teststats. DO NOT EDIT
v2:
schema extract (pass / total): 975 / 1637 = 59.6%
tests (pass / total): 3140 / 7175 = 43.8%
tests on extracted schemas (pass / total): 3140 / 3546 = 88.6%
schema extract (pass / total): 1015 / 1637 = 62.0%
tests (pass / total): 3308 / 7175 = 46.1%
tests on extracted schemas (pass / total): 3308 / 3722 = 88.9%

v3:
schema extract (pass / total): 967 / 1637 = 59.1%
tests (pass / total): 3074 / 7175 = 42.8%
tests on extracted schemas (pass / total): 3074 / 3538 = 86.9%
schema extract (pass / total): 1003 / 1637 = 61.3%
tests (pass / total): 3214 / 7175 = 44.8%
tests on extracted schemas (pass / total): 3214 / 3678 = 87.4%
Loading

0 comments on commit e7175ba

Please sign in to comment.