this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Join string slices in makeParams to fix broken urlencodes (#145)

Currently the `makeParams` function will take a string slice and URL
encode it as `[hello+world]` instead of `hello,world` which makes the
XRPC API upset (HTTP:400 errors) since it wants a comma separated list
of strings. This change tries to cast the params interface into a string
slice, and if it matches, it does a join instead of just printing the
debug value string into the query params.

Tested in the
[GraphBuilder](https://github.com/ericvolp12/bsky-experiments/blob/main/pkg/events/helpers.go#L20)
and hasn't broken anything, not sure if/what the testing plan is here.

That function previously threw 400s trying to talk to the XRPC API.

That project is using a [module
replace](https://github.com/ericvolp12/bsky-experiments/blob/main/go.mod#L107)
to test my commit in place of the upstream indigo package.

authored by

Whyrusleeping and committed by
GitHub
b18c0e75 4508f3e4

+70 -4
+11 -4
xrpc/xrpc.go
··· 44 44 Procedure 45 45 ) 46 46 47 - func makeParams(p map[string]interface{}) string { 48 - var parts []string 47 + // makeParams converts a map of string keys and any values into a URL-encoded string. 48 + // If a value is a slice of strings, it will be joined with commas. 49 + // Generally the values will be strings, numbers, booleans, or slices of strings 50 + func makeParams(p map[string]any) string { 51 + params := url.Values{} 49 52 for k, v := range p { 50 - parts = append(parts, fmt.Sprintf("%s=%s", k, url.QueryEscape(fmt.Sprint(v)))) 53 + if s, ok := v.([]string); ok { 54 + params.Add(k, strings.Join(s, ",")) 55 + } else { 56 + params.Add(k, fmt.Sprint(v)) 57 + } 51 58 } 52 59 53 - return strings.Join(parts, "&") 60 + return params.Encode() 54 61 } 55 62 56 63 func (c *Client) Do(ctx context.Context, kind XRPCRequestType, inpenc string, method string, params map[string]interface{}, bodyobj interface{}, out interface{}) error {
+59
xrpc/xrpc_test.go
··· 1 + package xrpc 2 + 3 + import ( 4 + "testing" 5 + ) 6 + 7 + // TestMakeParams tests the makeParams function. 8 + func TestMakeParams(t *testing.T) { 9 + testCases := []struct { 10 + name string 11 + input map[string]interface{} 12 + expected string 13 + }{ 14 + { 15 + name: "Empty input", 16 + input: map[string]interface{}{}, 17 + expected: "", 18 + }, 19 + { 20 + name: "Single value", 21 + input: map[string]interface{}{ 22 + "key": "value", 23 + }, 24 + expected: "key=value", 25 + }, 26 + { 27 + name: "Multiple values", 28 + input: map[string]interface{}{ 29 + "key1": "value1", 30 + "key2": "value2", 31 + }, 32 + expected: "key1=value1&key2=value2", 33 + }, 34 + { 35 + name: "Slice of strings", 36 + input: map[string]interface{}{ 37 + "key": []string{"value1", "value2", "value3"}, 38 + }, 39 + expected: "key=value1%2Cvalue2%2Cvalue3", 40 + }, 41 + { 42 + name: "Mixed values", 43 + input: map[string]interface{}{ 44 + "key1": "value1", 45 + "key2": []string{"value2", "value3"}, 46 + }, 47 + expected: "key1=value1&key2=value2%2Cvalue3", 48 + }, 49 + } 50 + 51 + for _, tc := range testCases { 52 + t.Run(tc.name, func(t *testing.T) { 53 + result := makeParams(tc.input) 54 + if result != tc.expected { 55 + t.Errorf("got '%q', want '%q'", result, tc.expected) 56 + } 57 + }) 58 + } 59 + }