Skip to content

Commit

Permalink
feat(path): support parsing path wildcard characters
Browse files Browse the repository at this point in the history
  • Loading branch information
polebug committed Dec 18, 2023
1 parent 77413a1 commit f04828e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
18 changes: 15 additions & 3 deletions operation_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,22 @@ func (p *parsedParam) loadURL(qs url.Values) (reflect.Value, error) { //nolint:
val := reflect.New(p.param)

for _, f := range p.fields {
var fv reflect.Value
var (
fv reflect.Value
err error
)

if !f.InPath && f.slice { //nolint: nestif
if f.name == "path" {

Check failure on line 63 in operation_params.go

View workflow job for this annotation

GitHub Actions / test

ifElseChain: rewrite if-else to switch statement (gocritic)

Check failure on line 63 in operation_params.go

View workflow job for this annotation

GitHub Actions / test

ifElseChain: rewrite if-else to switch statement (gocritic)
vs, has := qs["*"]
if !has {
continue
}

fv, err = toValue(f.item, vs[0])
if err != nil {
return reflect.Value{}, fmt.Errorf("failed to parse url path param `%s`: %w", f.name, err)
}
} else if !f.InPath && f.slice {
vs, has := qs[f.name]
if has { //nolint: gocritic
fv = reflect.MakeSlice(f.sliceType, len(vs), len(vs))
Expand All @@ -78,7 +91,6 @@ func (p *parsedParam) loadURL(qs url.Values) (reflect.Value, error) { //nolint:
} else {
vs, has := qs[f.name]
if has { //nolint: gocritic
var err error
fv, err = toValue(f.item, vs[0])
if err != nil {
return reflect.Value{}, fmt.Errorf("failed to parse url path param `%s`: %w", f.name, err)
Expand Down
27 changes: 27 additions & 0 deletions operation_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ func Test_loadURL(t *testing.T) {
})
}

func Test_loadURL_any(t *testing.T) {
g := got.T(t)

type params struct {
InURL

Path string `path:"*"`
}

path, err := newPath("/test/*", false)
g.E(err)

s := jschema.New("")

parsed := parseParam(s, path, reflect.TypeOf(params{}))

v, err := parsed.loadURL(url.Values{
"*": []string{"a/b/c"},
})
g.E(err)

g.Eq(v.Interface(), params{
InURL: InURL{},
Path: "a/b/c",
})
}

func Test_loadURL_nil(t *testing.T) {
g := got.T(t)

Expand Down
6 changes: 3 additions & 3 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func newPath(path string, optionalSlash bool) (*Path, error) {

var regexPath string
if strings.HasSuffix(path, "/*") {
regexPath = "^" + strings.ReplaceAll(path[:len(path)-2], "/", "\\/") + "(?:\\/(?P<wildcard>.*))?$"
regexPath = "^" + strings.ReplaceAll(path[:len(path)-2], "/", "\\/") + "(?:\\/(?P<path>.*))?$"

params = append(params, "wildcard")
params = append(params, "path")
} else {
// Replace OpenAPI wildcards with Go RegExp named wildcards
regexPath = regOpenAPIPath.ReplaceAllStringFunc(path, func(m string) string {
Expand Down Expand Up @@ -62,7 +62,7 @@ func (p *Path) match(path string) map[string]string {
continue
}

if name == "wildcard" {
if name == "path" {
matches["*"] = ms[i]
} else {
matches[name] = ms[i]
Expand Down

0 comments on commit f04828e

Please sign in to comment.