-
Notifications
You must be signed in to change notification settings - Fork 4
/
Types.mo
151 lines (124 loc) · 3.63 KB
/
Types.mo
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Map "mo:map/Map";
module {
public type KeyValuePair = (Text, Candid);
/// A standard representation of the Candid type
public type Candid = {
#Int : Int;
#Int8 : Int8;
#Int16 : Int16;
#Int32 : Int32;
#Int64 : Int64;
#Nat : Nat;
#Nat8 : Nat8;
#Nat16 : Nat16;
#Nat32 : Nat32;
#Nat64 : Nat64;
#Bool : Bool;
#Float : Float;
#Text : Text;
#Blob : Blob;
#Null;
#Empty;
#Principal : Principal;
#Option : Candid;
#Array : [Candid];
#Record : [KeyValuePair];
#Tuple : [Candid]; // shorthand for record with indexed keys -> #Record([(0, Candid), (1, Candid), ...])
#Map : [KeyValuePair];
#Variant : KeyValuePair;
};
public type CandidType = {
#Int;
#Int8;
#Int16;
#Int32;
#Int64;
#Nat;
#Nat8;
#Nat16;
#Nat32;
#Nat64;
#Bool;
#Float;
#Text;
#Blob;
#Null;
#Empty;
#Principal;
#Option : CandidType;
#Array : CandidType;
#Record : [(Text, CandidType)];
#Tuple : [CandidType];
#Map : [(Text, CandidType)]; // ICRC3 version of #Record
#Variant : [(Text, CandidType)];
#Recursive : (Nat);
};
// nat values could be either reference pointers to compound types
// or actual primitive value codes
public type ShallowCandidTypes = {
#OptionRef : Nat;
#ArrayRef : Nat;
#RecordRef : [(Text, Nat)];
#VariantRef : [(Text, Nat)];
};
public let TypeCode = {
// primitive types
Null : Nat8 = 0x7f;
Bool : Nat8 = 0x7e;
Nat : Nat8 = 0x7d;
Int : Nat8 = 0x7c;
Nat8 : Nat8 = 0x7b;
Nat16 : Nat8 = 0x7a;
Nat32 : Nat8 = 0x79;
Nat64 : Nat8 = 0x78;
Int8 : Nat8 = 0x77;
Int16 : Nat8 = 0x76;
Int32 : Nat8 = 0x75;
Int64 : Nat8 = 0x74;
// Float32 : Nat8 = 0x73;
Float : Nat8 = 0x72;
Text : Nat8 = 0x71;
// Reserved : Nat8 = 0x70;
Empty : Nat8 = 0x6f;
Principal : Nat8 = 0x68;
// compound types
Option : Nat8 = 0x6e;
Array : Nat8 = 0x6d;
Record : Nat8 = 0x6c;
Variant : Nat8 = 0x6b;
// Func : Nat8 = 0x6a;
// Service : Nat8 = 0x69;
};
/// Encoding and Decoding options
public type Options = {
/// #### Encoding Options
/// Contains an array of tuples of the form (old_name, new_name) to rename the record keys.
renameKeys : [(Text, Text)];
// convertAllNumbersToFloats : Bool;
/// Returns #Map instead of #Record supported by the icrc3 spec
use_icrc_3_value_type : Bool;
/// encodes faster if the complete type is known, but not necessary
/// fails if types are incorrect
types : ?[CandidType];
/// #### Decoding Options
/// When decoding, you have the option to pass in the Candid variant type
/// and omit the type portion of the candid blob and only pass in the
/// serialized values
blob_contains_only_values : Bool;
};
public type ICRC3Value = {
#Blob : Blob;
#Text : Text;
#Nat : Nat;
#Int : Int;
#Array : [ICRC3Value];
#Map : [(Text, ICRC3Value)];
};
public let defaultOptions : Options = {
renameKeys = [];
// convertAllNumbersToFloats = false;
use_icrc_3_value_type = false;
types = null;
blob_contains_only_values = false;
};
};