|
|
@@ -11,6 +11,7 @@ import (
|
|
|
"go/parser"
|
|
|
"go/token"
|
|
|
"math"
|
|
|
+ "runtime"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"unicode"
|
|
|
@@ -25,6 +26,9 @@ import (
|
|
|
"google.golang.org/protobuf/types/descriptorpb"
|
|
|
)
|
|
|
|
|
|
+// GenerateVersionMarkers specifies whether to generate version markers.
|
|
|
+var GenerateVersionMarkers = true
|
|
|
+
|
|
|
const (
|
|
|
// generateEnumJSONMethods specifies whether to generate the UnmarshalJSON
|
|
|
// method for proto2 enums.
|
|
|
@@ -145,28 +149,21 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
|
|
|
}
|
|
|
|
|
|
genStandaloneComments(g, f, fieldnum.FileDescriptorProto_Syntax)
|
|
|
-
|
|
|
- g.P("// Code generated by protoc-gen-go. DO NOT EDIT.")
|
|
|
- if f.Proto.GetOptions().GetDeprecated() {
|
|
|
- g.P("// ", f.Desc.Path(), " is a deprecated file.")
|
|
|
- } else {
|
|
|
- g.P("// source: ", f.Desc.Path())
|
|
|
- }
|
|
|
- g.P()
|
|
|
-
|
|
|
+ genGeneratedHeader(gen, g, f)
|
|
|
genStandaloneComments(g, f, fieldnum.FileDescriptorProto_Package)
|
|
|
-
|
|
|
g.P("package ", f.GoPackageName)
|
|
|
g.P()
|
|
|
|
|
|
// Emit a static check that enforces a minimum version of the proto package.
|
|
|
- g.P("const (")
|
|
|
- g.P("// Verify that runtime/protoimpl is sufficiently up-to-date.")
|
|
|
- g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimplPackage.Ident("MaxVersion"), " - ", protoimpl.Version, ")")
|
|
|
- g.P("// Verify that this generated code is sufficiently up-to-date.")
|
|
|
- g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimpl.Version, " - ", protoimplPackage.Ident("MinVersion"), ")")
|
|
|
- g.P(")")
|
|
|
- g.P()
|
|
|
+ if GenerateVersionMarkers {
|
|
|
+ g.P("const (")
|
|
|
+ g.P("// Verify that this generated code is sufficiently up-to-date.")
|
|
|
+ g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimpl.GenVersion, " - ", protoimplPackage.Ident("MinVersion"), ")")
|
|
|
+ g.P("// Verify that runtime/protoimpl is sufficiently up-to-date.")
|
|
|
+ g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimplPackage.Ident("MaxVersion"), " - ", protoimpl.GenVersion, ")")
|
|
|
+ g.P(")")
|
|
|
+ g.P()
|
|
|
+ }
|
|
|
|
|
|
for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {
|
|
|
genImport(gen, g, f, imps.Get(i))
|
|
|
@@ -209,6 +206,33 @@ func genStandaloneComments(g *protogen.GeneratedFile, f *fileInfo, n int32) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func genGeneratedHeader(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
|
|
|
+ g.P("// Code generated by protoc-gen-go. DO NOT EDIT.")
|
|
|
+
|
|
|
+ if GenerateVersionMarkers {
|
|
|
+ g.P("// versions:")
|
|
|
+ protocGenGoVersion := protoimpl.VersionString()
|
|
|
+ protocVersion := "(unknown)"
|
|
|
+ if v := gen.Request.GetCompilerVersion(); v != nil {
|
|
|
+ protocVersion = fmt.Sprintf("v%v.%v.%v", v.GetMajor(), v.GetMinor(), v.GetPatch())
|
|
|
+ }
|
|
|
+ goVersion := runtime.Version()
|
|
|
+ if strings.HasPrefix(goVersion, "go") {
|
|
|
+ goVersion = "v" + goVersion[len("go"):]
|
|
|
+ }
|
|
|
+ g.P("// \tprotoc-gen-go ", protocGenGoVersion)
|
|
|
+ g.P("// \tprotoc ", protocVersion)
|
|
|
+ g.P("// \tgo ", goVersion)
|
|
|
+ }
|
|
|
+
|
|
|
+ if f.Proto.GetOptions().GetDeprecated() {
|
|
|
+ g.P("// ", f.Desc.Path(), " is a deprecated file.")
|
|
|
+ } else {
|
|
|
+ g.P("// source: ", f.Desc.Path())
|
|
|
+ }
|
|
|
+ g.P()
|
|
|
+}
|
|
|
+
|
|
|
func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp protoreflect.FileImport) {
|
|
|
impFile, ok := gen.FileByName(imp.Path())
|
|
|
if !ok {
|