Skip to content

Commit dc26601

Browse files
committed
Support PathParams
1 parent e265423 commit dc26601

File tree

3 files changed

+172
-8
lines changed

3 files changed

+172
-8
lines changed

context.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,9 @@ func (c *Context) CLog(callerDepth int) *logrus.Entry {
140140
}).WithFields(c.logFields)
141141
}
142142

143-
func (c *Context) extractOptionStringFromPath(key string, target *string) error {
144-
s, ok := c.request.PathParams[key]
145-
if !ok {
146-
return nil
147-
}
148-
149-
*target = s
150-
return nil
143+
// PathParams returns parameters embedded in the URL.
144+
func (c *Context) PathParams() *PathParams {
145+
return &PathParams{c.request}
151146
}
152147

153148
func (c *Context) render(status int, v interface{}) {

params.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package jasco
2+
3+
import (
4+
"fmt"
5+
"github.com/gocraft/web"
6+
"strconv"
7+
)
8+
9+
// PathParams contains parameters embedded in the URL supported by gocraft/web
10+
type PathParams struct {
11+
req *web.Request
12+
}
13+
14+
// String returns a string value corresponding to key. When the key doesn't
15+
// exist, it returns defaultValue.
16+
func (p *PathParams) String(key string, defaultValue string) string {
17+
s, ok := p.req.PathParams[key]
18+
if !ok {
19+
return defaultValue
20+
}
21+
return s
22+
}
23+
24+
// RequiredString return a string value corresponding to the key. When the key
25+
// doesn't exist, it returns an error.
26+
func (p *PathParams) RequiredString(key string) (string, error) {
27+
s, ok := p.req.PathParams[key]
28+
if !ok {
29+
return "", fmt.Errorf("path parameter '%v' doesn't exist", key)
30+
}
31+
return s, nil
32+
}
33+
34+
// Int returns a positive integer value correspoding to the key as uint64. When
35+
// the key doesn't exist, it returns defaultValue. It returns an error if it
36+
// cannot parse the path string parameter as an integer or the value is greater
37+
// than or equal to 2^63.
38+
func (p *PathParams) Int(key string, defaultValue uint64) (uint64, error) {
39+
s, err := p.RequiredString(key)
40+
if err != nil {
41+
return defaultValue, nil
42+
}
43+
return strconv.ParseUint(s, 10, 63)
44+
}
45+
46+
func (p *PathParams) RequiredInt(key string) (uint64, error) {
47+
s, err := p.RequiredString(key)
48+
if err != nil {
49+
return 0, err
50+
}
51+
return strconv.ParseUint(s, 10, 63)
52+
}

params_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package jasco
2+
3+
import (
4+
"github.com/gocraft/web"
5+
. "github.com/smartystreets/goconvey/convey"
6+
"testing"
7+
)
8+
9+
func TestPathParams(t *testing.T) {
10+
Convey("Given a PathParams", t, func() {
11+
p := PathParams{
12+
req: &web.Request{
13+
PathParams: map[string]string{
14+
"str": "value",
15+
"int": "10",
16+
"neg_int": "-10",
17+
},
18+
},
19+
}
20+
21+
Convey("when getting an optional string value", func() {
22+
s := p.String("str", "default")
23+
24+
Convey("it should return the stored value", func() {
25+
So(s, ShouldEqual, "value")
26+
})
27+
})
28+
29+
Convey("when getting a nonexistent string value", func() {
30+
s := p.String("nonexistent_str", "default")
31+
32+
Convey("it should return the default value", func() {
33+
So(s, ShouldEqual, "default")
34+
})
35+
})
36+
37+
Convey("when getting a required string value", func() {
38+
s, err := p.RequiredString("str")
39+
40+
Convey("it should succeed", func() {
41+
So(err, ShouldBeNil)
42+
})
43+
44+
Convey("it should return the stored value", func() {
45+
So(s, ShouldEqual, "value")
46+
})
47+
})
48+
49+
Convey("when getting a required but nonexistent string value", func() {
50+
_, err := p.RequiredString("nonexistent_str")
51+
52+
Convey("it should fail", func() {
53+
So(err, ShouldNotBeNil)
54+
})
55+
})
56+
57+
Convey("when getting an optional positive integer value", func() {
58+
i, err := p.Int("int", 20)
59+
60+
Convey("it should succeed", func() {
61+
So(err, ShouldBeNil)
62+
})
63+
64+
Convey("it should return the stored value", func() {
65+
So(i, ShouldEqual, 10)
66+
})
67+
})
68+
69+
Convey("when getting a nonexistent positive integer value", func() {
70+
i, err := p.Int("nonexistent_int", 20)
71+
72+
Convey("it should succeed", func() {
73+
So(err, ShouldBeNil)
74+
})
75+
76+
Convey("it should return the default value", func() {
77+
So(i, ShouldEqual, 20)
78+
})
79+
})
80+
81+
Convey("when getting a required positive integer value", func() {
82+
i, err := p.RequiredInt("int")
83+
84+
Convey("it should succeed", func() {
85+
So(err, ShouldBeNil)
86+
})
87+
88+
Convey("it should return the stored value", func() {
89+
So(i, ShouldEqual, 10)
90+
})
91+
})
92+
93+
Convey("when getting a required but nonexistent positive integer value", func() {
94+
_, err := p.RequiredInt("nonexistent_int")
95+
96+
Convey("it should fail", func() {
97+
So(err, ShouldNotBeNil)
98+
})
99+
})
100+
101+
Convey("when getting a negative integer", func() {
102+
_, err := p.Int("neg_int", 20)
103+
104+
Convey("it should fail", func() {
105+
So(err, ShouldNotBeNil)
106+
})
107+
})
108+
109+
Convey("when getting a string as an integer", func() {
110+
_, err := p.Int("str", 10)
111+
112+
Convey("it should fail", func() {
113+
So(err, ShouldNotBeNil)
114+
})
115+
})
116+
})
117+
}

0 commit comments

Comments
 (0)