Skip to content

Commit c6bdd1f

Browse files
committed
Fix examples in README
1 parent b7e7db6 commit c6bdd1f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ _test
1818

1919
vendor/*
2020
!vendor/vendor.json
21+
examples

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,20 @@ fmt.Printf("%#v\n", tokens[2])
224224
Path-To-RegExp exposes a compile function for transforming a string into a valid path.
225225

226226
```go
227+
falseValue := false
227228
toPath := pathToRegexp.MustCompile("/user/:id", nil)
228229

229230
toPath(map[string]int{"id": 123}, nil) //=> "/user/123"
230231
toPath(map[string]string{"id": "café"}, nil) //=> "/user/caf%C3%A9"
231232
toPath(map[string]string{"id": "/"}, nil) //=> "/user/%2F"
232233

233234
toPath(map[string]string{"id": ":/"}, nil) //=> "/user/%3A%2F"
234-
toPath(map[string]string{"id": ":&"}, &Options{encode: func(value string, token interface{}) string {
235-
return value
236-
}}) //=> panic
237-
toPath(map[string]string{"id": ":&"}, nil) //=> "/user/%3A%26"
238-
toPath(map[string]string{"id": ":&"}, &Options{encode: func(value string, token interface{}) string {
239-
return value
240-
}}) //=> /user/:&
235+
toPath(map[string]string{"id": ":/"}, &Options{
236+
Encode: func(value string, token interface{}) string {
237+
return value
238+
},
239+
Validate: &falseValue,
240+
}) //=> "/user/:/"
241241

242242
toPathRepeated := pathToRegexp.MustCompile("/:segment+", nil)
243243

@@ -249,8 +249,7 @@ toPathRegexp := pathToRegexp.MustCompile("/user/:id(\\d+)", nil)
249249
toPathRegexp(map[string]int{"id": 123}, nil) //=> "/user/123"
250250
toPathRegexp(map[string]string{"id": "123"}, nil) //=> "/user/123"
251251
toPathRegexp(map[string]string{"id": "abc"}, nil) //=> panic
252-
t1 := true
253-
toPathRegexp(map[string]string{"id": "abc"}, &Options{validate: &t1}) //=> panic
252+
toPathRegexp(map[string]string{"id": "abc"}, &Options{Validate: &falseValue}) //=> "/user/abc"
254253
```
255254

256255
**Note:** The generated function will panic on invalid input. It will do all necessary checks to ensure the generated path is valid. This method only works with strings.

0 commit comments

Comments
 (0)