Skip to content

Commit

Permalink
Add compiler and plugin version info to header comment (#12)
Browse files Browse the repository at this point in the history
* add version info to template generation

* regenerate examples

* change imports of plugin

* add  CHANGELOG entry
  • Loading branch information
alehechka authored Jul 31, 2022
1 parent 4938ea4 commit b218024
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
7 changes: 6 additions & 1 deletion example/greeter/greeter/greeter.graphql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion example/starwars/spec/starwars/starwars.graphql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion protoc-gen-graphql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 46 additions & 4 deletions protoc-gen-graphql/spec/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 ""
}
27 changes: 27 additions & 0 deletions protoc-gen-graphql/spec/package.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package spec

import (
"fmt"
"path/filepath"
"strings"

plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/iancoleman/strcase"
)

Expand All @@ -17,19 +19,31 @@ 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 {
p := &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 != "" {
Expand Down Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions protoc-gen-graphql/spec/ptypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion protoc-gen-graphql/template/template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b218024

Please sign in to comment.