-
Notifications
You must be signed in to change notification settings - Fork 211
/
postproviderid.go
36 lines (31 loc) · 992 Bytes
/
postproviderid.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
package mapstructureutil
import (
"fmt"
"math"
"reflect"
"github.com/mitchellh/mapstructure"
"github.com/spacemeshos/go-spacemesh/activation"
)
// PostProviderIDDecodeFunc mapstructure decode func for activation.PostProviderID.
func PostProviderIDDecodeFunc() mapstructure.DecodeHookFunc {
return func(f, t reflect.Type, data any) (any, error) {
if t != reflect.TypeOf(activation.PostProviderID{}) {
return data, nil
}
switch f.Kind() {
case reflect.Float64:
value := data.(float64)
// TODO(mafa): update check from `value < -1` to `value < 0`, see https://github.com/spacemeshos/go-spacemesh/issues/4801
if value > math.MaxUint32 || value < -1 || value != math.Trunc(value) {
return nil, fmt.Errorf("invalid provider ID value: %v", value)
}
id := activation.PostProviderID{}
id.SetInt64(int64(value))
return id, nil
case reflect.String:
return nil, fmt.Errorf("invalid provider ID value: %v", data)
default:
return data, nil
}
}
}