Skip to content

Commit

Permalink
encoding/encoding/jsonschema: allow type number with int enum
Browse files Browse the repository at this point in the history
This change adjusts the jsonschema logic to allow a number type
to be combined with a numeric enum.

Fixes #3173.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I4e2c63a51135fa3a59ec92d401b03295dc991306
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199214
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Aug 13, 2024
1 parent 8e99f5e commit bdf9be8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 10 additions & 3 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ const (
var coreToCUE = []cue.Kind{
nullType: cue.NullKind,
boolType: cue.BoolKind,
numType: cue.FloatKind,
numType: cue.NumberKind, // Note: both int and float.
stringType: cue.StringKind,
arrayType: cue.ListKind,
objectType: cue.StructKind,
Expand All @@ -256,16 +256,20 @@ func kindToAST(k cue.Kind) ast.Expr {
return ast.NewNull()
case cue.BoolKind:
return ast.NewIdent("bool")
case cue.FloatKind:
case cue.NumberKind:
return ast.NewIdent("number")
case cue.IntKind:
return ast.NewIdent("int")
case cue.FloatKind:
return ast.NewIdent("float")
case cue.StringKind:
return ast.NewIdent("string")
case cue.ListKind:
return ast.NewList(&ast.Ellipsis{})
case cue.StructKind:
return ast.NewStruct(&ast.Ellipsis{})
}
return nil
panic(fmt.Errorf("unexpected kind %v", k))
}

var coreTypeName = []string{
Expand Down Expand Up @@ -303,6 +307,9 @@ func (s *state) add(n cue.Value, t coreType, x ast.Expr) {
}

func (s *state) setTypeUsed(n cue.Value, t coreType) {
if int(t) >= len(s.types) {
panic(fmt.Errorf("type out of range %v/%v", int(t), len(s.types)))
}
s.types[t].setTypeUsed(n, t)
}

Expand Down
5 changes: 2 additions & 3 deletions encoding/jsonschema/testdata/numericenum.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
"type": "number",
"enum": [ 1, 2, 3]
}
-- out/decode/err --
constraint not allowed because type number is excluded:
type.json:2:5
-- out/decode/cue --
1 | 2 | 3

0 comments on commit bdf9be8

Please sign in to comment.