Skip to content

Commit ffe5602

Browse files
alvii147gavv
authored andcommitted
fix WithPath to work for floating point numbers
1 parent 2c2e4c8 commit ffe5602

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

request.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"reflect"
1616
"sort"
17+
"strconv"
1718
"strings"
1819
"sync"
1920
"time"
@@ -1049,7 +1050,13 @@ func (r *Request) withPath(opChain *chain, key string, value interface{}) {
10491050
},
10501051
})
10511052
} else {
1052-
mustWrite(w, fmt.Sprint(value))
1053+
switch value.(type) {
1054+
case float64, float32:
1055+
v := value.(float64)
1056+
mustWrite(w, strconv.FormatFloat(v, 'f', -1, 64))
1057+
default:
1058+
mustWrite(w, fmt.Sprint(value))
1059+
}
10531060
found = true
10541061
}
10551062
} else {

request_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,24 @@ func TestRequest_Path(t *testing.T) {
10741074
req.WithPath("arg", nil)
10751075
req.chain.assert(t, failure)
10761076
})
1077+
1078+
t.Run("floating point", func(t *testing.T) {
1079+
req := NewRequestC(config, "GET", "/{arg1}")
1080+
req.WithPath("arg1", 12345.6789)
1081+
req.Expect().chain.assert(t, success)
1082+
require.NotNil(t, client.req)
1083+
assert.Equal(t, "http://example.com/12345.6789",
1084+
client.req.URL.String())
1085+
})
1086+
1087+
t.Run("floating point with trailing zero", func(t *testing.T) {
1088+
req := NewRequestC(config, "GET", "/{arg1}")
1089+
req.WithPath("arg1", 12345.0)
1090+
req.Expect().chain.assert(t, success)
1091+
require.NotNil(t, client.req)
1092+
assert.Equal(t, "http://example.com/12345",
1093+
client.req.URL.String())
1094+
})
10771095
}
10781096

10791097
func TestRequest_PathObject(t *testing.T) {

0 commit comments

Comments
 (0)