-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathmain.go
55 lines (48 loc) · 1.2 KB
/
main.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
package federation
import (
"encoding/json"
"strconv"
)
// NameResponse represents the result of a federation request
// for `name` and `forward` requests.
type NameResponse struct {
AccountID string `json:"account_id"`
MemoType string `json:"memo_type,omitempty"`
Memo Memo `json:"memo,omitempty"`
}
// IDResponse represents the result of a federation request
// for `id` request.
type IDResponse struct {
Address string `json:"stellar_address"`
}
// Memo value can be either integer or string in JSON. This struct
// allows marshaling and unmarshaling both types.
type Memo struct {
Value string
}
func (m Memo) MarshalJSON() ([]byte, error) {
// Memo after marshaling should always be a string
value, err := json.Marshal(m.Value)
if err != nil {
return []byte{}, err
}
return value, nil
}
func (m *Memo) UnmarshalJSON(value []byte) error {
// Try to unmarshal value into uint64. If that fails
// unmarshal into string.
var uintValue uint64
err := json.Unmarshal(value, &uintValue)
if err == nil {
m.Value = strconv.FormatUint(uintValue, 10)
return nil
}
err = json.Unmarshal(value, &m.Value)
if err != nil {
return err
}
return nil
}
func (m *Memo) String() string {
return m.Value
}