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

fix: outputs UTF-8 characters in ProtocolBuffer format #172

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions testdata/person_ko.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

민수%minsu@example.com"
555-1213
14 changes: 10 additions & 4 deletions valueformatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (

"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/dynamic"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -225,15 +227,19 @@ func (f *valueFormatting) pbFormatter(ctype string) (valueFormatter, error) {
if md == nil {
return nil, fmt.Errorf("no Protocol-Buffer message time for: %v", ctype)
}
protoV2md := md.UnwrapMessage()

return func(in []byte) (string, error) {
message := dynamic.NewMessage(md)
err := message.Unmarshal(in)
message := dynamicpb.NewMessage(protoV2md)
err := proto.Unmarshal(in, message)
if err != nil {
return "", fmt.Errorf("couldn't deserialize bytes to protobuffer message: %v", err)
}

data, err := message.MarshalTextIndent()
data, err := prototext.MarshalOptions{
Multiline: true,
Indent: " ",
}.Marshal(message)
if err != nil {
return "", fmt.Errorf("couldn't serialize message to bytes: %v", err)
}
Expand Down
71 changes: 48 additions & 23 deletions valueformatting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,38 @@ func TestValueFormattingPBFormatter(t *testing.T) {
}

got, err := formatter(in)
want := `name: "Jim"
id: 42
email: "jim@example.com"
phones: <
number: "555-1212"
type: HOME
>`
want := `name: "Jim"
id: 42
email: "jim@example.com"
phones: {
number: "555-1212"
type: HOME
}
`

if err != nil {
t.Errorf("Error creating protobuf formatter: %v", err)
}

if got != want {
t.Errorf("Protobuf not formatted correctly: wanted %s; got %s",
want, got)
}

in, err = ioutil.ReadFile(filepath.Join("testdata", "person_ko.bin"))
if err != nil {
t.Errorf("Error reading testdata: %v", err)
}

got, err = formatter(in)
want = `name: "민수"
id: 37
email: "minsu@example.com"
phones: {
number: "555-1213"
type: WORK
}
`

if err != nil {
t.Errorf("Error creating protobuf formatter: %v", err)
Expand Down Expand Up @@ -526,13 +551,13 @@ func TestValueFormattingFormat(t *testing.T) {

in, err := ioutil.ReadFile(filepath.Join("testdata", "person.bin"))
want =
" name: \"Jim\"\n" +
" id: 42\n" +
" email: \"jim@example.com\"\n" +
" phones: <\n" +
" number: \"555-1212\"\n" +
" type: HOME\n" +
" >\n"
" name: \"Jim\"\n" +
" id: 42\n" +
" email: \"jim@example.com\"\n" +
" phones: {\n" +
" number: \"555-1212\"\n" +
" type: HOME\n" +
" }\n\n"

if err != nil {
t.Errorf("Error when reading testdata: %v", err)
Expand Down Expand Up @@ -605,8 +630,8 @@ func TestProtobufferAndYAML(t *testing.T) {
want := ("----------------------------------------\n" +
"r1\n" +
" f1:cat\n" +
" name: \"Brave\"\n" +
" age: 2")
" name: \"Brave\"\n" +
" age: 2\n")

timestampsRE := regexp.MustCompile("[ ]+@ [^ \t\n]+")

Expand Down Expand Up @@ -690,13 +715,13 @@ func TestPrintRow(t *testing.T) {
" f1:c2\n" +
" 258\n" +
" f2:person\n" +
" name: \"Jim\"\n" +
" id: 42\n" +
" email: \"jim@example.com\"\n" +
" phones: <\n" +
" number: \"555-1212\"\n" +
" type: HOME\n" +
" >\n" +
" name: \"Jim\"\n" +
" id: 42\n" +
" email: \"jim@example.com\"\n" +
" phones: {\n" +
" number: \"555-1212\"\n" +
" type: HOME\n" +
" }\n\n" +
"")

var out2 bytes.Buffer
Expand Down