forked from danikarik/ncanode-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathncanode.go
48 lines (39 loc) · 1002 Bytes
/
ncanode.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
package ncanode
import (
"encoding/json"
"fmt"
"strings"
"time"
)
type apiRequest struct {
Version string `json:"version"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
type apiResponse struct {
Status int `json:"status"`
Message string `json:"message"`
}
func (r apiResponse) Error() string {
return fmt.Sprintf("ncanode: request failed with status %d: %s", r.Status, r.Message)
}
// Time is a small wrapper of std time.
// Difference is time layout used by json decoding.
type Time struct{ time.Time }
// UnmarshalJSON implements custom unmarshaling of json decoder.
func (t *Time) UnmarshalJSON(data []byte) error {
v := strings.Trim(string(data), "\"")
if v == "null" {
return nil
}
tt, err := time.Parse("2006-01-02 15:04:05", v)
if err != nil {
return err
}
*t = Time{tt}
return nil
}
// MarshalJSON implements custom marshaling of json encoder.
func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t)
}