Skip to content

Commit

Permalink
support thrift 0.9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhang committed Jun 2, 2019
1 parent b9420cf commit f738d70
Show file tree
Hide file tree
Showing 30 changed files with 2,709 additions and 2,771 deletions.
12 changes: 6 additions & 6 deletions .travis/install-thrift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ BUILD="$HOME/.thrift-build"
mkdir -p "$BUILD"
cd "$BUILD"

rm -rf thrift-0.11.0
rm -rf thrift-0.9.3
ls

if [ ! -d thrift-0.11.0 ]; then
wget http://archive.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz
tar -xzf thrift-0.11.0.tar.gz
cd thrift-0.11.0
if [ ! -d thrift-0.9.3 ]; then
wget http://archive.apache.org/dist/thrift/0.9.3/thrift-0.9.3.tar.gz
tar -xzf thrift-0.9.3.tar.gz
cd thrift-0.9.3
./configure --enable-libs=no --enable-tests=no --enable-tutorial=no --without-haskell --without-java --without-python --without-ruby --without-perl --without-php --without-erlang
else
cd thrift-0.11.0
cd thrift-0.9.3
fi

sudo make -j2
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Turbo  [![Build Status](https://travis-ci.org/vaporz/turbo.svg?branch=master)](https://travis-ci.org/vaporz/turbo) [![Coverage Status](https://coveralls.io/repos/github/vaporz/turbo/badge.svg?branch=master)](https://coveralls.io/github/vaporz/turbo?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/vaporz/turbo)](https://goreportcard.com/report/github.com/vaporz/turbo) [![codebeat badge](https://codebeat.co/badges/7a166e48-dae1-454c-b925-4fbcd3f1f461)](https://codebeat.co/projects/github-com-vaporz-turbo-master)

本分支使用 Thrift v0.9.3版本。

最新版本 | Latest Release: v0.3.0-thrift0.9
最新版本 | Latest Release: v0.3.0-thrift0.9.3

文档地址 | Documentation: https://vaporz.github.io

## Requirements
Golang version: >= 1.9.x
Thrift version: 0.9.0
Golang version: >= 1.9.1
Thrift version: 0.9.3

-------------------------

Expand Down
13 changes: 6 additions & 7 deletions creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *Creator) createThrift(serviceName string) {
struct {
ServiceName string
}{serviceName},
`namespace go gen
`namespace go services
struct SayHelloResponse {
1: string message,
Expand Down Expand Up @@ -315,15 +315,14 @@ func (c *Creator) generateThriftServiceImpl() {
`package impl
import (
"context"
"{{.PkgPath}}/gen/thrift/gen-go/gen"
"{{.PkgPath}}/gen/thrift/gen-go/services"
"git.apache.org/thrift.git/lib/go/thrift"
)
// TProcessor returns TProcessor
func TProcessor() map[string]thrift.TProcessor {
return map[string]thrift.TProcessor{
"{{.ServiceName}}": gen.New{{.ServiceName}}Processor({{.ServiceName}}{}),
"{{.ServiceName}}": services.New{{.ServiceName}}Processor({{.ServiceName}}{}),
}
}
Expand All @@ -332,8 +331,8 @@ type {{.ServiceName}} struct {
}
// SayHello is an example entry point
func (s {{.ServiceName}}) SayHello(ctx context.Context, yourName string) (r *gen.SayHelloResponse, err error) {
return &gen.SayHelloResponse{Message: "[thrift server]Hello, " + yourName}, nil
func (s {{.ServiceName}}) SayHello(yourName string) (r *services.SayHelloResponse, err error) {
return &services.SayHelloResponse{Message: "[thrift server]Hello, " + yourName}, nil
}
`,
)
Expand Down Expand Up @@ -434,7 +433,7 @@ func (c *Creator) generateThriftHTTPComponent() {
`package component
import (
t "{{.PkgPath}}/gen/thrift/gen-go/gen"
t "{{.PkgPath}}/gen/thrift/gen-go/services"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/vaporz/turbo"
)
Expand Down
54 changes: 35 additions & 19 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ var buildThriftParameters = `package main
import (
"flag"
"fmt"
g "{{.PkgPath}}/gen/thrift/gen-go/gen"
g "{{.PkgPath}}/gen/thrift/gen-go/services"
"io"
"os"
"reflect"
Expand Down Expand Up @@ -212,11 +212,8 @@ func buildFields() {
numIn := method.Type.NumIn()
for j := 0; j < numIn; j++ {
argType := method.Type.In(j)
argStr := argType.String()
if argType.Kind() == reflect.Ptr && argType.Elem().Kind() == reflect.Struct {
arr := strings.Split(argStr, ".")
name := arr[len(arr)-1:][0]
items = findItem(items, name, argType)
items = findItem(items, argType.Elem().PkgPath()+"."+argType.Elem().Name(), argType)
}
}
}
Expand All @@ -237,11 +234,9 @@ func findItem(items []string, name string, structType reflect.Type) []string {
for i := 0; i < numField; i++ {
fieldType := structType.Elem().Field(i)
if fieldType.Type.Kind() == reflect.Ptr && fieldType.Type.Elem().Kind() == reflect.Struct {
arr := strings.Split(fieldType.Type.String(), ".")
typeName := arr[len(arr)-1:][0]
argName := fieldType.Name
item += fmt.Sprintf("%s %s,", typeName, argName)
items = findItem(items, typeName, fieldType.Type)
item += fmt.Sprintf("%s %s,", fieldType.Type.Elem().PkgPath()+"."+"."+fieldType.Type.Elem().Name(), argName)
items = findItem(items, fieldType.Type.Elem().PkgPath()+"."+"."+fieldType.Type.Elem().Name(), fieldType.Type)
}
}
item += "]"
Expand Down Expand Up @@ -315,8 +310,22 @@ func (g *Generator) GenerateThriftSwitcher() {
var argCasesStr string
fields := make([]string, 0, len(g.c.fieldMappings))
structNames := make([]string, 0, len(g.c.fieldMappings))
structPrefixes := make([]string, 0, len(g.c.fieldMappings))
importPackages := make(map[string]int)
importPackages[g.PkgPath+"/gen/thrift/gen-go/services"] = 1
for k := range g.c.fieldMappings {
structNames = append(structNames, k)
s := strings.Split(k, ".")
var importPackage string
for i, v := range s {
if i != len(s)-1 {
importPackage += v + "."
}
}
importPackage = importPackage[0 : len(importPackage)-1]
importPackages[importPackage] = 1
packagePath := strings.Split(s[len(s)-2], "/")
structPrefixes = append(structPrefixes, packagePath[len(packagePath)-1])
structNames = append(structNames, s[len(s)-1])
fields = append(fields, g.structFields(k))
}
writeFileWithTemplate(
Expand All @@ -328,17 +337,21 @@ func (g *Generator) GenerateThriftSwitcher() {
ServiceMethodMap map[string][]string
Parameters map[string][]string
NotEmptyParameters map[string][]bool
StructPrefixes []string
StructNames []string
StructFields []string
ImportPackages map[string]int
}{
g.PkgPath,
argCasesStr,
g.c.ThriftServiceNames(),
serviceMethodMap,
parameters,
notEmptyParameters,
structPrefixes,
structNames,
fields},
fields,
importPackages},
thriftSwitcherFunc,
)
}
Expand Down Expand Up @@ -377,26 +390,24 @@ var thriftSwitcherFunc = `// Code generated by turbo. DO NOT EDIT.
package gen
import (
"context"
"errors"
"github.com/vaporz/turbo"
"{{.PkgPath}}/gen/thrift/gen-go/gen"
"github.com/vaporz/turbo"{{range $p, $i := $.ImportPackages}}
"{{$p}}"{{end}}
"net/http"
"reflect"
)
// ThriftSwitcher is a runtime func with which a server starts.
var ThriftSwitcher = func(s turbo.Servable, serviceName, methodName string, resp http.ResponseWriter, req *http.Request) (serviceResponse interface{}, err error) { {{range $Service, $Methods := .ServiceMethodMap}}
if serviceName == "{{$Service}}" {
ctx := context.Background()
switch methodName { {{range $i, $MethodName := $Methods}}
case "{{$MethodName}}":{{if index $.NotEmptyParameters $Service $i }}
params, err := turbo.BuildThriftRequest(s, gen.{{$Service}}{{$MethodName}}Args{}, req, buildStructArg)
params, err := turbo.BuildThriftRequest(s, services.{{$Service}}{{$MethodName}}Args{}, req, buildStructArg)
if err != nil {
return nil, err
}{{end}}
return s.Service("{{$Service}}").(*gen.{{$Service}}Client).{{$MethodName}}(
ctx,{{index $.Parameters $Service $i}}){{end}}
return s.Service("{{$Service}}").(*services.{{$Service}}Client).{{$MethodName}}(
{{index $.Parameters $Service $i}}){{end}}
default:
return nil, errors.New("No such method[" + methodName + "]")
}
Expand All @@ -412,7 +423,7 @@ func buildStructArg(s turbo.Servable, typeName string, req *http.Request) (v ref
switch typeName {
{{range $i, $StructName := .StructNames}}
case "{{$StructName}}":
request := &gen.{{$StructName}}{ {{index $.StructFields $i}} }
request := &{{index $.StructPrefixes $i}}.{{$StructName}}{ {{index $.StructFields $i}} }
turbo.BuildStruct(s, reflect.TypeOf(request).Elem(), reflect.ValueOf(request).Elem(), req)
return reflect.ValueOf(request), nil
{{end}}
Expand All @@ -430,7 +441,12 @@ func (g *Generator) GenerateThriftStub() {
nameLower := strings.ToLower(g.c.ThriftServiceNames()[0]) // todo change a thrift file name
cmd := "thrift " + g.Options + " -r --gen go:package_prefix=" + g.PkgPath + "/gen/thrift/gen-go/ -o" +
" " + g.c.ServiceRootPathAbsolute() + "/" + "gen/thrift " + g.c.ServiceRootPathAbsolute() + "/" + nameLower + ".thrift"

executeCmd("bash", "-c", cmd)

// remove generated folder "*-remote",
// there're compiling errors in this folder, when importing a thrift file with a different package name.
executeCmd("bash", "-c", "find "+g.c.ServiceRootPathAbsolute()+"/"+"gen/thrift -name \"*-remote\" -exec rm -rf {} +")
}

func executeCmd(cmd string, args ...string) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/vaporz/turbo
go 1.12

require (
git.apache.org/thrift.git v0.0.0-20171203172758-327ebb6c2b6d
git.apache.org/thrift.git v0.0.0-20151001171628-53dd39833a08
github.com/bitly/go-simplejson v0.5.0
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/fsnotify/fsnotify v1.4.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
git.apache.org/thrift.git v0.0.0-20171203172758-327ebb6c2b6d h1:RsHq5kCWkxgbqyQPWB8OWNU/CRStjE/vusovT2bGKOw=
git.apache.org/thrift.git v0.0.0-20171203172758-327ebb6c2b6d/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
git.apache.org/thrift.git v0.0.0-20151001171628-53dd39833a08 h1:goxS3HZARSCj217iYEtwVahA/7WJ9MbZR2QJMeMDmxM=
git.apache.org/thrift.git v0.0.0-20151001171628-53dd39833a08/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand Down
19 changes: 10 additions & 9 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/vaporz/turbo"
"github.com/vaporz/turbo/test/testservice/gen"
"github.com/vaporz/turbo/test/testservice/gen/proto"
tgen "github.com/vaporz/turbo/test/testservice/gen/thrift/gen-go/gen"
"github.com/vaporz/turbo/test/testservice/gen/thrift/gen-go/services"
tgen "github.com/vaporz/turbo/test/testservice/gen/thrift/gen-go/shared"
gcomponent "github.com/vaporz/turbo/test/testservice/grpcapi/component"
gimpl "github.com/vaporz/turbo/test/testservice/grpcservice/impl"
tcompoent "github.com/vaporz/turbo/test/testservice/thriftapi/component"
Expand Down Expand Up @@ -291,7 +292,7 @@ message CommonValues {
func overwriteThrift() {
writeFileWithTemplate(
turbo.GOPATH()+"/src/github.com/vaporz/turbo/test/testcreateservice/shared.thrift",
`namespace go gen
`namespace go shared
struct CommonValues {
1: i64 transactionId,
Expand All @@ -305,7 +306,7 @@ struct HelloValues {
)
writeFileWithTemplate(
turbo.GOPATH()+"/src/github.com/vaporz/turbo/test/testcreateservice/testcreateservice.thrift",
`namespace go gen
`namespace go services
include "shared.thrift"
struct SayHelloResponse {
Expand All @@ -324,23 +325,23 @@ service TestCreateService {
`package impl
import (
"context"
"github.com/vaporz/turbo/test/testcreateservice/gen/thrift/gen-go/gen"
"github.com/vaporz/turbo/test/testcreateservice/gen/thrift/gen-go/shared"
"github.com/vaporz/turbo/test/testcreateservice/gen/thrift/gen-go/services"
"git.apache.org/thrift.git/lib/go/thrift"
)
func TProcessor() map[string]thrift.TProcessor {
return map[string]thrift.TProcessor{
"TestCreateService": gen.NewTestCreateServiceProcessor(TestCreateService{}),
"TestCreateService": services.NewTestCreateServiceProcessor(TestCreateService{}),
}
}
type TestCreateService struct {
}
func (s TestCreateService) SayHello(ctx context.Context, values *gen.CommonValues, yourName string, int64Value int64, boolValue bool, float64Value float64, uint64Value int64) (r *gen.SayHelloResponse, err error) {
return &gen.SayHelloResponse{Message: "[thrift server]Hello, " + yourName}, nil
func (s TestCreateService) SayHello(values *shared.CommonValues, yourName string, int64Value int64, boolValue bool, float64Value float64, uint64Value int64) (r *services.SayHelloResponse, err error) {
return &services.SayHelloResponse{Message: "[thrift server]Hello, " + yourName}, nil
}
`,
nil,
Expand Down Expand Up @@ -634,7 +635,7 @@ var postProcessor turbo.Postprocessor = func(resp http.ResponseWriter, req *http
}

var thriftPostProcessor turbo.Postprocessor = func(resp http.ResponseWriter, req *http.Request, serviceResp interface{}, err error) {
r := serviceResp.(*tgen.SayHelloResponse)
r := serviceResp.(*services.SayHelloResponse)
resp.Write([]byte("postprocessor:" + r.Message))
}

Expand Down
12 changes: 6 additions & 6 deletions test/testservice/gen/grpcswitcher.go

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

Loading

0 comments on commit f738d70

Please sign in to comment.