-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_filter.go
50 lines (39 loc) · 896 Bytes
/
url_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2019 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package filter
import (
"fmt"
"net/url"
"reflect"
)
type urlFilter struct {
stripQueryParameters []string
}
// NewURLFilter constructor.
func NewURLFilter(opts ...URLOption) Filter {
f := &urlFilter{
stripQueryParameters: []string{},
}
for _, opt := range opts {
opt(f)
}
return f
}
func (f urlFilter) Filter(value Value) (Value, error) {
switch val := value.(type) {
case string:
u, err := url.Parse(val)
if err != nil {
return value, fmt.Errorf("parsing url failed: %w", err)
}
q := u.Query()
for _, key := range f.stripQueryParameters {
q.Del(key)
}
u.RawQuery = q.Encode()
return u.String(), nil
default:
return value, fmt.Errorf("URLFilter: unsupported type %v", reflect.TypeOf(value))
}
}