/
forward.go
111 lines (98 loc) · 3.02 KB
/
forward.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
package types
import (
"encoding/json"
"errors"
"fmt"
"time"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/iancoleman/orderedmap"
)
type PacketMetadata struct {
Forward *ForwardMetadata `json:"forward"`
}
type ForwardMetadata struct {
Port string `json:"port,omitempty"`
Channel string `json:"channel,omitempty"`
Timeout Duration `json:"timeout,omitempty"`
Retries *uint8 `json:"retries,omitempty"`
// Using JSONObject so that objects for next property will not be mutated by golang's lexicographic key sort on map keys during Marshal.
// Supports primitives for Unmarshal/Marshal so that an escaped JSON-marshaled string is also valid.
Next *JSONObject `json:"next,omitempty"`
}
type Duration time.Duration
func (m *ForwardMetadata) Validate() error {
if err := host.PortIdentifierValidator(m.Port); err != nil {
return fmt.Errorf("failed to validate metadata: %w", err)
}
if err := host.ChannelIdentifierValidator(m.Channel); err != nil {
return fmt.Errorf("failed to validate metadata: %w", err)
}
return nil
}
// JSONObject is a wrapper type to allow either a primitive type or a JSON object.
// In the case the value is a JSON object, OrderedMap type is used so that key order
// is retained across Unmarshal/Marshal.
type JSONObject struct {
obj bool
primitive []byte
orderedMap orderedmap.OrderedMap
}
// NewJSONObject is a constructor used for tests.
// The usage of JSONObject in the middleware is only json Marshal/Unmarshal
func NewJSONObject(object bool, primitive []byte, orderedMap orderedmap.OrderedMap) *JSONObject {
return &JSONObject{
obj: object,
primitive: primitive,
orderedMap: orderedMap,
}
}
// UnmarshalJSON overrides the default json.Unmarshal behavior
func (o *JSONObject) UnmarshalJSON(b []byte) error {
if err := o.orderedMap.UnmarshalJSON(b); err != nil {
// If ordered map unmarshal fails, this is a primitive value
o.obj = false
// Attempt to unmarshal as string, this removes extra JSON escaping
var primitiveStr string
if err := json.Unmarshal(b, &primitiveStr); err != nil {
o.primitive = b
return nil
}
o.primitive = []byte(primitiveStr)
return nil
}
// This is a JSON object, now stored as an ordered map to retain key order.
o.obj = true
return nil
}
// MarshalJSON overrides the default json.Marshal behavior
func (o JSONObject) MarshalJSON() ([]byte, error) {
if o.obj {
// non-primitive, return marshaled ordered map.
return o.orderedMap.MarshalJSON()
}
// primitive, return raw bytes.
return o.primitive, nil
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).Nanoseconds())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*d = Duration(time.Duration(value))
return nil
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(tmp)
return nil
default:
return errors.New("invalid duration")
}
}