-
Notifications
You must be signed in to change notification settings - Fork 9
/
result.go
146 lines (124 loc) · 2.7 KB
/
result.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
package toolkit
import (
"errors"
"fmt"
"time"
)
type ResultStatus string
const (
Status_OK ResultStatus = "OK"
Status_NOK ResultStatus = "NOK"
)
type Result struct {
Status ResultStatus
Message string
Duration time.Duration
DurationTxt string
Data interface{}
EncoderID string
time0 time.Time
}
func NewResult() *Result {
r := new(Result)
r.Status = Status_OK
r.time0 = time.Now()
return r
}
func (r *Result) IsEncoded() bool {
//fmt.Printf("Encoder ID: %s \n", r.EncoderID)
return r.EncoderID != ""
}
func (r *Result) SetData(o interface{}) *Result {
r.Data = o
r.SetDuration()
return r
}
func (r *Result) SetBytes(data interface{}, EncoderID string) *Result {
if EncoderID == "" {
EncoderID = "json"
}
r.EncoderID = EncoderID
r.Data = ToBytes(data, r.EncoderID)
//fmt.Println("Encoder ID now is " + r.EncoderID)
return r
}
func (r *Result) GetFromBytes(out interface{}) error {
if r.IsEncoded() == false {
return errors.New("Data is not encoded")
}
return FromBytes(r.Data.([]byte), r.EncoderID, out)
}
func (r *Result) SetDuration(){
r.Duration = time.Since(r.time0)
r.DurationTxt = r.Duration.String()
}
func (r *Result) SetError(e error) *Result {
r.Status = Status_NOK
r.Message = e.Error()
r.SetDuration()
return r
}
func (r *Result) SetErrorTxt(e string) *Result {
r.Status = Status_NOK
r.Message = e
r.SetDuration()
return r
}
func (r *Result) Error() error {
var e error
if r.Status == Status_NOK {
e = errors.New(r.Message)
}
return e
}
func (r *Result) Cast(out interface{}, method string) error {
if method == "" {
method = "json"
}
if r.Data == nil {
return errors.New("Data is nil")
}
if method == "json" {
bs := Jsonify(r.Data)
e := Unjson(bs, out)
if e != nil {
return errors.New("Can not decode data. " + e.Error())
}
return nil
}
return errors.New("Unable to cast due to unknown cast method")
}
func (a *Result) Run(f func(data interface{}) (interface{}, error), parm interface{}) *Result {
t0 := time.Now()
a.Status = Status_OK
a.Message = ""
if f == nil {
a.Data = nil
} else {
r, e := f(parm)
if e != nil {
a.Status = Status_NOK
a.Message = e.Error()
a.Data = nil
} else {
a.Data = r
}
}
a.Duration = time.Since(t0)
return a
}
func CallResult(url, calltype string, data []byte) (*Result, error) {
r, e := HttpCall(url, calltype, data, M{}.Set("expectedstatus", 200))
if e != nil {
return nil, fmt.Errorf(url + " Call eror: " + e.Error())
}
result := NewResult()
edecode := Unjson(HttpContent(r), &result)
if edecode != nil {
return nil, fmt.Errorf(url + " Decode error: " + edecode.Error())
}
if result.Status == Status_NOK {
return result, fmt.Errorf(result.Message)
}
return result, nil
}