this repo has no description
0
fork

Configure Feed

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

cmd/cue: wire up xml+koala via encoding/xml/koala

This is only the encoder side; the decoder side will be implemented
at a later time.

Unlike other encodings with interpretations, like json+jsonschema,
here we add "koala" as a boolean tag to the encoding "xml" so that
`xml+koala` can be used as a filetype, but not `koala` by itself.

"koala" is a made up word that we don't want to promote to the top
level namespace for encodings, and encodings like XML and INI will need
multiple encoding variants to be named, so we don't want all of them
to be promoted to the top level by default.

See #3776.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ic1c56f69f2afadf5596887286ab78170ba484ad1
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1211720
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+151 -2
+1 -1
cmd/cue/cmd/common.go
··· 429 429 f.Interpretation = p.cfg.interpretation 430 430 } 431 431 switch f.Encoding { 432 - case build.Protobuf, build.YAML, build.TOML, build.JSON, build.JSONL, 432 + case build.Protobuf, build.YAML, build.TOML, build.XML, build.JSON, build.JSONL, 433 433 build.Text, build.Binary: 434 434 if f.Interpretation == build.ProtobufJSON { 435 435 // Need a schema.
+89
cmd/cue/cmd/testdata/script/encoding_xml_koala.txtar
··· 1 + # Test that the XML Koala encoding is fully supported in cmd/cue. 2 + # Note that we allow both "xml+koala" and just "koala", 3 + # even though the latter by itself is a made up word. 4 + # This is consistent with other encoding interpretations, 5 + # and should not cause conflicts as "koala" is rather unusual. 6 + 7 + # An XML interpretation like Koala must be specified. 8 + ! exec cue export --out xml . 9 + cmp stderr encode-xml.stderr 10 + 11 + # We don't support encoding into XML yet. 12 + ! exec cue export --out xml+koala . 13 + cmp stderr encode-xml-koala.stderr 14 + 15 + # An XML interpretation like Koala must be specified. 16 + ! exec cue export export.xml 17 + cmp stderr decode-xml.stderr 18 + 19 + # Koala by itself is not a top-level encoding, unlike jsonschema. 20 + ! exec cue export koala: export.xml 21 + cmp stderr decode-koala.stderr 22 + 23 + # Specifying xml+koala works, no matter the file input filename. 24 + exec cue export xml+koala: export.xml 25 + cmp stdout export.json 26 + stdin export.xml 27 + exec cue export xml+koala: export.xml 28 + cmp stdout export.json 29 + 30 + exec cue import -o - xml+koala: export.xml 31 + cmp stdout import.cue 32 + 33 + -- encode-xml.stderr -- 34 + unsupported encoding "xml" 35 + -- encode-xml-koala.stderr -- 36 + unsupported encoding "xml" 37 + -- decode-xml.stderr -- 38 + xml requires a variant, such as: xml+koala 39 + -- decode-koala.stderr -- 40 + unknown filetype koala 41 + -- export.xml -- 42 + <root> 43 + <message>Hello World!</message> 44 + <nested> 45 + <a1>one level</a1> 46 + <a2> 47 + <b>two levels</b> 48 + </a2> 49 + </nested> 50 + </root> 51 + -- export.json -- 52 + { 53 + "root": { 54 + "message": { 55 + "$$": "Hello World!" 56 + }, 57 + "nested": { 58 + "a1": { 59 + "$$": "one level" 60 + }, 61 + "a2": { 62 + "b": { 63 + "$$": "two levels" 64 + } 65 + } 66 + } 67 + } 68 + } 69 + -- import.cue -- 70 + root: { 71 + message: $$: "Hello World!" 72 + nested: { 73 + a1: $$: "one level" 74 + a2: b: $$: "two levels" 75 + } 76 + } 77 + -- data.cue -- 78 + package hello 79 + 80 + _who: "World" 81 + -- hello.cue -- 82 + package hello 83 + 84 + root: { 85 + message: "Hello \(_who)!" // who declared in data.cue 86 + 87 + nested: a1: "one level" 88 + nested: a2: b: "two levels" 89 + }
+1
cue/build/file.go
··· 47 47 JSON Encoding = "json" 48 48 YAML Encoding = "yaml" 49 49 TOML Encoding = "toml" 50 + XML Encoding = "xml" 50 51 JSONL Encoding = "jsonl" 51 52 Text Encoding = "text" 52 53 Binary Encoding = "binary"
+9
internal/encoding/encoding.go
··· 37 37 "cuelang.org/go/encoding/protobuf/jsonpb" 38 38 "cuelang.org/go/encoding/protobuf/textproto" 39 39 "cuelang.org/go/encoding/toml" 40 + "cuelang.org/go/encoding/xml/koala" 40 41 "cuelang.org/go/internal" 41 42 "cuelang.org/go/internal/encoding/yaml" 42 43 "cuelang.org/go/internal/filetypes" ··· 262 263 case build.TOML: 263 264 i.next = toml.NewDecoder(path, r).Decode 264 265 i.Next() 266 + case build.XML: 267 + switch { 268 + case f.BoolTags["koala"]: 269 + i.next = koala.NewDecoder(path, r).Decode 270 + i.Next() 271 + default: 272 + i.err = fmt.Errorf("xml requires a variant, such as: xml+koala") 273 + } 265 274 case build.Text: 266 275 b, err := io.ReadAll(r) 267 276 i.err = err
+33
internal/filetypes/filetypes_test.go
··· 235 235 Attributes: true, 236 236 }, 237 237 }, { 238 + name: "KoalaXML", 239 + in: build.File{ 240 + Filename: "foo.xml", 241 + BoolTags: map[string]bool{ 242 + "koala": true, 243 + }, 244 + }, 245 + mode: Def, 246 + out: &FileInfo{ 247 + File: &build.File{ 248 + Filename: "foo.xml", 249 + Encoding: "xml", 250 + Form: "data", 251 + BoolTags: map[string]bool{ 252 + "koala": true, 253 + }, 254 + }, 255 + Definitions: false, 256 + Data: true, 257 + Optional: false, 258 + Constraints: false, 259 + References: false, 260 + Cycles: false, 261 + KeepDefaults: false, 262 + Incomplete: false, 263 + Imports: false, 264 + Docs: true, 265 + Attributes: true, 266 + }, 267 + }, { 238 268 name: "OpenAPIDefaults", 239 269 in: build.File{ 240 270 Filename: "-", ··· 445 475 }, 446 476 }, 447 477 }, 478 + }, { 479 + in: "koala: bar.xml", 480 + out: "unknown filetype koala", 448 481 }, { 449 482 in: "jsonschema+strict: bar.schema", 450 483 out: []*build.File{
+18 -1
internal/filetypes/types.cue
··· 97 97 } 98 98 extensions: ".json": interpretation: *"auto" | _ 99 99 extensions: ".yaml": interpretation: *"auto" | _ 100 - extensions: ".yml": interpretation: *"auto" | _ 100 + extensions: ".yml": interpretation: *"auto" | _ 101 101 extensions: ".toml": interpretation: *"auto" | _ 102 + extensions: ".xml": interpretation: *"auto" | _ 102 103 } 103 104 104 105 modes: export: { ··· 160 161 ".yaml": tagInfo.yaml 161 162 ".yml": tagInfo.yaml 162 163 ".toml": tagInfo.toml 164 + ".xml": tagInfo.xml 163 165 ".txt": tagInfo.text 164 166 ".go": tagInfo.go 165 167 ".wasm": tagInfo.binary ··· 205 207 } 206 208 207 209 encodings: toml: { 210 + forms.data 211 + stream: false 212 + } 213 + 214 + encodings: xml: { 208 215 forms.data 209 216 stream: false 210 217 } ··· 329 336 jsonl: encoding: "jsonl" 330 337 yaml: encoding: "yaml" 331 338 toml: encoding: "toml" 339 + xml: { 340 + encoding: "xml" 341 + boolTags: { 342 + // We implement XML variants as boolean tags, such that "koala" is not accessible 343 + // as a top-level filetype, and can only be used via "xml+koala". 344 + // These effectively behave like a "one of", but we enforce this via the Go code 345 + // so that we can provide the users with good error messages. 346 + koala: *false | bool 347 + } 348 + } 332 349 proto: encoding: "proto" 333 350 textproto: encoding: "textproto" 334 351 // "binpb": encodings.binproto