From b218024eaf8fd185833389cab1d0c5664e2e52eb Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Sun, 31 Jul 2022 09:26:15 -0500 Subject: [PATCH] Add compiler and plugin version info to header comment (#12) * add version info to template generation * regenerate examples * change imports of plugin * add CHANGELOG entry --- CHANGELOG.md | 4 ++ example/greeter/greeter/greeter.graphql.go | 7 ++- .../spec/starwars/starwars.graphql.go | 7 ++- protoc-gen-graphql/main.go | 6 ++- protoc-gen-graphql/spec/file.go | 50 +++++++++++++++++-- protoc-gen-graphql/spec/package.go | 27 ++++++++++ protoc-gen-graphql/spec/ptypes.go | 14 +++--- protoc-gen-graphql/template/template.go | 7 ++- 8 files changed, 107 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a20822..2b1352d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## [v0.2.4](https://github.com/alehechka/grpc-graphql-gateway/releases/tag/v0.2.4) + +- Add compiler and plugin version info to header comment ([#12](https://github.com/alehechka/grpc-graphql-gateway/pull/12)) + ## [v0.2.3](https://github.com/alehechka/grpc-graphql-gateway/releases/tag/v0.2.3) - Resolve issues with goreleaser action [v0.2.3] ([#11](https://github.com/alehechka/grpc-graphql-gateway/pull/11)) diff --git a/example/greeter/greeter/greeter.graphql.go b/example/greeter/greeter/greeter.graphql.go index 4c3e1ee..4ee5c30 100644 --- a/example/greeter/greeter/greeter.graphql.go +++ b/example/greeter/greeter/greeter.graphql.go @@ -1,4 +1,9 @@ -// Code generated by proroc-gen-graphql, DO NOT EDIT. +// Code generated by protoc-gen-graphql. DO NOT EDIT. +// versions: +// protoc-gen-graphql dev +// protoc v3.21.4 +// source: greeter.proto + package greeter import ( diff --git a/example/starwars/spec/starwars/starwars.graphql.go b/example/starwars/spec/starwars/starwars.graphql.go index 8959c61..29a8d32 100644 --- a/example/starwars/spec/starwars/starwars.graphql.go +++ b/example/starwars/spec/starwars/starwars.graphql.go @@ -1,4 +1,9 @@ -// Code generated by proroc-gen-graphql, DO NOT EDIT. +// Code generated by protoc-gen-graphql. DO NOT EDIT. +// versions: +// protoc-gen-graphql dev +// protoc v3.21.4 +// source: starwars/starwars.proto + package starwars import ( diff --git a/protoc-gen-graphql/main.go b/protoc-gen-graphql/main.go index 29b3668..ca9cb97 100644 --- a/protoc-gen-graphql/main.go +++ b/protoc-gen-graphql/main.go @@ -72,7 +72,11 @@ func main() { // in order to access easily plugin options, package name, comment, etc... var files []*spec.File for _, f := range req.GetProtoFile() { - files = append(files, spec.NewFile(f, req.GetCompilerVersion(), args.FieldCamelCase)) + files = append(files, spec.NewFile(f, &spec.FileConfig{ + IsCamel: args.FieldCamelCase, + CompilerVersion: req.GetCompilerVersion(), + PluginVersion: version, + })) } g := generator.New(files, args) diff --git a/protoc-gen-graphql/spec/file.go b/protoc-gen-graphql/spec/file.go index bc093aa..5bc844e 100644 --- a/protoc-gen-graphql/spec/file.go +++ b/protoc-gen-graphql/spec/file.go @@ -22,24 +22,52 @@ type File struct { isCamel bool + compilerVersion *plugin.Version + pluginVersion string +} + +type FileConfig struct { + IsCamel bool CompilerVersion *plugin.Version + PluginVersion string +} + +func (c *FileConfig) GetIsCamel() bool { + if c != nil { + return c.IsCamel + } + return false +} + +func (c *FileConfig) GetCompilerVersion() *plugin.Version { + if c != nil { + return c.CompilerVersion + } + return nil +} + +func (c *FileConfig) GetPluginVersion() string { + if c != nil { + return c.PluginVersion + } + return "" } func NewFile( d *descriptor.FileDescriptorProto, - cv *plugin.Version, - isCamel bool, + config *FileConfig, ) *File { f := &File{ - CompilerVersion: cv, + compilerVersion: config.GetCompilerVersion(), + pluginVersion: config.GetPluginVersion(), descriptor: d, comments: makeComments(d), services: make([]*Service, 0), messages: make([]*Message, 0), enums: make([]*Enum, 0), - isCamel: isCamel, + isCamel: config.GetIsCamel(), } for i, s := range d.GetService() { f.services = append(f.services, NewService(s, f, 6, i)) @@ -125,3 +153,17 @@ func (f *File) getComment(paths []int) string { } return "" } + +func (f *File) CompilerVersion() *plugin.Version { + if f != nil { + return f.compilerVersion + } + return nil +} + +func (f *File) PluginVersion() string { + if f != nil { + return f.pluginVersion + } + return "" +} diff --git a/protoc-gen-graphql/spec/package.go b/protoc-gen-graphql/spec/package.go index a53fc46..82726dd 100644 --- a/protoc-gen-graphql/spec/package.go +++ b/protoc-gen-graphql/spec/package.go @@ -1,9 +1,11 @@ package spec import ( + "fmt" "path/filepath" "strings" + plugin "github.com/golang/protobuf/protoc-gen-go/plugin" "github.com/iancoleman/strcase" ) @@ -17,12 +19,19 @@ type PackageGetter interface { Filename() string } +type Versionable interface { + CompilerVersion() *plugin.Version + PluginVersion() string +} + type Package struct { Name string CamelName string Path string GeneratedFilenamePrefix string FileName string + CompilerVersion string + PluginVersion string } func NewPackage(g PackageGetter) *Package { @@ -30,6 +39,11 @@ func NewPackage(g PackageGetter) *Package { p.GeneratedFilenamePrefix = strings.TrimSuffix(g.Filename(), filepath.Ext(g.Filename())) p.FileName = filepath.Base(p.GeneratedFilenamePrefix) + if c, ok := g.(Versionable); ok { + p.CompilerVersion = compilerVersionString(c.CompilerVersion()) + p.PluginVersion = c.PluginVersion() + } + if pkg := g.GoPackage(); pkg != "" { p.Name, p.Path = ParsePackagePathName(pkg) } else if pkg := g.Package(); pkg != "" { @@ -75,3 +89,16 @@ func ParsePackagePathName(pkg string) (name string, path string) { return } + +func compilerVersionString(cv *plugin.Version) string { + if isBufCompiled(cv) { + return "(unknown)" + } + + ver := fmt.Sprintf("v%d.%d.%d", cv.GetMajor(), cv.GetMinor(), cv.GetPatch()) + if suffix := cv.GetSuffix(); len(suffix) > 0 { + ver += fmt.Sprintf("-%s", suffix) + } + + return ver +} diff --git a/protoc-gen-graphql/spec/ptypes.go b/protoc-gen-graphql/spec/ptypes.go index 8d420d6..0c622ea 100644 --- a/protoc-gen-graphql/spec/ptypes.go +++ b/protoc-gen-graphql/spec/ptypes.go @@ -25,13 +25,13 @@ var supportedGoogleTypes = []string{ "money", } -func getSupportedPtypeNames(cv *plugin.Version) []string { - // TODO: buf.build does not currently supply a version and thus must be accounted for to use google.protobuf graphql types correctly - if cv.GetMajor() == 0 && cv.GetMinor() == 0 && cv.GetPatch() == 0 && cv.GetSuffix() == "" { - return supportedProtobufTypesLaterV3_14_0 - } +// TODO: buf.build does not currently supply a version and thus must be accounted for to use google.protobuf graphql types correctly +func isBufCompiled(cv *plugin.Version) bool { + return cv.GetMajor() == 0 && cv.GetMinor() == 0 && cv.GetPatch() == 0 && cv.GetSuffix() == "" +} - if cv.GetMajor() >= 3 && cv.GetMinor() >= 14 { +func getSupportedPtypeNames(cv *plugin.Version) []string { + if isBufCompiled(cv) || cv.GetMajor() >= 3 && cv.GetMinor() >= 14 { return supportedProtobufTypesLaterV3_14_0 } return supportedProtobufTypes @@ -42,7 +42,7 @@ func getImplementedPtypes(m *Message) (string, error) { var found bool if pkg := m.Package(); pkg == "google.protobuf" { - for _, v := range getSupportedPtypeNames(m.CompilerVersion) { + for _, v := range getSupportedPtypeNames(m.CompilerVersion()) { if ptype == v { found = true } diff --git a/protoc-gen-graphql/template/template.go b/protoc-gen-graphql/template/template.go index 4a101e6..64cf8b6 100644 --- a/protoc-gen-graphql/template/template.go +++ b/protoc-gen-graphql/template/template.go @@ -1,7 +1,12 @@ package template var GoTemplate = ` -// Code generated by proroc-gen-graphql, DO NOT EDIT. +// Code generated by protoc-gen-graphql. DO NOT EDIT. +// versions: +// protoc-gen-graphql {{ .RootPackage.PluginVersion }} +// protoc {{ .RootPackage.CompilerVersion }} +// source: {{ .RootPackage.GeneratedFilenamePrefix }}.proto + package {{ .RootPackage.Name }} import (