Skip to content

Commit

Permalink
internal/encoding/{json|yaml}: disallow non-regular fields
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I9aa012353a7ba465092203c6da4517593c5d48da
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/546885
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mpvl committed Feb 1, 2023
1 parent 9b573be commit 76e0be4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal/encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ func (e *encoder) encodeDecls(decls []ast.Decl, endPos token.Pos) error {
continue

case *ast.Field:
if internal.IsDefinition(x.Label) {
return errors.Newf(x.TokenPos, "json: definition not allowed")
if !internal.IsRegularField(x) {
return errors.Newf(x.TokenPos, "json: definition or hidden field not allowed")
}
if x.Optional != token.NoPos {
return errors.Newf(x.Optional, "json: optional fields not allowed")
Expand Down
6 changes: 5 additions & 1 deletion internal/encoding/json/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ f4: {
}, {
name: "disallowDefinitions",
in: `#a: 2 `,
out: "json: definition not allowed",
out: "json: definition or hidden field not allowed",
}, {
name: "disallowHidden",
in: `_a: 2 `,
out: "json: definition or hidden field not allowed",
}, {
name: "disallowOptionals",
in: `a?: 2`,
Expand Down
4 changes: 2 additions & 2 deletions internal/encoding/yaml/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ func encodeDecls(decls []ast.Decl) (n *yaml.Node, err error) {
continue

case *ast.Field:
if internal.IsDefinition(x.Label) {
return nil, errors.Newf(x.TokenPos, "yaml: definition not allowed")
if !internal.IsRegularField(x) {
return nil, errors.Newf(x.TokenPos, "yaml: definition or hidden fields not allowed")
}
if x.Optional != token.NoPos {
return nil, errors.Newf(x.Optional, "yaml: optional fields not allowed")
Expand Down
6 changes: 5 additions & 1 deletion internal/encoding/yaml/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ f4: {} # line 4
}, {
name: "disallowDefinitions",
in: `#a: 2 `,
out: "yaml: definition not allowed",
out: "yaml: definition or hidden fields not allowed",
}, {
name: "disallowHidden",
in: `_a: 2 `,
out: "yaml: definition or hidden fields not allowed",
}, {
name: "disallowOptionals",
in: `a?: 2`,
Expand Down

0 comments on commit 76e0be4

Please sign in to comment.