Skip to content

Commit 171640b

Browse files
committed
fix: template refactor
1 parent 6a7af45 commit 171640b

23 files changed

+152
-265
lines changed

Makefile

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
SHA := $(shell git rev-parse --short=10 HEAD)
44

55
MAKEFILE_PATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
6-
VERSION_DATE := $(shell $(MAKEFILE_PATH)/commit_date.sh)
76

87
# Build native Truss by default.
98
default: truss
109

1110
dependencies:
1211
go get -u google.golang.org/genproto
13-
go get -u github.com/gogo/protobuf/protoc-gen-gogo
14-
go get -u github.com/gogo/protobuf/protoc-gen-gogofaster
15-
go get -u github.com/gogo/protobuf/protoc-gen-gofast
1612
go get -u github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
1713
go get -u github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
1814
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
@@ -21,7 +17,7 @@ dependencies:
2117

2218
# Install truss
2319
truss:
24-
go install -ldflags '-X "main.version=$(SHA)" -X "main.date=$(VERSION_DATE)"' github.com/DoNewsCode/truss/cmd/truss
20+
go install -ldflags '-X "main.version=$(SHA)"' github.com/DoNewsCode/truss/cmd/truss
2521

2622
# Run the go tests and the truss integration tests
2723
test: test-go

cmd/truss/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ func readPreviousGeneration(serviceDir string) (map[string]io.Reader, error) {
349349
files := make(map[string]io.Reader)
350350

351351
addFileToFiles := func(path string, info os.FileInfo, err error) error {
352+
if err != nil {
353+
return err
354+
}
352355
if info.IsDir() {
353356
switch info.Name() {
354357
// Only files within the handlers dir are used to

commit_date.sh

-22
This file was deleted.

deftree/gogo/helper.go

+9-30
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ func IsStdBytes(field *google_protobuf.FieldDescriptorProto) bool {
8484
}
8585

8686
func IsStdType(field *google_protobuf.FieldDescriptorProto) bool {
87-
return (IsStdTime(field) || IsStdDuration(field) ||
87+
return IsStdTime(field) || IsStdDuration(field) ||
8888
IsStdDouble(field) || IsStdFloat(field) ||
8989
IsStdInt64(field) || IsStdUInt64(field) ||
9090
IsStdInt32(field) || IsStdUInt32(field) ||
9191
IsStdBool(field) ||
92-
IsStdString(field) || IsStdBytes(field))
92+
IsStdString(field) || IsStdBytes(field)
9393
}
9494

9595
func IsWktPtr(field *google_protobuf.FieldDescriptorProto) bool {
@@ -109,34 +109,22 @@ func NeedsNilCheck(proto3 bool, field *google_protobuf.FieldDescriptorProto) boo
109109

110110
func IsCustomType(field *google_protobuf.FieldDescriptorProto) bool {
111111
typ := GetCustomType(field)
112-
if len(typ) > 0 {
113-
return true
114-
}
115-
return false
112+
return len(typ) > 0
116113
}
117114

118115
func IsCastType(field *google_protobuf.FieldDescriptorProto) bool {
119116
typ := GetCastType(field)
120-
if len(typ) > 0 {
121-
return true
122-
}
123-
return false
117+
return len(typ) > 0
124118
}
125119

126120
func IsCastKey(field *google_protobuf.FieldDescriptorProto) bool {
127121
typ := GetCastKey(field)
128-
if len(typ) > 0 {
129-
return true
130-
}
131-
return false
122+
return len(typ) > 0
132123
}
133124

134125
func IsCastValue(field *google_protobuf.FieldDescriptorProto) bool {
135126
typ := GetCastValue(field)
136-
if len(typ) > 0 {
137-
return true
138-
}
139-
return false
127+
return len(typ) > 0
140128
}
141129

142130
func HasEnumDecl(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool {
@@ -201,26 +189,17 @@ func GetCastValue(field *google_protobuf.FieldDescriptorProto) string {
201189

202190
func IsCustomName(field *google_protobuf.FieldDescriptorProto) bool {
203191
name := GetCustomName(field)
204-
if len(name) > 0 {
205-
return true
206-
}
207-
return false
192+
return len(name) > 0
208193
}
209194

210195
func IsEnumCustomName(field *google_protobuf.EnumDescriptorProto) bool {
211196
name := GetEnumCustomName(field)
212-
if len(name) > 0 {
213-
return true
214-
}
215-
return false
197+
return len(name) > 0
216198
}
217199

218200
func IsEnumValueCustomName(field *google_protobuf.EnumValueDescriptorProto) bool {
219201
name := GetEnumValueCustomName(field)
220-
if len(name) > 0 {
221-
return true
222-
}
223-
return false
202+
return len(name) > 0
224203
}
225204

226205
func GetCustomName(field *google_protobuf.FieldDescriptorProto) string {

gengokit/handlers/handler_test.go

+22-36
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func init() {
3131
log.SetLevel(log.DebugLevel)
3232
}
3333

34+
func fatalError(t *testing.T, err error) {
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
}
39+
3440
func TestServerMethsTempl(t *testing.T) {
3541
const def = `
3642
syntax = "proto3";
@@ -63,19 +69,16 @@ func TestServerMethsTempl(t *testing.T) {
6369
}
6470
`
6571
sd, err := svcdef.NewFromString(def, gopath)
66-
if err != nil {
67-
t.Fatal(err)
68-
}
72+
fatalError(t, err)
6973

7074
var he handlerData
7175
he.Methods = sd.Service.Methods
7276
he.ServiceName = sd.Service.Name
7377

7478
gen, err := applyServerMethsTempl(he)
75-
if err != nil {
76-
t.Fatal(err)
77-
}
79+
fatalError(t, err)
7880
genBytes, err := ioutil.ReadAll(gen)
81+
fatalError(t, err)
7982
const expected = `
8083
func (s Service) ProtoMethod(ctx context.Context, in *pb.RequestMessage) (*pb.ResponseMessage, error){
8184
var resp pb.ResponseMessage
@@ -124,13 +127,14 @@ func TestApplyServerTempl(t *testing.T) {
124127
PBPackage: "github.com/DoNewsCode/truss/gengokit/general-service",
125128
}
126129
sd, err := svcdef.NewFromString(def, gopath)
127-
if err != nil {
128-
t.Fatal(err)
129-
}
130+
fatalError(t, err)
130131
te, err := gengokit.NewData(sd, conf)
132+
fatalError(t, err)
131133

132134
gen, err := applyServerTempl(te)
135+
fatalError(t, err)
133136
genBytes, err := ioutil.ReadAll(gen)
137+
fatalError(t, err)
134138
expected := `
135139
package proto
136140
@@ -212,9 +216,7 @@ func TestIsValidFunc(t *testing.T) {
212216
}
213217
`
214218
sd, err := svcdef.NewFromString(def, gopath)
215-
if err != nil {
216-
t.Fatal(err)
217-
}
219+
fatalError(t, err)
218220

219221
m := newMethodMap(sd.Service.Methods)
220222
const validUnexported = `package p;
@@ -297,9 +299,7 @@ func TestPruneDecls(t *testing.T) {
297299
}
298300
`
299301
sd, err := svcdef.NewFromString(def, gopath)
300-
if err != nil {
301-
t.Fatal(err)
302-
}
302+
fatalError(t, err)
303303

304304
m := newMethodMap(sd.Service.Methods)
305305

@@ -429,9 +429,7 @@ func TestUpdateMethods(t *testing.T) {
429429
`
430430

431431
sd, err := svcdef.NewFromString(def, gopath)
432-
if err != nil {
433-
t.Fatal(err)
434-
}
432+
fatalError(t, err)
435433

436434
svc := sd.Service
437435
allMethods := svc.Methods
@@ -442,21 +440,15 @@ func TestUpdateMethods(t *testing.T) {
442440
}
443441

444442
te, err := gengokit.NewData(sd, conf)
445-
if err != nil {
446-
t.Fatal(err)
447-
}
443+
fatalError(t, err)
448444

449445
svc.Methods = []*svcdef.ServiceMethod{allMethods[0]}
450446

451447
firstCode, err := renderService(svc, "", te)
452-
if err != nil {
453-
t.Fatal(err)
454-
}
448+
fatalError(t, err)
455449

456450
secondCode, err := renderService(svc, firstCode, te)
457-
if err != nil {
458-
t.Fatal(err)
459-
}
451+
fatalError(t, err)
460452

461453
if len(firstCode) != len(secondCode) {
462454
t.Fatal("Generated service differs after regenerated with same definition\n" +
@@ -466,9 +458,7 @@ func TestUpdateMethods(t *testing.T) {
466458
svc.Methods = append(svc.Methods, allMethods[1])
467459

468460
thirdCode, err := renderService(svc, secondCode, te)
469-
if err != nil {
470-
t.Fatal(err)
471-
}
461+
fatalError(t, err)
472462

473463
if len(thirdCode) <= len(secondCode) {
474464
t.Fatal("Generated service not longer after regenerated with additional service method\n" +
@@ -479,9 +469,7 @@ func TestUpdateMethods(t *testing.T) {
479469
svc.Methods = svc.Methods[1:]
480470

481471
forthCode, err := renderService(svc, thirdCode, te)
482-
if err != nil {
483-
t.Fatal(err)
484-
}
472+
fatalError(t, err)
485473

486474
if len(forthCode) >= len(thirdCode) {
487475
t.Fatal("Generated service not shorter after regenerated with fewer service method\n" +
@@ -491,9 +479,7 @@ func TestUpdateMethods(t *testing.T) {
491479
svc.Methods = allMethods
492480

493481
fifthCode, err := renderService(svc, forthCode, te)
494-
if err != nil {
495-
t.Fatal(err)
496-
}
482+
fatalError(t, err)
497483

498484
if len(fifthCode) <= len(forthCode) {
499485
t.Fatal("Generated service not longer after regenerated with additional service method\n" +

gengokit/handlers/handlers.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (m methodMap) pruneDecls(decls []ast.Decl, svcName string) []ast.Decl {
161161
newDecls = append(newDecls, x)
162162
continue
163163
}
164-
if ok := isValidFunc(x, m, svcName); ok == true {
164+
if ok := isValidFunc(x, m, svcName); ok {
165165
updateParams(x, m[name])
166166
updateResults(x, m[name])
167167
newDecls = append(newDecls, x)
@@ -265,11 +265,6 @@ func exprString(e ast.Expr) string {
265265
}
266266
// *foo.Foo or foo.Foo
267267
if sel, _ := e.(*ast.SelectorExpr); sel != nil {
268-
// *foo.Foo -> foo.Foo
269-
if ptr, _ := e.(*ast.StarExpr); ptr != nil {
270-
prefix = "*"
271-
e = ptr.X
272-
}
273268
// foo.Foo
274269
if x, _ := sel.X.(*ast.Ident); x != nil {
275270
return prefix + x.Name + "." + sel.Sel.Name

gengokit/handlers/templates/hook.go

-22
This file was deleted.

gengokit/httptransport/get_source.go

+9-20
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ func FuncSourceCode(val interface{}) (string, error) {
1919
ptr := reflect.ValueOf(val).Pointer()
2020
fpath, _ := runtime.FuncForPC(ptr).FileLine(ptr)
2121

22-
funcName := runtime.FuncForPC(ptr).Name()
23-
parts := strings.Split(funcName, ".")
24-
funcName = parts[len(parts)-1]
22+
parts := strings.Split(runtime.FuncForPC(ptr).Name(), ".")
23+
funcName := parts[len(parts)-1]
2524

2625
// Parse the go file into the ast
2726
fset := token.NewFileSet()
@@ -32,13 +31,9 @@ func FuncSourceCode(val interface{}) (string, error) {
3231

3332
// Search ast for function declaration with name of function passed
3433
var fAst *ast.FuncDecl
35-
for _, decs := range fileAst.Decls {
36-
switch decs.(type) {
37-
case *ast.FuncDecl:
38-
f := decs.(*ast.FuncDecl)
39-
if f.Name.String() == funcName {
40-
fAst = f
41-
}
34+
for _, decl := range fileAst.Decls {
35+
if decl, ok := decl.(*ast.FuncDecl); ok && funcName == decl.Name.String() {
36+
fAst = decl
4237
}
4338
}
4439
code := bytes.NewBuffer(nil)
@@ -59,10 +54,6 @@ func AllFuncSourceCode(val interface{}) (string, error) {
5954
ptr := reflect.ValueOf(val).Pointer()
6055
fpath, _ := runtime.FuncForPC(ptr).FileLine(ptr)
6156

62-
funcName := runtime.FuncForPC(ptr).Name()
63-
parts := strings.Split(funcName, ".")
64-
funcName = parts[len(parts)-1]
65-
6657
// Parse the go file into the ast
6758
fset := token.NewFileSet()
6859
fileAst, err := parser.ParseFile(fset, fpath, nil, parser.ParseComments)
@@ -71,12 +62,10 @@ func AllFuncSourceCode(val interface{}) (string, error) {
7162
}
7263

7364
// Search ast for all function declarations
74-
fncSlc := []*ast.FuncDecl{}
75-
for _, decs := range fileAst.Decls {
76-
switch decs.(type) {
77-
case *ast.FuncDecl:
78-
f := decs.(*ast.FuncDecl)
79-
fncSlc = append(fncSlc, f)
65+
var fncSlc []*ast.FuncDecl
66+
for _, decl := range fileAst.Decls {
67+
if decl, ok := decl.(*ast.FuncDecl); ok {
68+
fncSlc = append(fncSlc, decl)
8069
}
8170
}
8271

0 commit comments

Comments
 (0)