Skip to content

Commit bb52361

Browse files
authored
Merge 9d25284 into 1ab258e
2 parents 1ab258e + 9d25284 commit bb52361

File tree

5 files changed

+112
-90
lines changed

5 files changed

+112
-90
lines changed

encoding/form/proto_encode.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,12 @@ func EncodeField(fieldDescriptor protoreflect.FieldDescriptor, value protoreflec
137137
}
138138
desc := fieldDescriptor.Enum().Values().ByNumber(value.Enum())
139139
return string(desc.Name()), nil
140-
case protoreflect.StringKind:
141-
return value.String(), nil
142140
case protoreflect.BytesKind:
143141
return base64.URLEncoding.EncodeToString(value.Bytes()), nil
144142
case protoreflect.MessageKind, protoreflect.GroupKind:
145143
return encodeMessage(fieldDescriptor.Message(), value)
146144
default:
147-
return fmt.Sprint(value.Interface()), nil
145+
return value.String(), nil
148146
}
149147
}
150148

encoding/form/proto_encode_test.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ func TestEncodeValues(t *testing.T) {
1818
t.Cleanup(func() { time.Local = loc })
1919

2020
in := &complex.Complex{
21-
Id: 2233,
22-
NoOne: "2233",
23-
Simple: &complex.Simple{Component: "5566"},
24-
Simples: []string{"3344", "5566"},
25-
B: true,
26-
Sex: complex.Sex_woman,
27-
Age: 18,
28-
A: 19,
29-
Count: 3,
30-
Price: 11.23,
31-
D: 22.22,
32-
Byte: []byte("123"),
33-
Map: map[string]string{"kratos": "https://go-kratos.dev/", "kratos_start": "https://go-kratos.dev/en/docs/getting-started/start/"},
21+
Id: 2233,
22+
NoOne: "2233",
23+
Simple: &complex.Simple{Component: "5566"},
24+
Simples: []string{"3344", "5566"},
25+
B: true,
26+
Sex: complex.Sex_woman,
27+
Age: 18,
28+
A: 19,
29+
Count: 3,
30+
Price: 11.23,
31+
D: 22.22,
32+
Byte: []byte("123"),
33+
Map: map[string]string{"kratos": "https://go-kratos.dev/", "kratos_start": "https://go-kratos.dev/en/docs/getting-started/start/"},
34+
MapInt64Key: map[int64]string{1: "kratos", 2: "go-zero"},
3435

3536
Timestamp: &timestamppb.Timestamp{Seconds: 20, Nanos: 2},
3637
Duration: &durationpb.Duration{Seconds: 120, Nanos: 22},
@@ -49,9 +50,9 @@ func TestEncodeValues(t *testing.T) {
4950
if err != nil {
5051
t.Fatal(err)
5152
}
52-
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
53+
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
5354
if got := query.Encode(); want != got {
54-
t.Errorf("want: %s, got: %s", want, got)
55+
t.Errorf("\nwant: %s, \ngot: %s", want, got)
5556
}
5657
}
5758

internal/testdata/complex/complex.pb.go

+91-72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/testdata/complex/complex.proto

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ message Complex {
4040
google.protobuf.BytesValue bytes = 24;
4141

4242
map<string,string> map = 25;
43+
map<int64,string> map_int64_key = 26 [json_name = "map_int64_key"];
4344
}
4445

4546
message Simple {

internal/testdata/complex/generate.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package complex
2+
3+
//go:generate protoc -I . --go_out=paths=source_relative:. ./complex.proto

0 commit comments

Comments
 (0)