forked from microservices-demo/user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.go
56 lines (45 loc) · 1.03 KB
/
links.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
package users
import (
"flag"
"fmt"
"os"
)
var (
domain string
entitymap = map[string]string{
"customer": "customers",
"address": "addresses",
"card": "cards",
}
)
func init() {
flag.StringVar(&domain, "link-domain", os.Getenv("HATEAOS"), "HATEAOS link domain")
}
type Links map[string]Href
func (l *Links) AddLink(ent string, id string) {
nl := make(Links)
link := fmt.Sprintf("http://%v/%v/%v", domain, entitymap[ent], id)
nl[ent] = Href{link}
nl["self"] = Href{link}
*l = nl
}
func (l *Links) AddAttrLink(attr string, corent string, id string) {
link := fmt.Sprintf("http://%v/%v/%v/%v", domain, entitymap[corent], id, entitymap[attr])
nl := *l
nl[entitymap[attr]] = Href{link}
*l = nl
}
func (l *Links) AddCustomer(id string) {
l.AddLink("customer", id)
l.AddAttrLink("address", "customer", id)
l.AddAttrLink("card", "customer", id)
}
func (l *Links) AddAddress(id string) {
l.AddLink("address", id)
}
func (l *Links) AddCard(id string) {
l.AddLink("card", id)
}
type Href struct {
string `json:"href"`
}