/
struct_endpoints.go
127 lines (110 loc) · 2.52 KB
/
struct_endpoints.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
package api
import (
"bytes"
"errors"
"fmt"
"github.com/MickMake/GoUnify/Only"
"github.com/olekukonko/tablewriter"
"sort"
)
type TypeEndPoints map[EndPointName]EndPoint // Map of EndPoints by endpoint name.
func (ps TypeEndPoints) Exists(name EndPointName) error {
var err error
for range Only.Once {
if _, ok := ps[name]; !ok {
err = errors.New("unknown endpoint")
break
}
}
return err
}
func (ps TypeEndPoints) String() string {
var ret string
for range Only.Once {
buf := new(bytes.Buffer)
ep := ps.GetEnabled()
if len(ep) > 0 {
table := tablewriter.NewWriter(buf)
table.SetHeader([]string{"EndPoint", "Url"})
table.SetBorder(true)
for _, endpoint := range ep.SortEndPoints() {
u := ep[endpoint].GetUrl().String()
table.Append([]string{
string(ep[endpoint].GetName()),
u,
})
}
table.Render()
ret += fmt.Sprintf("# Implemented endpoints\n")
ret += buf.String()
}
dp := ps.GetDisabled()
if len(dp) > 0 {
buf = new(bytes.Buffer)
table := tablewriter.NewWriter(buf)
table.SetHeader([]string{"EndPoint", "Url"})
table.SetBorder(true)
for _, endpoint := range dp.SortEndPoints() {
u := dp[endpoint].GetUrl().String()
table.Append([]string{
string(dp[endpoint].GetName()),
u,
})
}
table.Render()
ret += fmt.Sprintf("# Non-implemented endpoints\n")
ret += buf.String()
}
ret += fmt.Sprintf("\n# Implemented: %d Non-implemented: %d\n\n", len(ep), len(dp))
}
return ret
}
func (ps *TypeEndPoints) SortEndPoints() []EndPointName {
ret := make([]EndPointName, 0, len(*ps))
for range Only.Once {
keys := make([]string, 0, len(*ps))
for k := range *ps {
keys = append(keys, string(k))
}
sort.Strings(keys)
for _, k := range keys {
ret = append(ret, EndPointName(k))
}
}
return ret
}
func (ps *TypeEndPoints) GetEnabled() TypeEndPoints {
ret := make(TypeEndPoints)
for range Only.Once {
s := ps.SortEndPoints()
if len(s) == 0 {
break
}
for _, endpoint := range s {
if (*ps)[endpoint].IsDisabled() {
continue
}
ret[(*ps)[endpoint].GetName()] = (*ps)[endpoint]
}
}
return ret
}
func (ps *TypeEndPoints) GetDisabled() TypeEndPoints {
ret := make(TypeEndPoints)
for range Only.Once {
s := ps.SortEndPoints()
if len(s) == 0 {
break
}
for _, endpoint := range s {
if (*ps)[endpoint].IsDisabled() {
ret[(*ps)[endpoint].GetName()] = (*ps)[endpoint]
}
}
}
return ret
}
func (ps *TypeEndPoints) GetEndPoint(name EndPointName) EndPoint {
ret, _ := (*ps)[name]
return ret
}