-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse.go
145 lines (122 loc) · 3.21 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
141
142
143
144
145
// Copyright © 2015 Alexey Khalyapin - [email protected]
//
// This program or package and any associated files are licensed under the
// GNU GENERAL PUBLIC LICENSE Version 2 (the "License"); you may not use these files
// except in compliance with the License. You can get a copy of the License
// at: http://www.gnu.org/licenses/gpl-2.0.html
package reverse
import (
"errors"
"fmt"
"strings"
)
var Urls *URLStore
func init() {
Urls = NewURLStore()
}
// Adds url to store
func Add(urlName, urlAddr string, params ...string) string {
return Urls.MustAdd(urlName, 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
}
// create a new URLStore
func NewURLStore() *URLStore {
return &URLStore{store: make(map[string]URL)}
}
// Adds a Url to the Store
func (us *URLStore) Add(urlName, 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{urlAddr, params}
us.store[urlName] = tmpUrl
return urlAddr, nil
}
// 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
}
// Append a URLStore with prefix
func (us *URLStore) Append(prefix string, group *URLStore) error {
for name, url := range group.store {
_, err := us.Add(name, prefix+url.url, url.params...)
if err != nil {
return err
}
}
return nil
}
// Append a URLStore with prefix
func (us *URLStore) MustAppend(prefix string, store *URLStore) {
err := us.Append(prefix, store)
if err != nil {
panic(err)
}
}
// 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
}
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]
}