this repo has no description
0
fork

Configure Feed

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

cmd/cue: quote Go field tags if they are invalid CUE attribute bodies

Fixes #3436.

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

+27 -4
+23
cmd/cue/cmd/get_go.go
··· 1346 1346 } 1347 1347 1348 1348 func (e *extractor) addAttr(f *cueast.Field, tag, body string) { 1349 + if attrBodyNeedsQuoting(body) { 1350 + body = literal.String.Quote(body) 1351 + } 1349 1352 s := fmt.Sprintf("@%s(%s)", tag, body) 1350 1353 f.Attrs = append(f.Attrs, &cueast.Attribute{Text: s}) 1354 + } 1355 + 1356 + func attrBodyNeedsQuoting(s string) bool { 1357 + // TODO(mvdan): just like we have [cueast.StringLabelNeedsQuoting], 1358 + // we could add an API to tell whether an attribute body needs to be quoted. 1359 + // For now, use the CUE parser for this purpose, which works okay. 1360 + src := "@x(" + s + ")" 1361 + f, err := parser.ParseFile("", src) 1362 + if err != nil { 1363 + return true 1364 + } 1365 + // Make sure we parsed exactly one attribute with the content we expect. 1366 + if len(f.Decls) != 1 { 1367 + return true 1368 + } 1369 + attr, ok := f.Decls[0].(*cueast.Attribute) 1370 + if !ok || attr.Text != src { 1371 + return true 1372 + } 1373 + return false 1351 1374 } 1352 1375 1353 1376 func (e *extractor) addFields(x *types.Struct, st *cueast.StructLit) {
+4 -4
cmd/cue/cmd/testdata/script/get_go_types.txtar
··· 19 19 cmp go.mod go.mod.golden 20 20 21 21 # Check that the resulting CUE is valid 22 - ! exec cue vet -c=false ./... 22 + exec cue vet -c=false ./... 23 23 24 24 -- go.mod -- 25 25 module mod.test ··· 547 547 Err: _ @go(,error) 548 548 549 549 // Field tag strings which need quoting as CUE attributes. 550 - Slashes: _ @go(,any) @xml(https://foo.test/bar#fragment) 551 - Parens: _ @go(,any) @xml(closing)parens)) 552 - InjectCUE: _ @go(,any) @xml(), x: 4 @x() 550 + Slashes: _ @go(,any) @xml("https://foo.test/bar#fragment") 551 + Parens: _ @go(,any) @xml("closing)parens)") 552 + InjectCUE: _ @go(,any) @xml("), x: 4 @x(") 553 553 554 554 #Inline 555 555 }