Skip to content

Commit

Permalink
encoding/jsonschema: clearer internals
Browse files Browse the repository at this point in the history
It wasn't entirely clear to me when reading the
code exactly how the `cue.Value` passed to `newSel`,
`getNextIdent` and friends is used.

In fact, it's only used to report errors in the correct
position, so pass a `token.Pos` around instead to
make that a bit more clear. Also rearrange the arguments
to `state.newSel` to put the position first to match
other methods.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I78290b09be818126759c3f2162e567e9819887ba
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201909
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe committed Sep 27, 2024
1 parent da2e501 commit 19eea7f
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions encoding/jsonschema/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (s *state) makeCUERef(n cue.Value, u *url.URL, fragmentParts []string) (_e
return s.rootRef()
}

ident, fragmentParts = s.getNextIdent(n, fragmentParts)
ident, fragmentParts = s.getNextIdent(n.Pos(), fragmentParts)

case u.Host != "":
// Reference not found within scope. Create an import reference.
Expand Down Expand Up @@ -206,22 +206,22 @@ func (s *state) makeCUERef(n cue.Value, u *url.URL, fragmentParts []string) (_e
}
return newSel(e, s.idRef[1])
}
ident, fragmentParts = s.getNextIdent(n, fragmentParts)
ident, fragmentParts = s.getNextIdent(n.Pos(), fragmentParts)
ident.Node = s.obj
break
}
}

return s.newSel(ident, n, fragmentParts)
return s.newSel(n.Pos(), ident, fragmentParts)
}

// getNextSelector translates a JSON Reference path into a CUE path by consuming
// the first path elements and returning the corresponding CUE label.
func (s *state) getNextSelector(v cue.Value, a []string) (l label, tail []string) {
func (s *state) getNextSelector(pos token.Pos, a []string) (l label, tail []string) {
switch elem := a[0]; elem {
case "$defs", "definitions":
if len(a) == 1 {
s.errf(v, "cannot refer to %s section: must refer to one of its elements", a[0])
s.warnf(pos, "cannot refer to %s section: must refer to one of its elements", a[0])
return label{}, nil
}

Expand All @@ -233,7 +233,7 @@ func (s *state) getNextSelector(v cue.Value, a []string) (l label, tail []string

case "properties":
if len(a) == 1 {
s.errf(v, "cannot refer to %s section: must refer to one of its elements", a[0])
s.warnf(pos, "cannot refer to %s section: must refer to one of its elements", a[0])
return label{}, nil
}

Expand All @@ -245,7 +245,7 @@ func (s *state) getNextSelector(v cue.Value, a []string) (l label, tail []string
"additionalItems":
// TODO: as a temporary workaround, include the schema verbatim.
// TODO: provide definitions for these in CUE.
s.errf(v, "referring to field %q not yet supported", elem)
s.warnf(pos, "referring to field %q not yet supported", elem)

// Other known fields cannot be supported.
return label{}, nil
Expand All @@ -255,12 +255,12 @@ func (s *state) getNextSelector(v cue.Value, a []string) (l label, tail []string
}
}

// newSel converts a JSON Reference path and initial CUE identifier to
// a CUE selection path.
func (s *state) newSel(e ast.Expr, v cue.Value, a []string) ast.Expr {
// newSel converts an initial CUE identifier and a relative JSON Reference path
// to a CUE selection path.
func (s *state) newSel(pos token.Pos, e ast.Expr, a []string) ast.Expr {
for len(a) > 0 {
var label label
label, a = s.getNextSelector(v, a)
label, a = s.getNextSelector(pos, a)
e = newSel(e, label)
}
return e
Expand Down Expand Up @@ -310,8 +310,8 @@ func (s *state) setRef(lab label, r refs) {

// getNextIdent gets the first CUE reference from a JSON Reference path and
// converts it to a CUE identifier.
func (s *state) getNextIdent(v cue.Value, a []string) (resolved *ast.Ident, tail []string) {
lab, a := s.getNextSelector(v, a)
func (s *state) getNextIdent(pos token.Pos, a []string) (resolved *ast.Ident, tail []string) {
lab, a := s.getNextSelector(pos, a)

x := s.getRef(lab)
ident := ast.NewIdent(x.ident)
Expand Down

0 comments on commit 19eea7f

Please sign in to comment.