Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling version string in code generated header #3509

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion cmd/goa/gen.go
davideme marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (g *Generator) Write(_ bool) error {
codegen.NewImport("_", g.DesignPath),
}
sections = []*codegen.SectionTemplate{
codegen.Header("Code Generator", "main", imports),
codegen.Header("Code Generator", "main", imports, false),
{
Name: "main",
Source: mainT,
Expand Down
2 changes: 1 addition & 1 deletion codegen/example/example_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func exampleCLIMain(_ string, root *expr.RootExpr, svr *expr.ServerExpr) *codege
codegen.GoaImport(""),
}
sections := []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
codegen.Header("", "main", specs, false),
{
Name: "cli-main-start",
Source: readTemplate("client_start"),
Expand Down
2 changes: 1 addition & 1 deletion codegen/example/example_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func exampleSvrMain(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *c
specs = append(specs, &codegen.ImportSpec{Path: rootPath, Name: apiPkg})

sections := []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
codegen.Header("", "main", specs, false),
{
Name: "server-main-start",
Source: readTemplate("server_start"),
Expand Down
10 changes: 7 additions & 3 deletions codegen/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
)

// Header returns a Go source file header section template.
func Header(title, pack string, imports []*ImportSpec) *SectionTemplate {
func Header(title, pack string, imports []*ImportSpec, disableVersion bool) *SectionTemplate {
var toolVersion string
if !disableVersion {
toolVersion = goa.Version()
}
return &SectionTemplate{
Name: "source-header",
Source: headerT,
Data: map[string]any{
"Title": title,
"ToolVersion": goa.Version(),
"ToolVersion": toolVersion,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be consistent maybe this should be disableVersion and the test in the template should be {{ if not .DisableVersion }}?

"Pkg": pack,
"Imports": imports,
},
Expand All @@ -34,7 +38,7 @@ func AddImport(section *SectionTemplate, imprts ...*ImportSpec) {
}

const (
headerT = `{{if .Title}}// Code generated by goa {{.ToolVersion}}, DO NOT EDIT.
headerT = `{{if .Title}}// Code generated by goa{{if .ToolVersion}} {{.ToolVersion}}{{end}}, DO NOT EDIT.
//
// {{.Title}}
//
Expand Down
36 changes: 24 additions & 12 deletions codegen/sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ import (
package testpackage

`, goa.Version())
titleHeaderDisableVersion = `// Code generated by goa, DO NOT EDIT.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we call this titleHeaderVersionDisabled to make it a qualified name?

//
// test title
//
// Command:
// goa

package testpackage

`
imprt = []*ImportSpec{{Path: "test"}}
imports = append(imprt, &ImportSpec{Path: "other"})
pathImport = []*ImportSpec{{Path: "import/with/slashes"}}
Expand All @@ -76,22 +86,24 @@ package testpackage
pathNamedImports = append(pathNamedImport, &ImportSpec{Name: "myothername", Path: "other/import/with/slashes"})
)
cases := map[string]struct {
Title string
Imports []*ImportSpec
Expected string
Title string
Imports []*ImportSpec
Expected string
DisableVersion bool
}{
"no-title": {Expected: noTitleHeader},
"title": {Title: title, Expected: titleHeader},
"single-import": {Imports: imprt, Expected: singleImportHeader},
"many-imports": {Imports: imports, Expected: manyImportsHeader},
"path-import": {Imports: pathImport, Expected: pathImportHeader},
"path-imports": {Imports: pathImports, Expected: pathImportsHeader},
"path-named-import": {Imports: pathNamedImport, Expected: pathNamedImportHeader},
"path-named-imports": {Imports: pathNamedImports, Expected: pathNamedImportsHeader},
"no-title": {Expected: noTitleHeader},
"title": {Title: title, Expected: titleHeader},
"title-disable-version": {Title: title, DisableVersion: true, Expected: titleHeaderDisableVersion},
"single-import": {Imports: imprt, Expected: singleImportHeader},
"many-imports": {Imports: imports, Expected: manyImportsHeader},
"path-import": {Imports: pathImport, Expected: pathImportHeader},
"path-imports": {Imports: pathImports, Expected: pathImportsHeader},
"path-named-import": {Imports: pathNamedImport, Expected: pathNamedImportHeader},
"path-named-imports": {Imports: pathNamedImports, Expected: pathNamedImportsHeader},
}
for k, tc := range cases {
buf := new(bytes.Buffer)
s := Header(tc.Title, "testpackage", tc.Imports)
s := Header(tc.Title, "testpackage", tc.Imports, tc.DisableVersion)
s.Write(buf) // nolint: errcheck
actual := buf.String()
if actual != tc.Expected {
Expand Down
4 changes: 3 additions & 1 deletion codegen/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func ClientFile(_ string, service *expr.ServiceExpr) *codegen.File {
codegen.GoaImport(""),
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" client", svc.PkgName, imports)
value, ok := service.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
header := codegen.Header(service.Name+" client", svc.PkgName, imports, disableVersion)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we wouldn't be better of passing the metadata object to codegen.Header and moving the logic that computes disableVersion there:

  • This would remove the need for having this computation spread everywhere
  • This would allow for other metadata based tweaks in the future when generating file headers

def := &codegen.SectionTemplate{
Name: "client-struct",
Source: readTemplate("service_client"),
Expand Down
4 changes: 3 additions & 1 deletion codegen/service/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ func ConvertFile(root *expr.RootExpr, service *expr.ServiceExpr) (*codegen.File,
pkgs = append(pkgs, &codegen.ImportSpec{Path: "context"})
pkgs = append(pkgs, codegen.GoaImport(""))
path := filepath.Join(codegen.Gendir, codegen.SnakeCase(service.Name), "convert.go")
value, ok := service.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections := []*codegen.SectionTemplate{
codegen.Header(service.Name+" service type conversion functions", svc.PkgName, pkgs),
codegen.Header(service.Name+" service type conversion functions", svc.PkgName, pkgs, disableVersion),
}

var (
Expand Down
4 changes: 3 additions & 1 deletion codegen/service/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func EndpointFile(genpkg string, service *expr.ServiceExpr) *codegen.File {
{Path: genpkg + "/" + svcName + "/" + "views", Name: svc.ViewsPkg},
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" endpoints", svc.PkgName, imports)
value, ok := service.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
header := codegen.Header(service.Name+" endpoints", svc.PkgName, imports, disableVersion)
def := &codegen.SectionTemplate{
Name: "endpoints-struct",
Source: readTemplate("service_endpoints"),
Expand Down
2 changes: 1 addition & 1 deletion codegen/service/example_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func exampleServiceFile(genpkg string, _ *expr.RootExpr, svc *expr.ServiceExpr,
{Path: "goa.design/goa/v3/security"},
}
sections := []*codegen.SectionTemplate{
codegen.Header("", apipkg, specs),
codegen.Header("", apipkg, specs, false),
{
Name: "basic-service-struct",
Source: readTemplate("service_struct"),
Expand Down
6 changes: 4 additions & 2 deletions codegen/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ func Files(genpkg string, service *expr.ServiceExpr, userTypePkgs map[string][]s
codegen.NewImport(svc.ViewsPkg, genpkg+"/"+svcName+"/views"),
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" service", svc.PkgName, imports)
value, ok := service.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
header := codegen.Header(service.Name+" service", svc.PkgName, imports, disableVersion)
def := &codegen.SectionTemplate{
Name: "service",
Source: readTemplate("service"),
Expand Down Expand Up @@ -228,7 +230,7 @@ func Files(genpkg string, service *expr.ServiceExpr, userTypePkgs map[string][]s
}
fullRelPath := filepath.Join(codegen.Gendir, p)
dir, _ := filepath.Split(fullRelPath)
h := codegen.Header("User types", codegen.Goify(filepath.Base(dir), false), nil)
h := codegen.Header("User types", codegen.Goify(filepath.Base(dir), false), nil, false)
sections := append([]*codegen.SectionTemplate{h}, secs...)
files = append(files, &codegen.File{Path: fullRelPath, SectionTemplates: sections})
}
Expand Down
11 changes: 6 additions & 5 deletions codegen/service/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ func ViewsFile(_ string, service *expr.ServiceExpr) *codegen.File {
sections []*codegen.SectionTemplate
)
{
header := codegen.Header(service.Name+" views", "views",
[]*codegen.ImportSpec{
codegen.GoaImport(""),
{Path: "unicode/utf8"},
})
value, ok := service.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
header := codegen.Header(service.Name+" views", "views", []*codegen.ImportSpec{
codegen.GoaImport(""),
{Path: "unicode/utf8"},
}, disableVersion)
sections = []*codegen.SectionTemplate{header}

// type definitions
Expand Down
8 changes: 6 additions & 2 deletions grpc/codegen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ func client(genpkg string, svc *expr.GRPCServiceExpr) *codegen.File {
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections = []*codegen.SectionTemplate{
codegen.Header(svc.Name()+" gRPC client", "client", imports),
codegen.Header(svc.Name()+" gRPC client", "client", imports, disableVersion),
}
sections = append(sections, &codegen.SectionTemplate{
Name: "client-struct",
Expand Down Expand Up @@ -136,7 +138,9 @@ func clientEncodeDecode(genpkg string, svc *expr.GRPCServiceExpr) *codegen.File
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC client encoders and decoders", "client", imports)}
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC client encoders and decoders", "client", imports, disableVersion)}
fm := transTmplFuncs(svc)
fm["metadataEncodeDecodeData"] = metadataEncodeDecodeData
fm["typeConversionData"] = typeConversionData
Expand Down
8 changes: 6 additions & 2 deletions grpc/codegen/client_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ func endpointParser(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr, da
specs = append(specs, sd.Service.UserTypeImports...)
}

value, ok := svr.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections := []*codegen.SectionTemplate{
codegen.Header(title, "cli", specs),
codegen.Header(title, "cli", specs, disableVersion),
cli.UsageCommands(data),
cli.UsageExamples(data),
{
Expand Down Expand Up @@ -121,8 +123,10 @@ func payloadBuilders(genpkg string, svc *expr.GRPCServiceExpr, data *cli.Command
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: sd.PkgName},
}
specs = append(specs, sd.Service.UserTypeImports...)
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", specs),
codegen.Header(title, "client", specs, disableVersion),
}
for _, sub := range data.Subcommands {
if sub.BuildFunction != nil {
Expand Down
4 changes: 3 additions & 1 deletion grpc/codegen/client_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func clientType(genpkg string, svc *expr.GRPCServiceExpr, _ map[string]struct{})
}
imports = append(imports, sd.Service.UserTypeImports...)
imports = append(imports, sd.Service.ProtoImports...)
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC client types", "client", imports)}
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC client types", "client", imports, disableVersion)}
for _, init := range initData {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-type-init",
Expand Down
2 changes: 1 addition & 1 deletion grpc/codegen/example_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func exampleCLI(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *codeg
)
{
sections = []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
codegen.Header("", "main", specs, false),
{Name: "do-grpc-cli", Source: grpcCLIDoT, Data: svrdata},
}
}
Expand Down
2 changes: 1 addition & 1 deletion grpc/codegen/example_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func exampleServer(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *co
}
}
sections = []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
codegen.Header("", "main", specs, false),
{
Name: "server-grpc-start",
Source: grpcSvrStartT,
Expand Down
8 changes: 6 additions & 2 deletions grpc/codegen/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func serverFile(genpkg string, svc *expr.GRPCServiceExpr) *codegen.File {
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections = []*codegen.SectionTemplate{
codegen.Header(svc.Name()+" gRPC server", "server", imports),
codegen.Header(svc.Name()+" gRPC server", "server", imports, disableVersion),
{Name: "server-struct", Source: serverStructT, Data: data},
}
for _, e := range data.Endpoints {
Expand Down Expand Up @@ -141,7 +143,9 @@ func serverEncodeDecode(genpkg string, svc *expr.GRPCServiceExpr) *codegen.File
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
sections = []*codegen.SectionTemplate{codegen.Header(title, "server", imports)}
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections = []*codegen.SectionTemplate{codegen.Header(title, "server", imports, disableVersion)}

for _, e := range data.Endpoints {
if e.Response.ServerConvert != nil {
Expand Down
4 changes: 3 additions & 1 deletion grpc/codegen/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func serverType(genpkg string, svc *expr.GRPCServiceExpr, _ map[string]struct{})
}
imports = append(imports, sd.Service.UserTypeImports...)
imports = append(imports, sd.Service.ProtoImports...)
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC server types", "server", imports)}
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC server types", "server", imports, disableVersion)}
for _, init := range initData {
if _, ok := foundInits[init.Name]; ok {
continue
Expand Down
8 changes: 6 additions & 2 deletions http/codegen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func clientFile(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File {
svcName := data.Service.PathName
path := filepath.Join(codegen.Gendir, "http", svcName, "client", "client.go")
title := fmt.Sprintf("%s client HTTP transport", svc.Name())
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", []*codegen.ImportSpec{
{Path: "context"},
Expand All @@ -47,7 +49,7 @@ func clientFile(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File {
codegen.GoaNamedImport("http", "goahttp"),
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}),
}, disableVersion),
}
sections = append(sections, &codegen.SectionTemplate{
Name: "client-struct",
Expand Down Expand Up @@ -114,7 +116,9 @@ func clientEncodeDecodeFile(genpkg string, svc *expr.HTTPServiceExpr) *codegen.F
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}
imports = append(imports, data.Service.UserTypeImports...)
sections := []*codegen.SectionTemplate{codegen.Header(title, "client", imports)}
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections := []*codegen.SectionTemplate{codegen.Header(title, "client", imports, disableVersion)}

for _, e := range data.Endpoints {
sections = append(sections, &codegen.SectionTemplate{
Expand Down
8 changes: 6 additions & 2 deletions http/codegen/client_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ func endpointParser(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr, da
cliData[i] = cmd.CommandData
}

value, ok := svr.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections := []*codegen.SectionTemplate{
codegen.Header(title, "cli", specs),
codegen.Header(title, "cli", specs, disableVersion),
cli.UsageCommands(cliData),
cli.UsageExamples(cliData),
{
Expand Down Expand Up @@ -175,8 +177,10 @@ func payloadBuilders(genpkg string, svc *expr.HTTPServiceExpr, data *cli.Command
{Path: genpkg + "/" + sd.Service.PathName, Name: sd.Service.PkgName},
}
specs = append(specs, sd.Service.UserTypeImports...)
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", specs),
codegen.Header(title, "client", specs, disableVersion),
}
for _, sub := range data.Subcommands {
if sub.BuildFunction != nil {
Expand Down
4 changes: 3 additions & 1 deletion http/codegen/client_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func clientType(genpkg string, svc *expr.HTTPServiceExpr, seen map[string]struct
codegen.GoaImport(""),
}
imports = append(imports, data.Service.UserTypeImports...)
header := codegen.Header(svc.Name()+" HTTP client types", "client", imports)
value, ok := svc.Meta.Last("goa:version:disable")
disableVersion := ok && value == "true"
header := codegen.Header(svc.Name()+" HTTP client types", "client", imports, disableVersion)

var (
initData []*InitData
Expand Down
2 changes: 1 addition & 1 deletion http/codegen/example_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func exampleCLI(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *codeg
}
}
sections := []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
codegen.Header("", "main", specs, false),
{
Name: "cli-http-start",
Source: readTemplate("cli_start"),
Expand Down
4 changes: 2 additions & 2 deletions http/codegen/example_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func exampleServer(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *co
}

sections := []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
codegen.Header("", "main", specs, false),
{
Name: "server-http-start",
Source: readTemplate("server_start"),
Expand Down Expand Up @@ -166,7 +166,7 @@ func dummyMultipartFile(genpkg string, root *expr.RootExpr, svc *expr.HTTPServic
})

apiPkg := scope.Unique(strings.ToLower(codegen.Goify(root.API.Name, false)), "api")
sections = []*codegen.SectionTemplate{codegen.Header("", apiPkg, specs)}
sections = []*codegen.SectionTemplate{codegen.Header("", apiPkg, specs, false)}
for _, e := range data.Endpoints {
if e.MultipartRequestDecoder != nil {
mustGen = true
Expand Down
Loading