зеркало из https://github.com/arthurnn/twirp-ruby.git
protoc-gen-ruby: add test data
This commit is contained in:
Родитель
e2cd365251
Коммит
0049f93d3e
|
@ -33,21 +33,25 @@ import (
|
|||
|
||||
func main() {
|
||||
genReq := readGenRequest(os.Stdin)
|
||||
g := &generator{
|
||||
version: Version,
|
||||
genReq: genReq,
|
||||
fileToGoPackageName: make(map[*descriptor.FileDescriptorProto]string),
|
||||
reg: typemap.New(genReq.ProtoFile),
|
||||
}
|
||||
genResp := g.Generate()
|
||||
genResp := newGenerator(genReq).Generate()
|
||||
writeGenResponse(os.Stdout, genResp)
|
||||
}
|
||||
|
||||
func newGenerator(genReq *plugin.CodeGeneratorRequest) *generator {
|
||||
return &generator{
|
||||
version: Version,
|
||||
genReq: genReq,
|
||||
fileToGoPackageName: make(map[*descriptor.FileDescriptorProto]string),
|
||||
reg: typemap.New(genReq.ProtoFile),
|
||||
}
|
||||
}
|
||||
|
||||
type generator struct {
|
||||
version string
|
||||
genReq *plugin.CodeGeneratorRequest
|
||||
|
||||
reg *typemap.Registry
|
||||
genFiles []*descriptor.FileDescriptorProto
|
||||
fileToGoPackageName map[*descriptor.FileDescriptorProto]string
|
||||
}
|
||||
|
||||
|
@ -62,17 +66,9 @@ func fileDescSliceContains(slice []*descriptor.FileDescriptorProto, f *descripto
|
|||
|
||||
func (g *generator) Generate() *plugin.CodeGeneratorResponse {
|
||||
resp := new(plugin.CodeGeneratorResponse)
|
||||
genFiles := g.protoFilesToGenerate()
|
||||
g.findProtoFilesToGenerate()
|
||||
|
||||
for _, f := range g.genReq.ProtoFile {
|
||||
if fileDescSliceContains(genFiles, f) {
|
||||
g.fileToGoPackageName[f] = ""
|
||||
} else {
|
||||
g.fileToGoPackageName[f] = f.GetPackage()
|
||||
}
|
||||
}
|
||||
|
||||
for _, f := range genFiles {
|
||||
for _, f := range g.genFiles {
|
||||
twirpFileName := noExtension(filePath(f)) + "_twirp.rb" // e.g. "hello_world/service_twirp.rb"
|
||||
pbFileRelativePath := noExtension(onlyBase(filePath(f))) + "_pb.rb" // e.g. "service_pb.rb"
|
||||
|
||||
|
@ -98,7 +94,7 @@ func (g *generator) generateRubyCode(file *descriptor.FileDescriptorProto, pbFil
|
|||
pkgName := file.GetPackage()
|
||||
|
||||
var modules []string
|
||||
if file.Options.RubyPackage != nil {
|
||||
if file.Options != nil && file.Options.RubyPackage != nil {
|
||||
modules = strings.Split(*file.Options.RubyPackage, "::")
|
||||
} else {
|
||||
modules = splitRubyConstants(pkgName)
|
||||
|
@ -144,17 +140,23 @@ func (g *generator) generateRubyCode(file *descriptor.FileDescriptorProto, pbFil
|
|||
}
|
||||
|
||||
// protoFilesToGenerate selects descriptor proto files that were explicitly listed on the command-line.
|
||||
func (g *generator) protoFilesToGenerate() []*descriptor.FileDescriptorProto {
|
||||
files := []*descriptor.FileDescriptorProto{}
|
||||
func (g *generator) findProtoFilesToGenerate() {
|
||||
for _, name := range g.genReq.FileToGenerate { // explicitly listed on the command-line
|
||||
for _, f := range g.genReq.ProtoFile { // all files and everything they import
|
||||
if f.GetName() == name { // match
|
||||
files = append(files, f)
|
||||
g.genFiles = append(g.genFiles, f)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
return files
|
||||
|
||||
for _, f := range g.genReq.ProtoFile {
|
||||
if fileDescSliceContains(g.genFiles, f) {
|
||||
g.fileToGoPackageName[f] = ""
|
||||
} else {
|
||||
g.fileToGoPackageName[f] = f.GetPackage()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// indentation represents the level of Ruby indentation for a block of code. It
|
||||
|
@ -232,7 +234,7 @@ func (g *generator) toRubyType(protoType string) string {
|
|||
|
||||
var prefix string
|
||||
if pkg := g.fileToGoPackageName[def.File]; pkg != "" {
|
||||
prefix = pkg + "::"
|
||||
prefix = strings.Join(splitRubyConstants(pkg), "::") + "::"
|
||||
}
|
||||
|
||||
var name string
|
||||
|
|
|
@ -2,10 +2,36 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func loadTestPb(t *testing.T) []*descriptor.FileDescriptorProto {
|
||||
f, err := ioutil.ReadFile(filepath.Join("testdata", "fileset.pb"))
|
||||
require.NoError(t, err, "unable to read testdata protobuf file")
|
||||
|
||||
set := new(descriptor.FileDescriptorSet)
|
||||
err = proto.Unmarshal(f, set)
|
||||
require.NoError(t, err, "unable to unmarshal testdata protobuf file")
|
||||
|
||||
return set.File
|
||||
}
|
||||
|
||||
func testGenerator(t *testing.T) *generator {
|
||||
genReq := &plugin_go.CodeGeneratorRequest{
|
||||
FileToGenerate: []string{"rubytypes.proto"},
|
||||
ProtoFile: loadTestPb(t),
|
||||
}
|
||||
return newGenerator(genReq)
|
||||
}
|
||||
|
||||
func TestPrint(t *testing.T) {
|
||||
b := new(bytes.Buffer)
|
||||
print(b, "Hello World")
|
||||
|
@ -38,21 +64,18 @@ func TestFilePathOnlyBaseNoExtension(t *testing.T) {
|
|||
func TestToRubyType(t *testing.T) {
|
||||
tests := []struct {
|
||||
protoType string
|
||||
modules []string
|
||||
expected string
|
||||
}{
|
||||
{"", []string{}, ""},
|
||||
{"", []string{"Foo", "Bar"}, ""},
|
||||
{".foo.my_message", []string{}, "Foo::MyMessage"},
|
||||
{".foo.my_message", []string{"Foo"}, "MyMessage"},
|
||||
{"m.v.p99.hello_world", []string{}, "M::V::P99::HelloWorld"},
|
||||
{"m.v.p99.hello_world", []string{"M", "V"}, "P99::HelloWorld"},
|
||||
{"m.v.p99.hello_world", []string{"M", "V", "P99"}, "HelloWorld"},
|
||||
{"m.v.p99.hello_world", []string{"P99"}, "M::V::P99::HelloWorld"},
|
||||
{"google.protobuf.Empty", []string{"Foo"}, "Google::Protobuf::Empty"},
|
||||
{".twirp.rubytypes.foo.my_message", "Foo::MyMessage"},
|
||||
{".twirp.rubytypes.m.v.p99.hello_world", "M::V::P99::HelloWorld"},
|
||||
{".google.protobuf.Empty", "Google::Protobuf::Empty"},
|
||||
}
|
||||
|
||||
g := testGenerator(t)
|
||||
g.findProtoFilesToGenerate()
|
||||
|
||||
for _, tt := range tests {
|
||||
actual := toRubyType(tt.protoType, tt.modules)
|
||||
actual := g.toRubyType(tt.protoType)
|
||||
if !reflect.DeepEqual(actual, tt.expected) {
|
||||
t.Errorf("expected %v; actual %v", tt.expected, actual)
|
||||
}
|
||||
|
|
Двоичный файл не отображается.
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2018 Twitch Interactive, Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
|
||||
// use this file except in compliance with the License. A copy of the License is
|
||||
// located at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// or in the "license" file accompanying this file. This file 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 testdata
|
||||
|
||||
//go:generate protoc --descriptor_set_out=fileset.pb --include_imports --include_source_info ./rubytypes.proto
|
|
@ -0,0 +1,18 @@
|
|||
syntax = "proto3";
|
||||
|
||||
package twirp.rubytypes;
|
||||
|
||||
import "google/protobuf/empty.proto";
|
||||
|
||||
message foo {
|
||||
message my_message{}
|
||||
google.protobuf.Empty empty = 1;
|
||||
}
|
||||
|
||||
message m {
|
||||
message v {
|
||||
message p99 {
|
||||
message hello_world {}
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче