Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

utils.gno

1.95 Kb · 72 lines
 1package store
 2
 3import (
 4	bptree "gno.land/p/nt/bptree/v0"
 5	ufmt "gno.land/p/nt/ufmt/v0"
 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, 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, 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, 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, ufmt.Sprintf("cast %T to string", result))
47	}
48
49	return v, nil
50}
51
52// castToBPTree safely casts an any value to *bptree.BPTree
53// Returns ErrFailedCast if the value is not of type *bptree.BPTree
54func castToBPTree(result any) (*bptree.BPTree, error) {
55	tree, ok := result.(*bptree.BPTree)
56	if !ok {
57		return nil, ufmt.Errorf("%s: %s", ErrFailedCast, ufmt.Sprintf("cast %T to *bptree.BPTree", 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, ufmt.Sprintf("cast %T to address", result))
69	}
70
71	return v, nil
72}