Skip to content

Commit

Permalink
Remove patterns
Browse files Browse the repository at this point in the history
Added `Template()` method (with option to remove patterns)
  • Loading branch information
marrow16 committed Jul 19, 2023
1 parent fece0f4 commit 21e48eb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
12 changes: 12 additions & 0 deletions path_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ func (pt *pathPart) getVars(vars []PathVar, namePosns map[string]int) []PathVar
return vars
}

func (pt *pathPart) buildNoPattern(builder *strings.Builder) {
if pt.fixed {
builder.WriteString(pt.fixedValue)
} else if len(pt.subParts) > 0 {
for _, sp := range pt.subParts {
sp.buildNoPattern(builder)
}
} else {
builder.WriteString("{" + pt.name + "}")
}
}

type positionsTracker struct {
vars PathVars
varPosition int
Expand Down
18 changes: 18 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type Template interface {
Vars() []PathVar
// OriginalTemplate returns the original (or generated) path template string
OriginalTemplate() string
// Template returns the template (optionally with any path var patterns removed)
Template(removePatterns bool) string
}

type template struct {
Expand Down Expand Up @@ -316,6 +318,22 @@ func (t *template) OriginalTemplate() string {
return t.originalTemplate
}

// Template returns the template (optionally with any path var patterns removed)
func (t *template) Template(removePatterns bool) string {
if removePatterns {
var builder strings.Builder
if len(t.pathParts) == 0 {
builder.WriteString("/")
}
for _, pt := range t.pathParts {
builder.WriteString("/")
pt.buildNoPattern(&builder)
}
return builder.String()
}
return t.originalTemplate
}

func separatePathOptions(options []interface{}) (host HostOption, params QueryParamsOption, headers HeadersOption, varMatches varMatchOptions) {
for _, intf := range options {
if h, ok := intf.(HostOption); ok {
Expand Down
45 changes: 45 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package urit

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/url"
Expand Down Expand Up @@ -607,6 +608,50 @@ func TestTemplate_Vars(t *testing.T) {
}
}

func TestTemplate_Template(t *testing.T) {
testCases := []struct {
path string
expect string
}{
{
path: "/",
expect: "/",
},
{
path: "/foo/bar/baz",
expect: "/foo/bar/baz",
},
{
path: "/foo/{fooid}/bar/{barid}/baz/{bazid}",
expect: "/foo/{fooid}/bar/{barid}/baz/{bazid}",
},
{
path: "/foo/{fooid: [a-z]*}/bar/{barid: [a-z]*}/baz/{bazid: [a-z]*}",
expect: "/foo/{fooid}/bar/{barid}/baz/{bazid}",
},
{
path: "/foo/{fooA}-{fooB}",
expect: "/foo/{fooA}-{fooB}",
},
{
path: "/foo/{fooA: [a-z]*}-{fooB: [a-z]*}",
expect: "/foo/{fooA}-{fooB}",
},
{
path: "/foo/{fooA: [a-z]*}-{fooB: [a-z]*}/bar/{barA}-{barB}",
expect: "/foo/{fooA}-{fooB}/bar/{barA}-{barB}",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("[%d]", i+1), func(t *testing.T) {
tmp, err := NewTemplate(tc.path)
assert.NoError(t, err)
assert.Equal(t, tc.expect, tmp.Template(true))
assert.Equal(t, tc.path, tmp.Template(false))
})
}
}

type uuidChecker struct {
}

Expand Down

0 comments on commit 21e48eb

Please sign in to comment.