Add support for proto2 default values

This commit is contained in:
Arkadiy Tetelman committed 2023-08-17 22:40:09 -07:00
1 parent 49de7c273a
commit 874e4d3f8d
5 files changed
+35 -6

No files matched your search

+3 -3
View File
@@ -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
+19
View File
@@ -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
View File
@@ -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")
}
-3
View File
@@ -5,7 +5,6 @@ import (
"os"
"os/exec"
"path"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
@@ -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 {