this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/json: correct positions of field labels from json

In JSON, object keys must be quoted. When converting to CUE, where safe,
we convert the string lit to a plain ident. However, because the quote
marks have been lost, it means the literal string has become 2 chars
shorter.

We should add 1 to the starting position of the plain idents.
- This ensures the individual character offsets match up with the
original.
- It also means the start-end range of the ident makes sense.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I74cb800998953383bd1742a29c21c7555265e9f0
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1229647
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+45 -109
+5 -5
cmd/cue/cmd/testdata/script/def_jsonschema.txtar
··· 106 106 } 107 107 -- expect-stderr-strict -- 108 108 keyword "$dynamicAnchor" not yet implemented: 109 - ./bad.json:4:3 109 + ./bad.json:4:4 110 110 unknown keyword "foo": 111 - ./bad.json:5:3 111 + ./bad.json:5:4 112 112 -- expect-stderr-strict-features -- 113 113 keyword "$dynamicAnchor" not yet implemented: 114 - ./bad.json:4:3 114 + ./bad.json:4:4 115 115 -- data.yaml -- 116 116 age: twenty 117 117 118 118 -- expect-stderr2 -- 119 119 age: conflicting values "twenty" and int (mismatched types string and int): 120 120 ./data.yaml:1:6 121 - ./schema.json:18:7 121 + ./schema.json:18:8 122 122 -- expect-stderr3 -- 123 123 age: conflicting values "twenty" and int (mismatched types string and int): 124 124 ./data.yaml:1:6 125 - ./schema.json:18:7 125 + ./schema.json:18:8 126 126 -- cue.mod/module.cue -- 127 127 module: "mod.test/config" 128 128 language: version: "v0.9.0"
+3 -3
cmd/cue/cmd/testdata/script/def_openapi_strict.txtar
··· 80 80 } 81 81 -- expect-stderr-1 -- 82 82 keyword "xml" not yet implemented: 83 - ./openapi.json:13:17 83 + ./openapi.json:13:18 84 84 -- expect-out.cue -- 85 85 info: { 86 86 title: *"My OpenAPI" | string ··· 91 91 } 92 92 -- expect-stderr-2 -- 93 93 unknown keyword "unknown": 94 - ./openapi-badkeyword.json:12:17 94 + ./openapi-badkeyword.json:12:18 95 95 -- expect-stderr-3 -- 96 96 unknown keyword "unknown": 97 - ./openapi3.1-badkeyword.json:12:17 97 + ./openapi3.1-badkeyword.json:12:18
+1 -1
cmd/cue/cmd/testdata/script/embed_file_err.txtar
··· 11 11 -- want-stderr -- 12 12 @embed: invalid string: 13 13 ./x.cue:5:6 14 - schema.json:10:7 14 + schema.json:10:8 15 15 -- cue.mod/module.cue -- 16 16 module: "test.example" 17 17 language: version: "v0.9.2"
+20 -18
encoding/json/json.go
··· 223 223 224 224 case *ast.Field: 225 225 // label is always a string for JSON. 226 - switch { 227 - case true: 228 - s, ok := x.Label.(*ast.BasicLit) 229 - if !ok || s.Kind != token.STRING { 230 - break // should not happen: implies invalid JSON 231 - } 226 + s, ok := x.Label.(*ast.BasicLit) 227 + if !ok || s.Kind != token.STRING { 228 + break // should not happen: implies invalid JSON 229 + } 232 230 233 - u, err := literal.Unquote(s.Value) 234 - if err != nil { 235 - break // should not happen: implies invalid JSON 236 - } 237 - 238 - // TODO(legacy): remove checking for '_' prefix once hidden 239 - // fields are removed. 240 - if ast.StringLabelNeedsQuoting(u) { 241 - break // keep string 242 - } 231 + u, err := literal.Unquote(s.Value) 232 + if err != nil { 233 + break // should not happen: implies invalid JSON 234 + } 243 235 244 - x.Label = ast.NewIdent(u) 245 - astutil.CopyMeta(x.Label, s) 236 + // TODO(legacy): remove checking for '_' prefix once hidden 237 + // fields are removed. 238 + if ast.StringLabelNeedsQuoting(u) { 239 + break // keep string 246 240 } 241 + 242 + x.Label = ast.NewIdent(u) 243 + astutil.CopyMeta(x.Label, s) 244 + // Having removed the quote-marks, the ident start should be 245 + // incremented by 1 so that the label content matches up with 246 + // the raw json. 247 + ast.SetPos(x.Label, x.Label.Pos().Add(1)) 248 + 247 249 ast.Walk(x.Value, beforeFn, afterFn) 248 250 descent = false 249 251
+1 -19
encoding/jsonschema/testdata/txtar/boolschema.txtar
··· 7 7 ] 8 8 } 9 9 10 - -- out/decode-v3/extract -- 11 - matchN(>=1, [_, error("disallowed")]) 12 - -- diff/-out/decode-v3/extract<==>+out/decode/extract -- 13 - diff old new 14 - --- old 15 - +++ new 16 - @@ -1,1 +1,1 @@ 17 - -matchN(>=1, [_, _|_]) 18 - +matchN(>=1, [_, error("disallowed")]) 19 - -- out/decode-v3-noshare/extract -- 20 - matchN(>=1, [_, error("disallowed")]) 21 - -- diff/-out/decode-v3-noshare/extract<==>+out/decode/extract -- 22 - diff old new 23 - --- old 24 - +++ new 25 - @@ -1,1 +1,1 @@ 26 - -matchN(>=1, [_, _|_]) 27 - +matchN(>=1, [_, error("disallowed")]) 28 10 -- out/decode/extract -- 29 - matchN(>=1, [_, _|_]) 11 + matchN(>=1, [_, error("disallowed")])
+1 -14
encoding/jsonschema/testdata/txtar/constarray.txtar
··· 7 7 } 8 8 -- out/decode/extract -- 9 9 [1] 10 - -- out/decode-v3/testerr/err-1 -- 10 + -- out/decode/testerr/err-1 -- 11 11 conflicting values [1] and 1 (mismatched types list and int): 12 - generated.cue:1:1 13 - test/err-1.json:1:1 14 - -- diff/-out/decode-v3/testerr/err-1<==>+out/decode/testerr/err-1 -- 15 - diff old new 16 - --- old 17 - +++ new 18 - @@ -1,3 +1,3 @@ 19 - -conflicting values 1 and [1] (mismatched types int and list): 20 - +conflicting values [1] and 1 (mismatched types list and int): 21 - generated.cue:1:1 22 - test/err-1.json:1:1 23 - -- out/decode/testerr/err-1 -- 24 - conflicting values 1 and [1] (mismatched types int and list): 25 12 generated.cue:1:1 26 13 test/err-1.json:1:1 27 14 -- out/decode/testerr/err-2 --
+1 -1
encoding/jsonschema/testdata/txtar/crd_properties_with_additionalproperties.txtar
··· 42 42 -- out/decode/extract -- 43 43 ERROR: 44 44 additionalProperties may not be combined with properties in Kubernetes CRD: 45 - schema.json:21:21 45 + schema.json:21:22 46 46 -- out/decodeCRD/extractCRD/error -- 47 47 additionalProperties may not be combined with properties in Kubernetes CRD
+1 -1
encoding/jsonschema/testdata/txtar/defaultversion.txtar
··· 8 8 -- out/decode/extract -- 9 9 ERROR: 10 10 keyword "$id" is not supported in JSON schema version http://json-schema.org/draft-04/schema#: 11 - schema.json:2:3 11 + schema.json:2:4
+1 -1
encoding/jsonschema/testdata/txtar/invalid_pattern.txtar
··· 8 8 -- out/decode/extract -- 9 9 ERROR: 10 10 invalid string: 11 - schema.json:3:5 11 + schema.json:3:6
+1 -20
encoding/jsonschema/testdata/txtar/issue3351.txtar
··· 63 63 "title": "JSON-e templates", 64 64 "type": "object" 65 65 } 66 - -- out/decode-v3/extract -- 67 - // JSON-e templates 68 - @jsonschema(schema="https://json-schema.org/draft/2019-09/schema") 69 - _schema 70 - _schema: { 71 - @jsonschema(id="https://www.sourcemeta.com/schemas/vendor/json-eXXXX@1.json") 72 - $else?: #."jsone-value" 73 - $let?: [string]: null | bool | number | string | [...] | { 74 - [string]: _schema 75 - } 76 - $sort?: matchN(>=1, [_schema, [...number]]) 77 - {[!~"^(\\$else|\\$let|\\$sort)$"]: #."jsone-value"} 78 - 79 - #: "jsone-array": [...#."jsone-value"] 80 - 81 - #: "jsone-object-array": [..._schema] 82 - 83 - #: "jsone-value": matchN(1, [_schema, [..._schema]]) 84 - } 85 - -- out/decode-v3-noshare/extract -- 66 + -- out/decode/extract -- 86 67 // JSON-e templates 87 68 @jsonschema(schema="https://json-schema.org/draft/2019-09/schema") 88 69 _schema
-16
encoding/jsonschema/testdata/txtar/list.txtar
··· 54 54 size?: list.MaxItems(9) & list.UniqueItems() & [_, _, _, ...] 55 55 additional?: [int, int, ...string] 56 56 }) 57 - -- out/decode-v3/testerr/err-foo-not-string -- 58 - foo.0: conflicting values true and string (mismatched types bool and string): 59 - generated.cue:5:1 60 - generated.cue:6:12 61 - test/err-foo-not-string.json:2:10 62 - -- diff/-out/decode-v3/testerr/err-foo-not-string<==>+out/decode/testerr/err-foo-not-string -- 63 - diff old new 64 - --- old 65 - +++ new 66 - @@ -1,5 +1,4 @@ 67 - foo.0: conflicting values true and string (mismatched types bool and string): 68 - generated.cue:5:1 69 - - generated.cue:6:9 70 - generated.cue:6:12 71 - test/err-foo-not-string.json:2:10 72 57 -- out/decode/testerr/err-foo-not-string -- 73 58 foo.0: conflicting values true and string (mismatched types bool and string): 74 59 generated.cue:5:1 75 - generated.cue:6:9 76 60 generated.cue:6:12 77 61 test/err-foo-not-string.json:2:10 78 62 -- test/empty.json --
+1 -1
encoding/jsonschema/testdata/txtar/newid_oldversion.txtar
··· 8 8 -- out/decode/extract -- 9 9 ERROR: 10 10 keyword "$id" is not supported in JSON schema version http://json-schema.org/draft-04/schema#: 11 - schema.json:3:3 11 + schema.json:3:4
+1 -1
encoding/jsonschema/testdata/txtar/oldid_newversion.txtar
··· 8 8 -- out/decode/extract -- 9 9 ERROR: 10 10 keyword "id" is not supported in JSON schema version http://json-schema.org/draft-07/schema#: 11 - schema.json:3:3 11 + schema.json:3:4
+1 -1
encoding/jsonschema/testdata/txtar/perl_pattern_strict.txtar
··· 10 10 -- out/decode/extract -- 11 11 ERROR: 12 12 unsupported Perl regexp syntax in "^(?![ \\t\\n]*\\(default(.*)\\))[\\s\\S]*": error parsing regexp: invalid or unsupported Perl syntax: `(?!`: 13 - schema.json:3:5 13 + schema.json:3:6
+1 -1
encoding/jsonschema/testdata/txtar/schema_not_at_root.txtar
··· 13 13 -- out/decode/extract -- 14 14 ERROR: 15 15 $schema can only appear at the root in JSON Schema version http://json-schema.org/draft-07/schema#: 16 - schema.json:6:11 16 + schema.json:6:12
+1 -1
encoding/jsonschema/testdata/txtar/strictfeatures.txtar
··· 9 9 -- out/decode/extract -- 10 10 ERROR: 11 11 keyword "$dynamicAnchor" not yet implemented: 12 - schema.json:4:3 12 + schema.json:4:4
+1 -1
encoding/jsonschema/testdata/txtar/strictkeywords.txtar
··· 14 14 -- out/decode/extract -- 15 15 ERROR: 16 16 unknown keyword "foo": 17 - schema.json:5:3 17 + schema.json:5:4
+1 -1
encoding/jsonschema/testdata/txtar/strictkeywordswithref.txtar
··· 11 11 -- out/decode/extract -- 12 12 ERROR: 13 13 ignoring keyword "$id" alongside $ref: 14 - schema.json:3:3 14 + schema.json:3:4
+3 -3
encoding/jsonschema/testdata/txtar/used.txtar
··· 43 43 -- out/decode/extract -- 44 44 ERROR: 45 45 from version https://json-schema.org/draft/2020-12/schema onwards, the value of "items" must be an object or a boolean: 46 - schema.json:18:29 46 + schema.json:18:30 47 47 from version https://json-schema.org/draft/2020-12/schema onwards, the value of "items" must be an object or a boolean: 48 - schema.json:21:29 48 + schema.json:21:30 49 49 from version https://json-schema.org/draft/2020-12/schema onwards, the value of "items" must be an object or a boolean: 50 - schema.json:30:29 50 + schema.json:30:30