mirror of
https://github.com/arkadiyt/protodump.git
synced 2026-10-21 09:46:52 +02:00
Various bug fixes; improve testing harness
This commit is contained in:
1 parent
fc85ead5c1
commit
446d6a6c52
3 files changed
+33
-39
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
|
|
||||||
package helloworld;
|
package hello.world;
|
||||||
|
|
||||||
option go_package = "./;helloworld";
|
option go_package = "./;helloworld";
|
||||||
|
|
||||||
@@ -30,14 +30,14 @@ message HelloRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
string name = 1;
|
string name = 1;
|
||||||
helloworld.HelloRequest.Test blah = 2;
|
hello.world.HelloRequest.Test blah = 2;
|
||||||
helloworld.HelloRequest.NestedEnum blahblah = 3;
|
hello.world.HelloRequest.NestedEnum blahblah = 3;
|
||||||
helloworld.HelloReply blaha = 4;
|
hello.world.HelloReply blaha = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message HelloReply {
|
message HelloReply {
|
||||||
helloworld.HelloRequest.Test blah = 2;
|
hello.world.HelloRequest.Test blah = 2;
|
||||||
helloworld.HelloRequest.NestedEnum blahblah = 3;
|
hello.world.HelloRequest.NestedEnum blahblah = 3;
|
||||||
string message = 1;
|
string message = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+17
-10
@@ -17,13 +17,11 @@ import (
|
|||||||
type ProtoDefinition struct {
|
type ProtoDefinition struct {
|
||||||
builder strings.Builder
|
builder strings.Builder
|
||||||
indendation int
|
indendation int
|
||||||
pb descriptorpb.FileDescriptorProto
|
pb *descriptorpb.FileDescriptorProto
|
||||||
descriptor protoreflect.FileDescriptor
|
descriptor protoreflect.FileDescriptor
|
||||||
filename string
|
filename string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add proto2 support
|
|
||||||
|
|
||||||
func (pd *ProtoDefinition) indent() {
|
func (pd *ProtoDefinition) indent() {
|
||||||
pd.indendation += 1
|
pd.indendation += 1
|
||||||
}
|
}
|
||||||
@@ -118,6 +116,8 @@ func (pd *ProtoDefinition) writeField(field protoreflect.FieldDescriptor) {
|
|||||||
pd.write("optional ")
|
pd.write("optional ")
|
||||||
} else if field.Cardinality().String() == "repeated" {
|
} else if field.Cardinality().String() == "repeated" {
|
||||||
pd.write("repeated ")
|
pd.write("repeated ")
|
||||||
|
} else if field.Cardinality().String() == "required" && pd.descriptor.Syntax().String() == "proto2" {
|
||||||
|
pd.write("required ")
|
||||||
}
|
}
|
||||||
pd.writeType(field)
|
pd.writeType(field)
|
||||||
pd.write(" ")
|
pd.write(" ")
|
||||||
@@ -220,7 +220,11 @@ func (pd *ProtoDefinition) writeStringFileOptions(name string, value string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pd *ProtoDefinition) writeBoolFileOptions(name string, value bool) {
|
func (pd *ProtoDefinition) writeBoolFileOptions(name string, value bool) {
|
||||||
pd.writeStringFileOptions(name, strconv.FormatBool(value))
|
pd.write("option ")
|
||||||
|
pd.write(name)
|
||||||
|
pd.write(" = ")
|
||||||
|
pd.write(strconv.FormatBool(value))
|
||||||
|
pd.write(";\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pd *ProtoDefinition) writeFileOptions() {
|
func (pd *ProtoDefinition) writeFileOptions() {
|
||||||
@@ -276,9 +280,12 @@ func (pd *ProtoDefinition) writeFileDescriptor() {
|
|||||||
pd.write(pd.descriptor.Syntax().String())
|
pd.write(pd.descriptor.Syntax().String())
|
||||||
pd.write("\";\n\n")
|
pd.write("\";\n\n")
|
||||||
|
|
||||||
pd.write("package ")
|
packageName := pd.descriptor.FullName()
|
||||||
pd.write(string(pd.descriptor.Package().Name()))
|
if packageName != "" {
|
||||||
pd.write(";\n\n")
|
pd.write("package ")
|
||||||
|
pd.write(string(packageName))
|
||||||
|
pd.write(";\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
pd.writeFileOptions()
|
pd.writeFileOptions()
|
||||||
|
|
||||||
@@ -310,12 +317,12 @@ func NewFromBytes(payload []byte) (*ProtoDefinition, error) {
|
|||||||
return nil, fmt.Errorf("Couldn't unmarshal proto: %w", err)
|
return nil, fmt.Errorf("Couldn't unmarshal proto: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewFromDescriptor(pb)
|
return NewFromDescriptor(&pb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFromDescriptor(pb descriptorpb.FileDescriptorProto) (*ProtoDefinition, error) {
|
func NewFromDescriptor(pb *descriptorpb.FileDescriptorProto) (*ProtoDefinition, error) {
|
||||||
fileOptions := protodesc.FileOptions{AllowUnresolvable: true}
|
fileOptions := protodesc.FileOptions{AllowUnresolvable: true}
|
||||||
descriptor, err := fileOptions.New(&pb, &protoregistry.Files{})
|
descriptor, err := fileOptions.New(pb, &protoregistry.Files{})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Couldn't create FileDescriptor: %w", err)
|
return nil, fmt.Errorf("Couldn't create FileDescriptor: %w", err)
|
||||||
|
|||||||
+10
-23
@@ -1,59 +1,46 @@
|
|||||||
package protodump
|
package protodump
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
|
"google.golang.org/protobuf/types/descriptorpb"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FIXTURES = "fixtures"
|
const FIXTURES = "fixtures"
|
||||||
|
|
||||||
var regex = regexp.MustCompile("(?m)var .* = \\[\\]byte{([^}]*)}")
|
var regex = regexp.MustCompile("(?m)var .* = \\[\\]byte{([^}]*)}")
|
||||||
|
|
||||||
func convertProtoToFileDescriptor(filePath string) ([]byte, error) {
|
func convertProtoToFileDescriptor(filePath string) (*descriptorpb.FileDescriptorProto, error) {
|
||||||
dir, err := os.MkdirTemp("", "")
|
dir, err := os.MkdirTemp("", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
err = exec.Command("protoc", fmt.Sprintf("--go_out=%s", dir), filePath).Run()
|
filename := "proto.bin"
|
||||||
|
err = exec.Command("protoc", fmt.Sprintf("--go_out=%s", dir), fmt.Sprintf("--descriptor_set_out=%s", path.Join(dir, filename)), filePath).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fileName := fmt.Sprintf("%s.pb.go", strings.TrimSuffix(path.Base(filePath), ".proto"))
|
data, err := os.ReadFile(path.Join(dir, filename))
|
||||||
data, err := os.ReadFile(path.Join(dir, fileName))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
match := regex.FindSubmatch(data)
|
var descriptor descriptorpb.FileDescriptorSet
|
||||||
if len(match) != 2 {
|
err = proto.Unmarshal(data, &descriptor)
|
||||||
return nil, fmt.Errorf("Could not find file descriptor bytes in %s", fileName)
|
|
||||||
}
|
|
||||||
|
|
||||||
var hexString strings.Builder
|
|
||||||
for _, char := range strings.ToLower(string(bytes.ReplaceAll(match[1], []byte("0x"), []byte{}))) {
|
|
||||||
r := rune(char)
|
|
||||||
if (r >= '0' && r <= '9') || (r >= 'a' && r <= 'f') {
|
|
||||||
hexString.WriteRune(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := hex.DecodeString(hexString.String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return result, nil
|
return descriptor.GetFile()[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDefinitions(t *testing.T) {
|
func TestDefinitions(t *testing.T) {
|
||||||
@@ -69,7 +56,7 @@ func TestDefinitions(t *testing.T) {
|
|||||||
expected, err := os.ReadFile(filePath)
|
expected, err := os.ReadFile(filePath)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
actual, err := NewFromBytes(descriptor)
|
actual, err := NewFromDescriptor(descriptor)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, string(expected), actual.String())
|
assert.Equal(t, string(expected), actual.String())
|
||||||
|
|||||||
Reference in new issue
Block a user