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(encoding/form): optimize EncodeField and add test cases #3234

Merged
merged 2 commits into from
Mar 22, 2024
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
4 changes: 1 addition & 3 deletions encoding/form/proto_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,12 @@ func EncodeField(fieldDescriptor protoreflect.FieldDescriptor, value protoreflec
}
desc := fieldDescriptor.Enum().Values().ByNumber(value.Enum())
return string(desc.Name()), nil
case protoreflect.StringKind:
return value.String(), nil
case protoreflect.BytesKind:
return base64.URLEncoding.EncodeToString(value.Bytes()), nil
case protoreflect.MessageKind, protoreflect.GroupKind:
return encodeMessage(fieldDescriptor.Message(), value)
default:
return fmt.Sprint(value.Interface()), nil
return value.String(), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a feature or fix?

Copy link
Member Author

@demoManito demoManito Mar 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Not a feature or fix, optimized by the way.

value.String() already contains fmt.Sprint(value.Interface()), for non-special types just use .String()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm talking about is the title issue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

Expand Down
31 changes: 16 additions & 15 deletions encoding/form/proto_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ func TestEncodeValues(t *testing.T) {
t.Cleanup(func() { time.Local = loc })

in := &complex.Complex{
Id: 2233,
NoOne: "2233",
Simple: &complex.Simple{Component: "5566"},
Simples: []string{"3344", "5566"},
B: true,
Sex: complex.Sex_woman,
Age: 18,
A: 19,
Count: 3,
Price: 11.23,
D: 22.22,
Byte: []byte("123"),
Map: map[string]string{"kratos": "https://go-kratos.dev/", "kratos_start": "https://go-kratos.dev/en/docs/getting-started/start/"},
Id: 2233,
NoOne: "2233",
Simple: &complex.Simple{Component: "5566"},
Simples: []string{"3344", "5566"},
B: true,
Sex: complex.Sex_woman,
Age: 18,
A: 19,
Count: 3,
Price: 11.23,
D: 22.22,
Byte: []byte("123"),
Map: map[string]string{"kratos": "https://go-kratos.dev/", "kratos_start": "https://go-kratos.dev/en/docs/getting-started/start/"},
MapInt64Key: map[int64]string{1: "kratos", 2: "go-zero"},

Timestamp: &timestamppb.Timestamp{Seconds: 20, Nanos: 2},
Duration: &durationpb.Duration{Seconds: 120, Nanos: 22},
Expand All @@ -49,9 +50,9 @@ func TestEncodeValues(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want := "a=19&age=18&b=true&bool=false&byte=MTIz&bytes=MTIz&count=3&d=22.22&double=12.33&duration=2m0.000000022s&field=1%2C2&float=12.34&id=2233&int32=32&int64=64&map%5Bkratos%5D=https%3A%2F%2Fgo-kratos.dev%2F&map%5Bkratos_start%5D=https%3A%2F%2Fgo-kratos.dev%2Fen%2Fdocs%2Fgetting-started%2Fstart%2F&numberOne=2233&price=11.23&sex=woman&simples=3344&simples=5566&string=go-kratos&timestamp=1970-01-01T00%3A00%3A20.000000002Z&uint32=32&uint64=64&very_simple.component=5566" // nolint:lll
want := "a=19&age=18&b=true&bool=false&byte=MTIz&bytes=MTIz&count=3&d=22.22&double=12.33&duration=2m0.000000022s&field=1%2C2&float=12.34&id=2233&int32=32&int64=64&map%5Bkratos%5D=https%3A%2F%2Fgo-kratos.dev%2F&map%5Bkratos_start%5D=https%3A%2F%2Fgo-kratos.dev%2Fen%2Fdocs%2Fgetting-started%2Fstart%2F&map_int64_key%5B1%5D=kratos&map_int64_key%5B2%5D=go-zero&numberOne=2233&price=11.23&sex=woman&simples=3344&simples=5566&string=go-kratos&timestamp=1970-01-01T00%3A00%3A20.000000002Z&uint32=32&uint64=64&very_simple.component=5566" // nolint:lll
if got := query.Encode(); want != got {
t.Errorf("want: %s, got: %s", want, got)
t.Errorf("\nwant: %s, \ngot: %s", want, got)
}
}

Expand Down
163 changes: 91 additions & 72 deletions internal/testdata/complex/complex.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/testdata/complex/complex.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ message Complex {
google.protobuf.BytesValue bytes = 24;

map<string,string> map = 25;
map<int64,string> map_int64_key = 26 [json_name = "map_int64_key"];
}

message Simple {
Expand Down
3 changes: 3 additions & 0 deletions internal/testdata/complex/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package complex

//go:generate protoc -I . --go_out=paths=source_relative:. ./complex.proto
Loading