Skip to content

Commit

Permalink
encoding/jsonschema: remove unused state fields
Browse files Browse the repository at this point in the history
The presence of both `state.parent` and `state.up` was confusing,
especially as neither was documented and `state.parent` wasn't actually
used for anything.

Likewise, `state.path` isn't used for anything, and isn't actually an
accurate reflection of the JSON path (it's not updated for list values).

So remove both. They can easily be reinstated if they turn out to be
useful in the future.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ice08453b867f4e6a474db1e725e59b99e22ce036
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202558
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 f5f947e commit ded6fc3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
6 changes: 3 additions & 3 deletions encoding/jsonschema/constraints_combinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func constraintAllOf(key string, n cue.Value, s *state) {
}
a := make([]ast.Expr, 0, len(items))
for _, v := range items {
x, sub := s.schemaState(v, s.allowedTypes, nil, true)
x, sub := s.schemaState(v, s.allowedTypes, nil)
s.allowedTypes &= sub.allowedTypes
if sub.hasConstraints() {
// This might seem a little odd, since the actual
Expand Down Expand Up @@ -74,7 +74,7 @@ func constraintAnyOf(key string, n cue.Value, s *state) {
}
a := make([]ast.Expr, 0, len(items))
for _, v := range items {
x, sub := s.schemaState(v, s.allowedTypes, nil, true)
x, sub := s.schemaState(v, s.allowedTypes, nil)
if sub.allowedTypes == 0 {
// Nothing is allowed; omit.
continue
Expand Down Expand Up @@ -118,7 +118,7 @@ func constraintOneOf(key string, n cue.Value, s *state) {
}
a := make([]ast.Expr, 0, len(items))
for _, v := range items {
x, sub := s.schemaState(v, s.allowedTypes, nil, true)
x, sub := s.schemaState(v, s.allowedTypes, nil)
if sub.allowedTypes == 0 {
// Nothing is allowed; omit
continue
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/constraints_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func constraintProperties(key string, n cue.Value, s *state) {
s.processMap(n, func(key string, n cue.Value) {
// property?: value
name := ast.NewString(key)
expr, state := s.schemaState(n, allTypes, []label{{name: key}}, false)
expr, state := s.schemaState(n, allTypes, []label{{name: key}})
f := &ast.Field{Label: name, Value: expr}
state.doc(f)
f.Optional = token.Blank.Pos()
Expand All @@ -131,7 +131,7 @@ func constraintProperties(key string, n cue.Value, s *state) {

func constraintPropertyNames(key string, n cue.Value, s *state) {
// [=~pattern]: _
if names, _ := s.schemaState(n, cue.StringKind, nil, false); !isAny(names) {
if names, _ := s.schemaState(n, cue.StringKind, nil); !isAny(names) {
x := ast.NewStruct(ast.NewList(names), top())
s.add(n, objectType, x)
}
Expand Down
19 changes: 5 additions & 14 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (d *decoder) schema(ref []ast.Label, v cue.Value) (a []ast.Decl) {
root.isSchema = true
}

expr, state := root.schemaState(v, allTypes, nil, false)
expr, state := root.schemaState(v, allTypes, nil)
if state.allowedTypes == 0 {
d.addErr(errors.Newf(state.pos.Pos(), "constraints are not possible to satisfy"))
}
Expand Down Expand Up @@ -378,8 +378,7 @@ type state struct {

isSchema bool // for omitting ellipsis in an ast.File

up *state
parent *state
up *state

path []string

Expand Down Expand Up @@ -685,7 +684,7 @@ func (s *state) doc(n ast.Node) {
}

func (s *state) schema(n cue.Value, idRef ...label) ast.Expr {
expr, _ := s.schemaState(n, allTypes, idRef, false)
expr, _ := s.schemaState(n, allTypes, idRef)
// TODO: report unused doc.
return expr
}
Expand All @@ -695,21 +694,17 @@ func (s *state) schema(n cue.Value, idRef ...label) ast.Expr {
// types holds the set of possible types that the value can hold.
// idRef holds the path to the value.
// isLogical specifies whether the caller is a logical operator like anyOf, allOf, oneOf, or not.
func (s *state) schemaState(n cue.Value, types cue.Kind, idRef []label, isLogical bool) (ast.Expr, *state) {
func (s *state) schemaState(n cue.Value, types cue.Kind, idRef []label) (ast.Expr, *state) {
state := &state{
up: s,
schemaVersion: s.schemaVersion,
isSchema: s.isSchema,
decoder: s.decoder,
allowedTypes: types,
knownTypes: allTypes,
path: s.path,
idRef: idRef,
pos: n,
}
if isLogical {
state.parent = s
}
if n.Kind() == cue.BoolKind {
if vfrom(VersionDraft6).contains(state.schemaVersion) {
// From draft6 onwards, boolean values signify a schema that always passes or fails.
Expand Down Expand Up @@ -796,14 +791,10 @@ func (s *state) value(n cue.Value) ast.Expr {
// This may also prevent exponential blow-up (as may happen when
// converting YAML to JSON).
func (s *state) processMap(n cue.Value, f func(key string, n cue.Value)) {
saved := s.path
defer func() { s.path = saved }()

// TODO: intercept references to allow for optimized performance.
for i, _ := n.Fields(); i.Next(); {
key := i.Label()
s.path = append(saved, key)
f(key, i.Value())
f(i.Label(), i.Value())
}
}

Expand Down

0 comments on commit ded6fc3

Please sign in to comment.