utils.gno
1.95 Kb ยท 72 lines
1package store
2
3import (
4 "gno.land/p/nt/avl"
5 "gno.land/p/nt/ufmt"
6)
7
8// castToInt64 safely casts an any value to int64
9// Returns ErrFailedCast if the value is not of type int64
10func castToInt64(result any) (int64, error) {
11 v, ok := result.(int64)
12 if !ok {
13 return 0, ufmt.Errorf("%s: %s", ErrFailedCast.Error(), ufmt.Sprintf("cast %T to int64", result))
14 }
15
16 return v, nil
17}
18
19// castToUint64 safely casts an any value to uint64
20// Returns ErrFailedCast if the value is not of type uint64
21func castToUint64(result any) (uint64, error) {
22 v, ok := result.(uint64)
23 if !ok {
24 return 0, ufmt.Errorf("%s: %s", ErrFailedCast.Error(), ufmt.Sprintf("cast %T to uint64", result))
25 }
26
27 return v, nil
28}
29
30// castToBool safely casts an any value to bool
31// Returns ErrFailedCast if the value is not of type bool
32func castToBool(result any) (bool, error) {
33 v, ok := result.(bool)
34 if !ok {
35 return false, ufmt.Errorf("%s: %s", ErrFailedCast.Error(), ufmt.Sprintf("cast %T to bool", result))
36 }
37
38 return v, nil
39}
40
41// castToString safely casts an any value to string
42// Returns ErrFailedCast if the value is not of type string
43func castToString(result any) (string, error) {
44 v, ok := result.(string)
45 if !ok {
46 return "", ufmt.Errorf("%s: %s", ErrFailedCast.Error(), ufmt.Sprintf("cast %T to string", result))
47 }
48
49 return v, nil
50}
51
52// castToTree safely casts an any value to *avl.Tree
53// Returns ErrFailedCast if the value is not of type *avl.Tree
54func castToTree(result any) (*avl.Tree, error) {
55 tree, ok := result.(*avl.Tree)
56 if !ok {
57 return nil, ufmt.Errorf("%s: %s", ErrFailedCast.Error(), ufmt.Sprintf("cast %T to *avl.Tree", result))
58 }
59
60 return tree, nil
61}
62
63// castToAddress safely casts an any value to address
64// Returns ErrFailedCast if the value is not of type address
65func castToAddress(result any) (address, error) {
66 v, ok := result.(address)
67 if !ok {
68 return address(""), ufmt.Errorf("%s: %s", ErrFailedCast.Error(), ufmt.Sprintf("cast %T to address", result))
69 }
70
71 return v, nil
72}