-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuruki.go
50 lines (45 loc) · 1.39 KB
/
uruki.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
package uruki
import (
"errors"
"net/url"
)
var (
// ErrInvalidURL invalid url
ErrInvalidURL = errors.New("invalid scheme url, it should https or http prefix")
)
// Builder base struct uruki
type Builder struct {
url *url.URL
defaultSpaceEncode string
restrictedScheme map[string]bool
useEscapeAutomateURL bool
}
// Option options to create new Builder
type Option struct {
// URL: url to proceed
URL string
// RestrictScheme: if you want your url scheme keep as is, example []string{"https"} if any url set with "http" options this will be return error
RestrictScheme []string
// DefaultSpaceEncode: default space encoding for internally encode. see SpaceEncoding const for more the details
DefaultSpaceEncode string
// UseEscapeAutomateURL to automate query escape on existing url query parameter while initiating builder / SetURL(uri string)
UseEscapeAutomateURL bool
}
// NewBuilder create uruki (URi qUicK buIlder) parser & wrapper of net/url
func NewBuilder(options ...Option) (*Builder, error) {
ub := &Builder{url: &url.URL{}}
if len(options) > 0 {
opt := options[0]
ub.defaultSpaceEncode = opt.DefaultSpaceEncode
ub.useEscapeAutomateURL = opt.UseEscapeAutomateURL
ub.setRestrictedScheme(opt.RestrictScheme)
err := ub.setURL(opt.URL)
if err != nil {
return nil, err
}
if ub.useEscapeAutomateURL {
ub.queryEscapeAutomate()
}
}
return ub, nil
}