Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/valueextractor.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package valueextractor
import (
"fmt"
"strconv"
"strings"
)

// Converter is a function that takes an Extractor and a key and returns a value and an error
Expand All @@ -18,6 +19,17 @@ func ReturnString(ec *Extractor, key string) *string {
return &s
}

func ReturnStringArray(ec *Extractor, key string) *[]string {
var s []string
var str string
ec.With(key, AsString(&str))
keys := strings.Split(str, ",")
for _, k := range keys {
s = append(s, k)
}
return &s
}

// AsString maintains the value as a string, just allowing extraction
func AsString(ref *string) Converter {
return func(ec *Extractor, value string) error {
Expand Down
19 changes: 18 additions & 1 deletion extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,32 @@ func BenchmarkParamsParser(b *testing.B) {
}
}

// compareSlices compares two slices of strings and returns true if they are equal.
func compareSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func BenchmarkTypeReturn(b *testing.B) {
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30&roles=hr,admin,super_admin", nil)

for i := 0; i < b.N; i++ {
ex := Using(QueryExtractor{Query: req.URL.Query()})

if *ReturnString(ex, "name") != "John" {
b.Fatal("Name not parsed correctly")
}
s := []string{"hr", "admin", "super_admin"}
if compareSlices(*ReturnStringArray(ex, "roles"), s) {
b.Fatal("roles not parsed correctly")
}

if *ReturnUint64(ex, "age") != 30 {
b.Fatal("Age not parsed correctly")
Expand Down