Skip to content

Commit

Permalink
fix(encoding/form): optimize EncodeField and add test cases (#3234)
Browse files Browse the repository at this point in the history
* test(encoding/form): supplementary encode map test cases

* remove string kind convert
  • Loading branch information
demoManito authored Mar 22, 2024
1 parent 26d7d5f commit bfafeca
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 90 deletions.
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
}
}

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

0 comments on commit bfafeca

Please sign in to comment.