Skip to content

Commit a2e4186

Browse files
committed
feat: support wasm & make it can used as a lib
1 parent b61166e commit a2e4186

File tree

9 files changed

+86
-37
lines changed

9 files changed

+86
-37
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.PHONY: build
2+
3+
default: build
4+
5+
build:
6+
go build -ldflags '-s -w' -o exe/protobuf-thrift exe/main.go

dev

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
cd ./exe
4+
5+
gowatch -args='-t=proto2thrift' -o protobuf-thrift

dev.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

main.go renamed to exe/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package main
22

33
import (
4+
pbThrift "github.com/YYCoder/protobuf-thrift"
45
"github.com/YYCoder/protobuf-thrift/utils/logger"
56
)
67

78
func main() {
8-
runner, err := NewRunner()
9+
runner, err := pbThrift.NewRunner()
910
if err != nil {
1011
logger.Fatal(err)
1112
}

generator.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package pbthrift
22

33
import (
44
"io/fs"
@@ -19,12 +19,14 @@ type FileInfo struct {
1919
type SubGenerator interface {
2020
Parse() (newFiles []FileInfo, err error) // return relative file path to parsed file
2121
Sink() (err error)
22+
Pipe() (res []byte, err error)
2223
FilePath() (res string)
2324
}
2425

2526
// Main generator for all idl files, responsible for initialize all SubGenerator for each file
2627
type Generator interface {
2728
Generate() (err error)
29+
Pipe() (res []byte, err error)
2830
}
2931

3032
func NewGenerator(conf *RunnerConfig) (res Generator, err error) {
@@ -35,7 +37,7 @@ func NewGenerator(conf *RunnerConfig) (res Generator, err error) {
3537
}
3638

3739
if conf.Task == TASK_CONTENT_PROTO2THRIFT || conf.Task == TASK_CONTENT_THRIFT2PROTO {
38-
gen.initSubGeneratorForRawContent()
40+
err = gen.initSubGeneratorForRawContent()
3941
} else {
4042
_, filename := filepath.Split(conf.InputPath)
4143

@@ -90,6 +92,22 @@ func (g *generator) Generate() (err error) {
9092
return
9193
}
9294

95+
// Pipe the transformed result to return value, since the scenario for Pipe is using protobuf-thrift as a lib, currently not support recursive transform.
96+
func (g *generator) Pipe() (res []byte, err error) {
97+
for _, sub := range g.subGeneratorMap {
98+
if _, err = sub.Parse(); err != nil {
99+
logger.Fatalf("Error occurred when parsing file %v", sub.FilePath())
100+
return
101+
}
102+
if res, err = sub.Pipe(); err != nil {
103+
logger.Fatalf("Error occurred when generating file %v", sub.FilePath(), err)
104+
return
105+
}
106+
break
107+
}
108+
return
109+
}
110+
93111
func (g *generator) absPathIsIdl(absPath string) (res bool, err error) {
94112
suffix := ""
95113
if g.conf.Task == TASK_CONTENT_PROTO2THRIFT || g.conf.Task == TASK_FILE_PROTO2THRIFT {
@@ -200,7 +218,7 @@ func (g *generator) initSubGenerator(fileInfos []FileInfo) (err error) {
200218

201219
if g.conf.Task == TASK_FILE_PROTO2THRIFT {
202220
var generator SubGenerator
203-
conf := &thriftGeneratorConfig{
221+
conf := &ThriftGeneratorConfig{
204222
taskType: g.conf.Task,
205223
filePath: path,
206224
fileName: filename,
@@ -219,7 +237,7 @@ func (g *generator) initSubGenerator(fileInfos []FileInfo) (err error) {
219237
g.subGeneratorMap[path] = generator
220238
} else if g.conf.Task == TASK_FILE_THRIFT2PROTO {
221239
var generator SubGenerator
222-
conf := &protoGeneratorConfig{
240+
conf := &ProtoGeneratorConfig{
223241
taskType: g.conf.Task,
224242
filePath: path,
225243
fileName: filename,
@@ -250,7 +268,7 @@ func (g *generator) initSubGeneratorForRawContent() (err error) {
250268
})
251269
if g.conf.Task == TASK_CONTENT_PROTO2THRIFT {
252270
var generator SubGenerator
253-
conf := &thriftGeneratorConfig{
271+
conf := &ThriftGeneratorConfig{
254272
taskType: g.conf.Task,
255273
rawContent: g.conf.RawContent,
256274
filePath: path,
@@ -262,12 +280,13 @@ func (g *generator) initSubGeneratorForRawContent() (err error) {
262280
}
263281
generator, err = NewThriftGenerator(conf)
264282
if err != nil {
265-
return
283+
logger.Errorf("NewThriftGenerator failed, %s", err)
284+
return err
266285
}
267286
g.subGeneratorMap[path] = generator
268287
} else if g.conf.Task == TASK_CONTENT_THRIFT2PROTO {
269288
var generator SubGenerator
270-
conf := &protoGeneratorConfig{
289+
conf := &ProtoGeneratorConfig{
271290
taskType: g.conf.Task,
272291
rawContent: g.conf.RawContent,
273292
filePath: path,
@@ -279,7 +298,8 @@ func (g *generator) initSubGeneratorForRawContent() (err error) {
279298
}
280299
generator, err = NewProtoGenerator(conf)
281300
if err != nil {
282-
return
301+
logger.Errorf("NewProtoGenerator failed, %s", err)
302+
return err
283303
}
284304
g.subGeneratorMap[path] = generator
285305
}

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
github.com/YYCoder/thrifter v0.0.4 h1:fgJ1Ti3E2lg0YPHNl6vaTpX2VymIsXdzZ9qipDYqDB8=
2-
github.com/YYCoder/thrifter v0.0.4/go.mod h1:S7jjpbFkt9VYlTUDkhsPhrFi++/wH+S1sJ2NLhDIdZQ=
3-
github.com/YYCoder/thrifter v0.0.5 h1:VN7KQyddfVqaIu8+cztmiuYZfzuvsxhx5u2S8qnpDUM=
4-
github.com/YYCoder/thrifter v0.0.5/go.mod h1:S7jjpbFkt9VYlTUDkhsPhrFi++/wH+S1sJ2NLhDIdZQ=
51
github.com/YYCoder/thrifter v0.0.6 h1:tEyqNXLfBssRllTExHEEmWI5pFSa9OpyP4V7j1yZE/0=
62
github.com/YYCoder/thrifter v0.0.6/go.mod h1:S7jjpbFkt9VYlTUDkhsPhrFi++/wH+S1sJ2NLhDIdZQ=
73
github.com/emicklei/proto v1.9.0 h1:l0QiNT6Qs7Yj0Mb4X6dnWBQer4ebei2BFcgQLbGqUDc=

proto-2-thrift-gen.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package pbthrift
22

33
import (
44
"bufio"
@@ -18,15 +18,15 @@ import (
1818
)
1919

2020
type thriftGenerator struct {
21-
conf *thriftGeneratorConfig
21+
conf *ThriftGeneratorConfig
2222
def *proto.Proto
2323
file *os.File
2424
thriftContent bytes.Buffer
2525
newFiles []FileInfo
2626
syntax int
2727
}
2828

29-
type thriftGeneratorConfig struct {
29+
type ThriftGeneratorConfig struct {
3030
taskType int
3131
filePath string // absolute path for current file
3232
fileName string // relative filename including path for file to be generated
@@ -42,9 +42,10 @@ type thriftGeneratorConfig struct {
4242
syntax int // 2 or 3
4343
}
4444

45-
func NewThriftGenerator(conf *thriftGeneratorConfig) (res SubGenerator, err error) {
45+
func NewThriftGenerator(conf *ThriftGeneratorConfig) (res SubGenerator, err error) {
4646
var parser *proto.Parser
4747
var file *os.File
48+
var content string
4849
var syntax int
4950
if conf.taskType == TASK_FILE_PROTO2THRIFT {
5051
file, err = os.Open(conf.filePath)
@@ -60,23 +61,27 @@ func NewThriftGenerator(conf *thriftGeneratorConfig) (res SubGenerator, err erro
6061
return nil, err
6162
}
6263
defer file1.Close()
63-
content, err := io.ReadAll(file1)
64+
contentBytes, err := io.ReadAll(file1)
6465
if err != nil {
6566
return nil, err
6667
}
67-
if strings.Contains(string(content), "syntax = \"proto3\"") {
68-
syntax = 3
69-
} else {
70-
syntax = 2
71-
}
68+
content = string(contentBytes)
7269
} else if conf.taskType == TASK_CONTENT_PROTO2THRIFT {
73-
if strings.Contains(conf.rawContent, "syntax = \"proto3\"") {
70+
content = conf.rawContent
71+
rd := strings.NewReader(conf.rawContent)
72+
parser = proto.NewParser(rd)
73+
}
74+
75+
if strings.Contains(content, "syntax =") {
76+
if strings.Contains(content, "syntax = \"proto3\"") {
7477
syntax = 3
7578
} else {
7679
syntax = 2
7780
}
78-
rd := strings.NewReader(conf.rawContent)
79-
parser = proto.NewParser(rd)
81+
} else if conf.syntax != 0 {
82+
syntax = conf.syntax
83+
} else {
84+
syntax = 3
8085
}
8186

8287
definition, err := parser.Parse()
@@ -541,3 +546,7 @@ func (g *thriftGenerator) writeIndent() {
541546
g.thriftContent.WriteString(" ")
542547
}
543548
}
549+
550+
func (g *thriftGenerator) Pipe() (res []byte, err error) {
551+
return g.thriftContent.Bytes(), nil
552+
}

runner.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package pbthrift
22

33
import (
44
"flag"
@@ -18,10 +18,11 @@ const (
1818
)
1919

2020
type Runner struct {
21-
config *RunnerConfig
21+
Config *RunnerConfig
2222
}
2323

2424
type RunnerConfig struct {
25+
Pipe bool // return the result from Generator instead of printing to os.Stdout or filesystem
2526
RawContent string
2627
InputPath string // absolute path for input idl file
2728
OutputDir string // absolute path for output dir
@@ -111,21 +112,31 @@ func NewRunner() (res *Runner, err error) {
111112
Recursive: recursive,
112113
}
113114
res = &Runner{
114-
config: config,
115+
Config: config,
115116
}
116117
return
117118
}
118119

119120
func (r *Runner) Run() (err error) {
120121
var generator Generator
121-
generator, err = NewGenerator(r.config)
122+
generator, err = NewGenerator(r.Config)
122123
if err != nil {
123124
return
124125
}
125126
err = generator.Generate()
126127
return
127128
}
128129

130+
func (r *Runner) Pipe() (res []byte, err error) {
131+
var generator Generator
132+
generator, err = NewGenerator(r.Config)
133+
if err != nil {
134+
return
135+
}
136+
res, err = generator.Pipe()
137+
return
138+
}
139+
129140
func ValidateTaskType(taskType string) {
130141
if taskType != "proto2thrift" && taskType != "thrift2proto" {
131142
logger.Fatal("You must specify which task you want to run, proto2thrift or thrift2proto.")

thrift-2-proto-gen.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package pbthrift
22

33
import (
44
"bufio"
@@ -15,15 +15,15 @@ import (
1515
)
1616

1717
type protoGenerator struct {
18-
conf *protoGeneratorConfig
18+
conf *ProtoGeneratorConfig
1919
def *thrifter.Thrift
2020
file *os.File
2121
protoContent bytes.Buffer
2222
currentToken *thrifter.Token
2323
packageDeclare string // used to detect whether has duplicate package
2424
}
2525

26-
type protoGeneratorConfig struct {
26+
type ProtoGeneratorConfig struct {
2727
taskType int
2828
filePath string // absolute path for current file
2929
fileName string // output file name, including extension
@@ -39,7 +39,7 @@ type protoGeneratorConfig struct {
3939
syntax int // 2 or 3
4040
}
4141

42-
func NewProtoGenerator(conf *protoGeneratorConfig) (res SubGenerator, err error) {
42+
func NewProtoGenerator(conf *ProtoGeneratorConfig) (res SubGenerator, err error) {
4343
var parser *thrifter.Parser
4444
var file *os.File
4545
var definition *thrifter.Thrift
@@ -488,3 +488,7 @@ func (g *protoGenerator) writeIndent() {
488488
}
489489
return
490490
}
491+
492+
func (g *protoGenerator) Pipe() (res []byte, err error) {
493+
return g.protoContent.Bytes(), nil
494+
}

0 commit comments

Comments
 (0)