-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
133 lines (113 loc) · 3.68 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//go:generate protoc -I pb -I proto --go_opt=Moption.proto=.;pb,Mqbit/errors.proto=.;pb --go_out=./pb pb/option.proto proto/qbit/errors.proto
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/go-qbit/protoc-gen-go-grpc-errors/pb"
)
const (
fileDescriptorProtoPackageFieldNumber = 2
fileDescriptorProtoSyntaxFieldNumber = 12
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
statusPackage = protogen.GoImportPath("google.golang.org/grpc/status")
)
var reSplit = regexp.MustCompile(`[\s_]+`)
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if len(file.Services) == 0 {
return nil
}
filename := file.GeneratedFilenamePrefix + "_grpc_errors.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
// Attach all comments associated with the syntax field.
genLeadingComments(g, file.Desc.SourceLocations().ByPath(protoreflect.SourcePath{fileDescriptorProtoSyntaxFieldNumber}))
g.P("// Code generated by protoc-gen-go-grpc-errors. DO NOT EDIT.")
g.P("// versions:")
g.P("// - protoc-gen-go-grpc-errors v", version)
g.P("// - protoc ", protocVersion(gen))
if file.Proto.GetOptions().GetDeprecated() {
g.P("// ", file.Desc.Path(), " is a deprecated file.")
} else {
g.P("// source: ", file.Desc.Path())
}
g.P()
// Attach all comments associated with the package field.
genLeadingComments(g, file.Desc.SourceLocations().ByPath(protoreflect.SourcePath{fileDescriptorProtoPackageFieldNumber}))
g.P("package ", file.GoPackageName)
g.P()
generateFileContent(gen, file, g)
return g
}
func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
if len(file.Services) == 0 {
return
}
g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the grpc package it is being compiled against.")
g.P("// Requires gRPC-Go v1.32.0 or later.")
g.P("const _ = ", grpcPackage.Ident("SupportPackageIsVersion7"))
g.P()
for _, service := range file.Services {
genService(gen, file, g, service)
}
}
func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service) {
g.P(protogen.Comments(service.GoName))
g.P("var (")
for _, m := range service.Methods {
data, err := proto.Marshal(m.Desc.Options())
if err != nil {
gen.Error(err)
}
qOption := &pb.QBitMethodOptions{}
if err := proto.Unmarshal(data, qOption); err != nil {
gen.Error(err)
}
if len(qOption.Error) == 0 {
continue
}
g.P("Errors", m.GoName, " = struct {")
for _, e := range qOption.Error {
g.P(stringToGoId(e.Message), " error")
}
g.P("}{")
for _, e := range qOption.Error {
g.P(stringToGoId(e.Message), ": ", statusPackage.Ident("Error("+strconv.FormatInt(int64(e.Code), 10)+`, "`+e.Message+`"),`))
}
g.P("}")
g.P()
}
g.P(")")
}
func genLeadingComments(g *protogen.GeneratedFile, loc protoreflect.SourceLocation) {
for _, s := range loc.LeadingDetachedComments {
g.P(protogen.Comments(s))
g.P()
}
if s := loc.LeadingComments; s != "" {
g.P(protogen.Comments(s))
g.P()
}
}
func protocVersion(gen *protogen.Plugin) string {
v := gen.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}
func stringToGoId(s string) string {
parts := reSplit.Split(s, -1)
for i, s := range parts {
parts[i] = strings.ToUpper(s[:1]) + s[1:]
}
return strings.Join(parts, "")
}