-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoding/jsonschema: reorganize constraints
There are many constraints that are not yet implemented, the table in constraints.go is already pretty large, and it's hard to see what remains to be done. Sorting constraints by keyword helps, but it's also nice to have some per-domain organization. So move all constraints into their own functions and make the table just map to the functions as implemented elsewhere rather than inline in the table. This change involves code movement only, with no semantic change at all. I've left the copyright year the same as the previous code to reflect the lack of code changes. Signed-off-by: Roger Peppe <rogpeppe@gmail.com> Change-Id: I733d44bbc479850fca4a87d484d5c9f454662097 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199491 Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
- Loading branch information
Showing
8 changed files
with
823 additions
and
667 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2019 CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package jsonschema | ||
|
||
import ( | ||
"cuelang.org/go/cue" | ||
"cuelang.org/go/cue/ast" | ||
"cuelang.org/go/cue/token" | ||
) | ||
|
||
// Array constraints | ||
|
||
func constraintAdditionalItems(key string, n cue.Value, s *state) { | ||
switch n.Kind() { | ||
case cue.BoolKind: | ||
// TODO: support | ||
|
||
case cue.StructKind: | ||
if s.list != nil { | ||
elem := s.schema(n) | ||
s.list.Elts = append(s.list.Elts, &ast.Ellipsis{Type: elem}) | ||
} | ||
|
||
default: | ||
s.errf(n, `value of "additionalItems" must be an object or boolean`) | ||
} | ||
} | ||
|
||
func constraintContains(key string, n cue.Value, s *state) { | ||
list := s.addImport(n, "list") | ||
// TODO: Passing non-concrete values is not yet supported in CUE. | ||
if x := s.schema(n); !isAny(x) { | ||
x := ast.NewCall(ast.NewSel(list, "Contains"), clearPos(x)) | ||
s.add(n, arrayType, x) | ||
} | ||
} | ||
|
||
func constraintItems(key string, n cue.Value, s *state) { | ||
switch n.Kind() { | ||
case cue.StructKind: | ||
elem := s.schema(n) | ||
ast.SetRelPos(elem, token.NoRelPos) | ||
s.add(n, arrayType, ast.NewList(&ast.Ellipsis{Type: elem})) | ||
|
||
case cue.ListKind: | ||
var a []ast.Expr | ||
for _, n := range s.listItems("items", n, true) { | ||
v := s.schema(n) // TODO: label with number literal. | ||
ast.SetRelPos(v, token.NoRelPos) | ||
a = append(a, v) | ||
} | ||
s.list = ast.NewList(a...) | ||
s.add(n, arrayType, s.list) | ||
|
||
default: | ||
s.errf(n, `value of "items" must be an object or array`) | ||
} | ||
} | ||
|
||
func constraintMaxItems(key string, n cue.Value, s *state) { | ||
list := s.addImport(n, "list") | ||
x := ast.NewCall(ast.NewSel(list, "MaxItems"), clearPos(s.uint(n))) | ||
s.add(n, arrayType, x) | ||
} | ||
|
||
func constraintMinItems(key string, n cue.Value, s *state) { | ||
a := []ast.Expr{} | ||
p, err := n.Uint64() | ||
if err != nil { | ||
s.errf(n, "invalid uint") | ||
} | ||
for ; p > 0; p-- { | ||
a = append(a, ast.NewIdent("_")) | ||
} | ||
s.add(n, arrayType, ast.NewList(append(a, &ast.Ellipsis{})...)) | ||
|
||
// TODO: use this once constraint resolution is properly implemented. | ||
// list := s.addImport(n, "list") | ||
// s.addConjunct(n, ast.NewCall(ast.NewSel(list, "MinItems"), clearPos(s.uint(n)))) | ||
} | ||
|
||
func constraintUniqueItems(key string, n cue.Value, s *state) { | ||
if s.boolValue(n) { | ||
list := s.addImport(n, "list") | ||
s.add(n, arrayType, ast.NewCall(ast.NewSel(list, "UniqueItems"))) | ||
} | ||
} | ||
|
||
func clearPos(e ast.Expr) ast.Expr { | ||
ast.SetRelPos(e, token.NoRelPos) | ||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2019 CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package jsonschema | ||
|
||
import ( | ||
"cuelang.org/go/cue" | ||
"cuelang.org/go/cue/ast" | ||
"cuelang.org/go/cue/token" | ||
) | ||
|
||
// Constraint combinators. | ||
|
||
func constraintAllOf(key string, n cue.Value, s *state) { | ||
var a []ast.Expr | ||
var knownTypes cue.Kind | ||
for _, v := range s.listItems("allOf", n, false) { | ||
x, sub := s.schemaState(v, s.allowedTypes, nil, true) | ||
s.allowedTypes &= sub.allowedTypes | ||
if sub.hasConstraints() { | ||
// This might seem a little odd, since the actual | ||
// types are the intersection of the known types | ||
// of the allOf members. However, knownTypes | ||
// is really there to avoid adding redundant disjunctions. | ||
// So if we have (int & string) & (disjunction) | ||
// we definitely don't have to add int or string to | ||
// disjunction. | ||
knownTypes |= sub.knownTypes | ||
a = append(a, x) | ||
} | ||
} | ||
// TODO maybe give an error/warning if s.allowedTypes == 0 | ||
// as that's a known-impossible assertion? | ||
if len(a) > 0 { | ||
s.knownTypes &= knownTypes | ||
s.all.add(n, ast.NewBinExpr(token.AND, a...)) | ||
} | ||
} | ||
|
||
func constraintAnyOf(key string, n cue.Value, s *state) { | ||
var types cue.Kind | ||
var a []ast.Expr | ||
var knownTypes cue.Kind | ||
for _, v := range s.listItems("anyOf", n, false) { | ||
x, sub := s.schemaState(v, s.allowedTypes, nil, true) | ||
types |= sub.allowedTypes | ||
knownTypes |= sub.knownTypes | ||
a = append(a, x) | ||
} | ||
s.allowedTypes &= types | ||
if len(a) > 0 { | ||
s.knownTypes &= knownTypes | ||
s.all.add(n, ast.NewBinExpr(token.OR, a...)) | ||
} | ||
} | ||
|
||
func constraintOneOf(key string, n cue.Value, s *state) { | ||
var types cue.Kind | ||
var knownTypes cue.Kind | ||
var a []ast.Expr | ||
hasSome := false | ||
for _, v := range s.listItems("oneOf", n, false) { | ||
x, sub := s.schemaState(v, s.allowedTypes, nil, true) | ||
types |= sub.allowedTypes | ||
|
||
// TODO: make more finegrained by making it two pass. | ||
if sub.hasConstraints() { | ||
hasSome = true | ||
} | ||
|
||
if !isAny(x) { | ||
knownTypes |= sub.knownTypes | ||
a = append(a, x) | ||
} | ||
} | ||
// TODO if there are no elements in the oneOf, validation | ||
// should fail. | ||
s.allowedTypes &= types | ||
if len(a) > 0 && hasSome { | ||
s.knownTypes &= knownTypes | ||
s.all.add(n, ast.NewBinExpr(token.OR, a...)) | ||
} | ||
|
||
// TODO: oneOf({a:x}, {b:y}, ..., not(anyOf({a:x}, {b:y}, ...))), | ||
// can be translated to {} | {a:x}, {b:y}, ... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// Copyright 2019 CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package jsonschema | ||
|
||
import ( | ||
"cuelang.org/go/cue" | ||
"cuelang.org/go/cue/ast" | ||
"cuelang.org/go/cue/errors" | ||
"cuelang.org/go/cue/token" | ||
) | ||
|
||
// Generic constraints | ||
|
||
func constraintAddDefinitions(key string, n cue.Value, s *state) { | ||
if n.Kind() != cue.StructKind { | ||
s.errf(n, `"definitions" expected an object, found %s`, n.Kind()) | ||
} | ||
|
||
old := s.isSchema | ||
s.isSchema = true | ||
defer func() { s.isSchema = old }() | ||
|
||
s.processMap(n, func(key string, n cue.Value) { | ||
name := key | ||
|
||
var f *ast.Field | ||
|
||
ident := "#" + name | ||
if ast.IsValidIdent(ident) { | ||
f = &ast.Field{Value: s.schema(n, label{ident, true})} | ||
f.Label = ast.NewIdent(ident) | ||
} else { | ||
f = &ast.Field{Value: s.schema(n, label{"#", true}, label{name: name})} | ||
f.Label = ast.NewString(name) | ||
ident = "#" | ||
f = &ast.Field{ | ||
Label: ast.NewIdent("#"), | ||
Value: ast.NewStruct(f), | ||
} | ||
} | ||
|
||
ast.SetRelPos(f, token.NewSection) | ||
s.definitions = append(s.definitions, f) | ||
s.setField(label{name: ident, isDef: true}, f) | ||
}) | ||
} | ||
|
||
func constraintComment(key string, n cue.Value, s *state) { | ||
} | ||
|
||
func constraintConst(key string, n cue.Value, s *state) { | ||
s.all.add(n, s.value(n)) | ||
s.allowedTypes &= n.Kind() | ||
s.knownTypes &= n.Kind() | ||
} | ||
|
||
func constraintDefault(key string, n cue.Value, s *state) { | ||
sc := *s | ||
s.default_ = sc.value(n) | ||
// TODO: must validate that the default is subsumed by the normal value, | ||
// as CUE will otherwise broaden the accepted values with the default. | ||
s.examples = append(s.examples, s.default_) | ||
} | ||
|
||
func constraintDeprecated(key string, n cue.Value, s *state) { | ||
if s.boolValue(n) { | ||
s.deprecated = true | ||
} | ||
} | ||
|
||
func constraintDescription(key string, n cue.Value, s *state) { | ||
s.description, _ = s.strValue(n) | ||
} | ||
|
||
func constraintEnum(key string, n cue.Value, s *state) { | ||
var a []ast.Expr | ||
var types cue.Kind | ||
for _, x := range s.listItems("enum", n, true) { | ||
if (s.allowedTypes & x.Kind()) == 0 { | ||
// Enum value is redundant because it's | ||
// not in the allowed type set. | ||
continue | ||
} | ||
a = append(a, s.value(x)) | ||
types |= x.Kind() | ||
} | ||
s.knownTypes &= types | ||
s.allowedTypes &= types | ||
if len(a) > 0 { | ||
s.all.add(n, ast.NewBinExpr(token.OR, a...)) | ||
} | ||
} | ||
|
||
func constraintExamples(key string, n cue.Value, s *state) { | ||
if n.Kind() != cue.ListKind { | ||
s.errf(n, `value of "examples" must be an array, found %v`, n.Kind()) | ||
} | ||
// TODO: implement examples properly. | ||
// for _, n := range s.listItems("examples", n, true) { | ||
// if ex := s.value(n); !isAny(ex) { | ||
// s.examples = append(s.examples, ex) | ||
// } | ||
// } | ||
} | ||
|
||
func constraintNullable(key string, n cue.Value, s *state) { | ||
// TODO: only allow for OpenAPI. | ||
null := ast.NewNull() | ||
setPos(null, n) | ||
s.nullable = null | ||
} | ||
|
||
func constraintRef(key string, n cue.Value, s *state) { | ||
u := s.resolveURI(n) | ||
|
||
fragmentParts, err := splitFragment(u) | ||
if err != nil { | ||
s.addErr(errors.Newf(n.Pos(), "%v", err)) | ||
return | ||
} | ||
expr := s.makeCUERef(n, u, fragmentParts) | ||
if expr == nil { | ||
expr = &ast.BadExpr{From: n.Pos()} | ||
} | ||
|
||
s.all.add(n, expr) | ||
} | ||
|
||
func constraintTitle(key string, n cue.Value, s *state) { | ||
s.title, _ = s.strValue(n) | ||
} | ||
|
||
func constraintType(key string, n cue.Value, s *state) { | ||
var types cue.Kind | ||
set := func(n cue.Value) { | ||
str, ok := s.strValue(n) | ||
if !ok { | ||
s.errf(n, "type value should be a string") | ||
} | ||
switch str { | ||
case "null": | ||
types |= cue.NullKind | ||
s.setTypeUsed(n, nullType) | ||
// TODO: handle OpenAPI restrictions. | ||
case "boolean": | ||
types |= cue.BoolKind | ||
s.setTypeUsed(n, boolType) | ||
case "string": | ||
types |= cue.StringKind | ||
s.setTypeUsed(n, stringType) | ||
case "number": | ||
types |= cue.NumberKind | ||
s.setTypeUsed(n, numType) | ||
case "integer": | ||
types |= cue.IntKind | ||
s.setTypeUsed(n, numType) | ||
s.add(n, numType, ast.NewIdent("int")) | ||
case "array": | ||
types |= cue.ListKind | ||
s.setTypeUsed(n, arrayType) | ||
case "object": | ||
types |= cue.StructKind | ||
s.setTypeUsed(n, objectType) | ||
|
||
default: | ||
s.errf(n, "unknown type %q", n) | ||
} | ||
} | ||
|
||
switch n.Kind() { | ||
case cue.StringKind: | ||
set(n) | ||
case cue.ListKind: | ||
for i, _ := n.List(); i.Next(); { | ||
set(i.Value()) | ||
} | ||
default: | ||
s.errf(n, `value of "type" must be a string or list of strings`) | ||
} | ||
|
||
s.allowedTypes &= types | ||
} |
Oops, something went wrong.