Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for proto2 default values #4

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: '1.20'
- name: Install Protoc
run: |
sudo apt install -y protobuf-compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
- name: Test
run: make test
- name: Build
Expand Down
19 changes: 19 additions & 0 deletions pkg/protodump/fixtures/test.proto2
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto2";

package hello.world;

option go_package = "./;helloworld";

message SearchRequest {
optional string query = 1 [default = "hello"];
optional int32 page_number = 2;
optional int32 result_per_page = 3 [default = 10];
optional .hello.world.Corpus corpus = 4 [default = CORPUS_UNIVERSAL];
optional bool field = 5 [default = true];
}

enum Corpus {
CORPUS_UNSPECIFIED = 0;
CORPUS_UNIVERSAL = 1;
}

File renamed without changes.
13 changes: 13 additions & 0 deletions pkg/protodump/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ func (pd *ProtoDefinition) writeField(field protoreflect.FieldDescriptor) {
pd.write(string(field.Name()))
pd.write(" = ")
pd.write(strconv.Itoa(int(field.Number())))
if field.HasDefault() {
pd.write(" [default = ")
kind := field.Kind().String()
if kind == "string" {
pd.write(fmt.Sprintf("\"%s\"", field.Default().String()))
} else if kind == "enum" {
pd.write(string(field.Enum().Values().ByNumber(field.Default().Enum()).Name()))
} else {
pd.write(field.Default().String())
}

pd.write("]")
}
pd.write(";\n")
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/protodump/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"os/exec"
"path"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,8 +14,6 @@ import (

const FIXTURES = "fixtures"

var regex = regexp.MustCompile("(?m)var .* = \\[\\]byte{([^}]*)}")

func convertProtoToFileDescriptor(filePath string) (*descriptorpb.FileDescriptorProto, error) {
dir, err := os.MkdirTemp("", "")
if err != nil {
Expand Down