this repo has no description
0
fork

Configure Feed

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

internal/encoding/gotypes: ignore definitions or fields via `@go(-)`

Support for the same mechanism to ignore entire packages or files
is left as a TODO, as it would require a slightly larger refactor
and it's presumably not a blocker for our own dogfooding yet.

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

+63 -19
+10 -3
cmd/cue/cmd/exp.go
··· 76 76 77 77 @go attributes can be used to override which name or type to be generated, for example: 78 78 79 - renamed: int @go(BetterName) 80 - retyped: string @go(,type=foo.com/bar.NamedString) 79 + package foo 80 + @go(betterpkgname) 81 + 82 + renamed: int @go(BetterName) 83 + retyped: string @go(,type=foo.com/bar.NamedString) 81 84 82 - TODO: support @go(,generate=true) to force a type to be generated or skipped 85 + The attribute "@go(-)" can be used to ignore a definition or field, for example: 83 86 87 + #ignoredDefinition: { 88 + @go(-) 89 + } 90 + ignoredField: int @go(-) 84 91 `[1:], 85 92 // TODO: write a long help text once the feature set is reasonably stable. 86 93 RunE: mkRunE(c, runExpGenGoTypes),
+26 -6
cmd/cue/cmd/testdata/script/exp_gengotypes.txtar
··· 1 1 exec cue exp gengotypes ./root 2 - # ! stderr . 2 + ! stderr . 3 3 4 4 # Check how many files were generated, and see that it aligns with how many files we expect. 5 5 find-files . ··· 213 213 ./cuetest/all.cue:5:24 214 214 ./cuetest/fail_check.cue:7:12 215 215 ./cuetest/fail_check.cue:7:17 216 - ./root/root.cue:81:8 216 + ./root/root.cue:83:8 217 217 fail.both.notString: conflicting values "not_a_struct" and {doc?:_} (mismatched types string and struct): 218 218 ./cuetest/all.cue:4:24 219 219 ./cuetest/fail_check.cue:7:12 220 220 ./cuetest/fail_check.cue:7:17 221 - ./root/root.cue:81:8 221 + ./root/root.cue:83:8 222 222 fail.cue."11_Int8".types.Int8: invalid value 99999 (out of bound <=127): 223 223 ./cuetest/all.cue:61:30 224 224 fail.cue."12_Int8".types.Int8: invalid value -99999 (out of bound >=-128): ··· 260 260 ./cuetest/all.cue:14:43 261 261 ./cuetest/fail_check.cue:7:12 262 262 ./cuetest/fail_check.cue:7:17 263 - ./root/root.cue:74:50 263 + ./root/root.cue:76:50 264 264 fail.cue.discBoth.discriminatorField.two: field not allowed: 265 265 ./cuetest/all.cue:14:51 266 266 ./cuetest/fail_check.cue:7:12 267 267 ./cuetest/fail_check.cue:7:17 268 - ./root/root.cue:74:29 268 + ./root/root.cue:76:29 269 269 fail.cue.isNotEqual.mustEqual2: conflicting values 8 and 99: 270 270 ./cuetest/all.cue:11:37 271 271 ./cuetest/all.cue:11:52 272 272 ./cuetest/fail_check.cue:7:12 273 273 ./cuetest/fail_check.cue:7:17 274 - ./root/root.cue:77:15 274 + ./root/root.cue:79:15 275 275 fail.cue."34_NumericBounds".types.NumericBounds: invalid value 5555 (out of bound <100): 276 276 ./root/types.cue:37:28 277 277 ./cuetest/all.cue:86:43 ··· 311 311 func main() { 312 312 // Sanity check that some types and fields are what we expect them to be. 313 313 var _ root.AttrChangedName 314 + var _ root.AttrChangedNameEmbed 314 315 var _ = root.AttrType(constant.Kind(0)) 315 316 316 317 // It's easier to test the fields with a root value. ··· 453 454 attrTypeCompat?: _#overridenNeverGenerate @go(,go/token.Token) 454 455 attrTypeNested?: {one?: two?: three?: int} @go(,type=map[any]any) 455 456 457 + attrIgnoreNeverGenerate?: int @go(-) 458 + 456 459 // doc is a great field. 457 460 // 458 461 // It deserves multiple paragraphs of documentation ··· 514 517 _#unusedHiddenStruct: neverGenerate?: int 515 518 516 519 #attrName: int @go(AttrChangedName) 520 + #attrNameEmbed: { 521 + @go(AttrChangedNameEmbed) 522 + 523 + field?: int 524 + } 517 525 #attrType: _#overridenNeverGenerate @go(,type=go/constant.Kind) 526 + #attrTypeEmbed: { 527 + @go(,type=go/constant.Kind) // obeyed even when the type has a different kind 528 + 529 + neverGenerate?: int 530 + } 531 + 532 + #attrIgnoreNeverGenerate: int @go(-) 533 + #attrIgnoreEmbedNeverGenerate: { 534 + @go(-) 535 + 536 + neverGenerate?: int 537 + } 518 538 -- root/types.cue -- 519 539 package root 520 540
+27 -10
internal/encoding/gotypes/generate.go
··· 69 69 g.addDef(cue.MakePath(sel)) 70 70 } 71 71 } 72 + // TODO: support ignoring an entire package via a @go(-) package attribute. 73 + // TODO: support ignoring an entire file via a @go(-) file attribute above a package clause. 72 74 for len(g.defsList) > 0 { 73 75 path := g.defsList[0] 74 76 g.defsList = g.defsList[1:] 75 77 76 78 val := instVal.LookupPath(path) 77 - goAttr := val.Attribute("go") 79 + goAttr := goValueAttr(val) 78 80 goName := goNameFromPath(path, true) 79 81 if goName == "" { 80 82 return fmt.Errorf("unexpected path in defsList: %q", path.String()) 81 83 } 82 84 if s, _ := goAttr.String(0); s != "" { 85 + if s == "-" { 86 + continue 87 + } 83 88 goName = s 84 89 } 85 90 ··· 202 207 // When possible, the Go type is emitted in the form of a reference. 203 208 // Otherwise, an inline Go type expression is used. 204 209 func (g *generator) emitType(val cue.Value, optional bool) error { 205 - goAttr := val.Attribute("go") 210 + goAttr := goValueAttr(val) 206 211 // We prefer the form @go(Name,type=pkg.Baz) as it is explicit and extensible, 207 212 // but we are also backwards compatible with @go(Name,pkg.Baz) as emitted by `cue get go`. 208 213 attrType, _, _ := goAttr.Lookup(1, "type") ··· 284 289 285 290 goAttr := val.Attribute("go") 286 291 if s, _ := goAttr.String(0); s != "" { 292 + if s == "-" { 293 + continue 294 + } 287 295 goName = s 288 296 } 289 297 ··· 390 398 return name 391 399 } 392 400 401 + // goValueAttr is like [cue.Value.Attribute] with the string parameter "go", 402 + // but it supports [cue.DeclAttr] attributes as well and not just [cue.FieldAttr]. 403 + // 404 + // TODO: surely this is a shortcoming of the method above? 405 + func goValueAttr(val cue.Value) cue.Attribute { 406 + attrs := val.Attributes(cue.ValueAttr) 407 + for _, attr := range attrs { 408 + if attr.Name() == "go" { 409 + return attr 410 + } 411 + } 412 + return cue.Attribute{} 413 + } 414 + 393 415 // goPkgNameForInstance determines what to name a Go package generated from a CUE instance. 394 416 // By default this is the CUE package name, but it can be overriden by a @go() package attribute. 395 417 func goPkgNameForInstance(inst *build.Instance, instVal cue.Value) string { 396 - attrs := instVal.Attributes(cue.DeclAttr) 397 - for _, attr := range attrs { 398 - if attr.Name() == "go" { 399 - if s, _ := attr.String(0); s != "" { 400 - return s 401 - } 402 - break 403 - } 418 + attr := goValueAttr(instVal) 419 + if s, _ := attr.String(0); s != "" { 420 + return s 404 421 } 405 422 return inst.PkgName 406 423 }