forked from noebs/noebs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
218 lines (189 loc) · 5.48 KB
/
helpers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"github.com/adonese/noebs/ebs_fields"
ginprometheus "github.com/zsais/go-gin-prometheus"
)
type redisPurchaseFields map[string]interface{}
func structFields(s interface{}) (fields []map[string]interface{}) {
val := reflect.Indirect(reflect.ValueOf(s))
// val is a reflect.Type now
t := val.Type()
for i := 0; i <= t.NumField(); i++ {
f := t.Field(i)
name := f.Tag.Get("json")
tag := f.Tag.Get("binding")
sType := f.Type
if tag == "" {
tag = "optional"
}
field := map[string]interface{}{
"field": name,
"is_required": tag,
"type": sType,
}
fields = append(fields, field)
}
return fields
}
// endpointToFields the corresponding struct field for the endpoint string.
// we use simple string matching to capture the results
func endpointToFields(e string) interface{} {
if strings.Contains(e, "cashIn") {
return ebs_fields.CashInFields{}
}
if strings.Contains(e, "cashOut") {
return ebs_fields.CashOutFields{}
}
if strings.Contains(e, "balance") {
return ebs_fields.BalanceFields{}
}
if strings.Contains(e, "billPayment") {
return ebs_fields.BillPaymentFields{}
}
if strings.Contains(e, "cardTransfer") {
return ebs_fields.CardTransferFields{}
}
if strings.Contains(e, "changePin") {
return ebs_fields.ChangePINFields{}
}
if strings.Contains(e, "purchase") {
return ebs_fields.PurchaseFields{}
}
return ebs_fields.EBSResponse{}
}
// generateDoc generates API doc for this particular field
func generateDoc(e string) []map[string]interface{} {
fields := endpointToFields(e)
scheme := structFields(&fields)
return scheme
}
// getAllRoutes gets all routes for this particular engine
// perhaps, it could better be rewritten to explicitly show that
func getAllRoutes() []map[string]string {
e := GetMainEngine()
endpoints := e.Routes()
var allRoutes []map[string]string
for _, r := range endpoints {
name := strings.TrimPrefix(r.Path, "/")
mapping := map[string]string{
"http_method": r.Method,
"path": name,
}
allRoutes = append(allRoutes, mapping)
}
return allRoutes
}
var response = ebs_fields.EBSResponse{
ResponseMessage: "Successful",
ResponseStatus: "Successful",
ResponseCode: 0,
ReferenceNumber: "094930",
ApprovalCode: "0032",
TranDateTime: "190613085100",
TerminalID: "19000019",
SystemTraceAuditNumber: 0,
ClientID: "ACTS",
PAN: "92220817",
}
func MockEBSServer() *httptest.Server {
f := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
// write the Generic ResponseBody onto the response writer
b, err := json.Marshal(&response)
if err != nil {
logrusLogger.Errorf("theres an error")
}
w.Write(b)
}
return httptest.NewServer(http.HandlerFunc(f))
}
func Metrics() []*ginprometheus.Metric {
metrics := []*ginprometheus.Metric{
{
ID: "1234", // optional string
Name: "test_metric", // required string
Description: "Counter test metric", // required string
Type: "counter", // required string
},
{
ID: "1235", // Identifier
Name: "test_metric_2", // Metric Name
Description: "Summary test metric", // Help Description
Type: "summary", // type associated with prometheus collector
},
{
ID: "1235", // Identifier
Name: "test_metric_3", // Metric Name
Description: "Summary test metric", // Help Description
Type: "counter_vec", // type associated with prometheus collector
},
{
ID: "1236", // Identifier
Name: "test_metric_4", // Metric Name
Description: "Summary test metric", // Help Description
Type: "histogram_vec", // type associated with prometheus collector
},
// Type Options:
// counter, counter_vec, gauge, gauge_vec,
// histogram, histogram_vec, summary, summary_vec
}
return metrics
}
func additionalFieldsToHash(a string) (map[string]string, error) {
fields := strings.Split(a, ";")
if len(fields) < 2 {
return nil, errors.New("index out of range")
}
out := make(map[string]string)
for _, v := range fields {
f := strings.Split(v, "=")
out[f[0]] = f[1]
}
return out, nil
}
type test struct {
AdditionalData string
PayeeID string
}
type mtnBill struct {
PaidAmount float64 `json:"PaidAmount"`
SubNewBalance float64 `json:"SubNewBalance"`
}
func (m *mtnBill) MarshalBinary() (data []byte, err error) {
d, err := json.Marshal(m)
return d, err
}
func (m *mtnBill) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, m)
}
func (m *mtnBill) NewFromMap(f map[string]string) {
m.PaidAmount, _ = strconv.ParseFloat(f["PaidAmount"], 32)
m.SubNewBalance, _ = strconv.ParseFloat(f["SubNewBalance"], 32)
}
type sudaniBill struct {
Status string `json:"Status"`
}
func (s *sudaniBill) MarshalBinary() (data []byte, err error) {
d, err := json.Marshal(s)
return d, err
}
func (s *sudaniBill) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, s)
}
func (s *sudaniBill) NewFromMap(f map[string]string) {
s.Status = f["Status"]
}
func generateFields() *ebs_fields.EBSResponse {
f := &ebs_fields.EBSResponse{}
f.AdditionalData = "SalesAmount=10.3;FixedFee=22.3;Token=23232;MeterNumber=12345;CustomerName=mohamed"
f.PayeeID = "0010020001"
return f
}