-
Notifications
You must be signed in to change notification settings - Fork 16
/
epoch_to_time.go
79 lines (67 loc) · 2.31 KB
/
epoch_to_time.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
package codec
import (
"fmt"
"math/big"
"reflect"
"time"
"github.com/smartcontractkit/chainlink-common/pkg/types"
)
// NewEpochToTimeModifier converts all fields from time.Time off-chain to int64.
func NewEpochToTimeModifier(fields []string) Modifier {
fieldMap := map[string]bool{}
for _, field := range fields {
fieldMap[field] = true
}
m := &timeToUnixModifier{
modifierBase: modifierBase[bool]{
fields: fieldMap,
onToOffChainType: map[reflect.Type]reflect.Type{},
offToOnChainType: map[reflect.Type]reflect.Type{},
},
}
m.modifyFieldForInput = func(_ string, field *reflect.StructField, _ string, _ bool) error {
t, err := convertInt64InTypeToTime(field.Type, field.Name)
if err != nil {
return err
}
field.Type = t
return nil
}
return m
}
type timeToUnixModifier struct {
modifierBase[bool]
}
func (t *timeToUnixModifier) TransformToOnChain(offChainValue any, itemType string) (any, error) {
// since the hook will convert time.Time to epoch, we don't need to worry about converting them in the maps
return transformWithMaps(offChainValue, t.offToOnChainType, t.fields, noop, EpochToTimeHook, BigIntHook)
}
func (t *timeToUnixModifier) TransformToOffChain(onChainValue any, itemType string) (any, error) {
// since the hook will convert epoch to time.Time, we don't need to worry about converting them in the maps
return transformWithMaps(onChainValue, t.onToOffChainType, t.fields, noop, EpochToTimeHook, BigIntHook)
}
func noop(_ map[string]any, _ string, _ bool) error {
return nil
}
func convertInt64InTypeToTime(t reflect.Type, field string) (reflect.Type, error) {
converter := func(t reflect.Type) reflect.Type { return t }
for {
if t.ConvertibleTo(i64Type) {
return converter(reflect.TypeOf(&time.Time{})), nil
}
switch t.Kind() {
case reflect.Pointer:
if t.ConvertibleTo(reflect.TypeOf(&big.Int{})) {
return converter(reflect.TypeOf(&time.Time{})), nil
}
case reflect.Slice, reflect.Array:
tmp := converter
// works for array decoding too, [SliceToArrayVerifySizeHook]
// is used if the on-chain type requires array size checking
converter = func(t reflect.Type) reflect.Type { return reflect.SliceOf(tmp(t)) }
default:
return nil, fmt.Errorf("%w: cannot convert time for field %s", types.ErrInvalidType, field)
}
t = t.Elem()
}
}