generated from cloudwego/.github
/
adopt_hz.go
189 lines (172 loc) · 4.5 KB
/
adopt_hz.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2022 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adopt
import (
"os"
"path/filepath"
"strings"
"github.com/cloudwego/protoc-gen-validator/validator"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)
func AdoptHz(gen *protogen.Plugin) error {
adopter := &HzAdopter{
Gen: gen,
}
adopter.Init()
newGen, err := adopter.newPluginGen()
if err != nil {
return err
}
newGen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
for _, f := range newGen.Files {
if strings.HasPrefix(f.Proto.GetPackage(), "google.protobuf") {
continue
}
g, err := validator.NewGenerator(newGen, f)
if err != nil {
return err
}
impt := string(f.GoImportPath)
if strings.HasPrefix(impt, adopter.GoModule) {
impt = impt[len(adopter.GoModule):]
}
f.GeneratedFilenamePrefix = filepath.Join(ImportToPath(impt, ""), BaseName(f.Proto.GetName(), ".proto"))
err = g.Generate()
if err != nil {
return err
}
}
*gen = *newGen
return nil
}
type HzAdopter struct {
Gen *protogen.Plugin
OutDir string
GoModule string
ModelDir string
}
func (ad *HzAdopter) Init() {
params := strings.Split(ad.Gen.Request.GetParameter(), ",")
// init parameters
ad.ModelDir = "biz/model"
ad.SetGoModule()
for _, param := range params {
if strings.Contains(param, "=") {
ret := strings.Split(param, "=")
if ret[0] == "out_dir" {
ad.OutDir = ret[1]
}
if ret[0] == "go_mod" {
ad.GoModule = ret[1]
}
if ret[0] == "model_dir" {
ad.ModelDir = ret[1]
}
}
}
}
func (ad *HzAdopter) newPluginGen() (*protogen.Plugin, error) {
oldRequest := ad.Gen.Request
ad.ModifyGoPackage(oldRequest)
newGen, err := protogen.Options{}.New(oldRequest)
if err != nil {
return nil, err
}
return newGen, nil
}
func (ad *HzAdopter) ModifyGoPackage(req *pluginpb.CodeGeneratorRequest) {
goMod := ad.GoModule
for _, f := range req.ProtoFile {
if strings.HasPrefix(f.GetPackage(), "google.protobuf") {
continue
}
goPkg := getGoPackage(f, nil)
if !strings.Contains(goPkg, goMod) {
if strings.HasPrefix(goPkg, "/") {
goPkg = goMod + goPkg
} else {
goPkg = goMod + "/" + goPkg
}
}
impt, _ := ad.fixModelPathAndPackage(goPkg)
*f.Options.GoPackage = impt
}
}
func (ad *HzAdopter) fixModelPathAndPackage(pkg string) (impt, path string) {
if strings.HasPrefix(pkg, ad.GoModule) {
impt = ImportToPathAndConcat(pkg[len(ad.GoModule):], "")
if !strings.HasPrefix(impt, "/") {
impt = "/" + impt
}
}
if ad.ModelDir != "" && ad.ModelDir != "." {
modelImpt := PathToImport(string(filepath.Separator)+ad.ModelDir, "")
// trim model dir for go package
if strings.HasPrefix(impt, modelImpt) {
impt = impt[len(modelImpt):]
}
impt = PathToImport(ad.ModelDir, "") + impt
}
path = ImportToPath(impt, "")
impt = ad.GoModule + "/" + impt
if IsWindows() {
impt = PathToImport(impt, "")
}
return
}
func (ad *HzAdopter) SetGoModule() {
goPath, err := GetGOPATH()
if err != nil {
return
}
if goPath == "" {
return
}
goSrc := filepath.Join(goPath, "src")
cwd, _ := os.Getwd()
// Generate the project under gopath, use the relative path as the package name
if strings.HasPrefix(cwd, goSrc) {
if goMod, _ := filepath.Rel(goSrc, cwd); err != nil {
return
} else {
ad.GoModule = goMod
}
}
}
// getGoPackage get option go_package
func getGoPackage(f *descriptorpb.FileDescriptorProto, pkgMap map[string]string) string {
if f.Options == nil {
f.Options = new(descriptorpb.FileOptions)
}
if f.Options.GoPackage == nil {
f.Options.GoPackage = new(string)
}
goPkg := f.Options.GetGoPackage()
// if go_package has ";", for example go_package="/a/b/c;d", we will use "/a/b/c" as go_package
if strings.Contains(goPkg, ";") {
pkg := strings.Split(goPkg, ";")
if len(pkg) == 2 {
goPkg = pkg[0]
}
}
if goPkg == "" {
goPkg = f.GetPackage()
}
if opt, ok := pkgMap[f.GetName()]; ok {
return opt
}
return goPkg
}