-
Notifications
You must be signed in to change notification settings - Fork 4
/
reverse.go
141 lines (117 loc) · 3.1 KB
/
reverse.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package reverse
import (
"errors"
"fmt"
"strings"
)
var Urls *urlStore
func init() {
Urls = &urlStore{store: make(map[string]url)}
}
func Clear() {
for k := range Urls.store {
delete(Urls.store, k)
}
}
// Adds url to store
func Add(urlName, urlAddr string, params ...string) string {
return Urls.MustAdd(urlName, urlAddr, params...)
}
// Adds url with concat group, but returns just urlAddr
func AddGr(urlName, group, urlAddr string, params ...string) string {
return Urls.MustAddGr(urlName, group, urlAddr, params...)
}
// Reverse url by name
func Rev(urlName string, params ...string) string {
return Urls.MustReverse(urlName, params...)
}
// Gets raw url by name
func Get(urlName string) string {
return Urls.Get(urlName)
}
// Gets saved all urls
func GetAllUrls() map[string]string {
out := map[string]string{}
for key, value := range Urls.store {
out[key] = value.url
}
return out
}
// Gets all params
func GetAllParams() map[string][]string {
out := map[string][]string{}
for key, value := range Urls.store {
out[key] = value.params
}
return out
}
type url struct {
url string
params []string
}
type urlStore struct {
store map[string]url
}
// Adds a Url to the Store
func (us *urlStore) Add(urlName, urlAddr string, params ...string) (string, error) {
return us.AddGr(urlName, "", urlAddr, params...)
}
// Adds a Url and panics if error
func (us urlStore) MustAdd(urlName, urlAddr string, params ...string) string {
addr, err := us.Add(urlName, urlAddr, params...)
if err != nil {
panic(err)
}
return addr
}
// Adds with group prefix
func (us *urlStore) AddGr(urlName, group, urlAddr string, params ...string) (string, error) {
if _, ok := us.store[urlName]; ok {
return "", errors.New("Url already exists. Try to use .Get() method.")
}
tmpUrl := url{group + urlAddr, params}
us.store[urlName] = tmpUrl
return urlAddr, nil
}
// Adds a Url with group prefix
func (us urlStore) MustAddGr(urlName, group, urlAddr string, params ...string) string {
addr, err := us.AddGr(urlName, group, urlAddr, params...)
if err != nil {
panic(err)
}
return addr
}
// Gets raw url string
func (us urlStore) Get(urlName string) string {
return us.store[urlName].url
}
// Gets reversed url
func (us urlStore) Reverse(urlName string, params ...string) (string, error) {
if len(params) != len(us.store[urlName].params) {
return "", errors.New("Bad Url Reverse: mismatch params for URL: "+ urlName)
}
res := us.store[urlName].url
for i, val := range params {
res = strings.Replace(res, us.store[urlName].params[i], val, 1)
}
return res, nil
}
// Gets reversed url and panics if error
func (us urlStore) MustReverse(urlName string, params ...string) string {
res, err := us.Reverse(urlName, params...)
if err != nil {
panic(err)
}
return res
}
// Reverse url, but returns empty string in case of error
func (us urlStore) Rev(urlName string, params ...string) string {
return us.MustReverse(urlName, params...)
}
func (us urlStore) Sting() string {
return fmt.Sprint(us.store)
}
// For testing
func (us urlStore) getParam(urlName string, num int) string {
return us.store[urlName].params[num]
}