Skip to content

Commit ebae915

Browse files
committed
Added support for handling Int8 scalar as i64
1 parent 86e1fda commit ebae915

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

.changeset/twelve-tables-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-ts': minor
3+
---
4+
5+
Added support for handling Int8 scalar as i64

packages/ts/common/numbers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export declare namespace bigDecimal {
2929
function fromString(s: string): BigDecimal;
3030
}
3131

32+
export type Int8 = i64;
33+
3234
/** An Ethereum address (20 bytes). */
3335
export class Address extends Bytes {
3436
static fromString(s: string): Address {

packages/ts/common/value.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export enum ValueKind {
1414
NULL = 5,
1515
BYTES = 6,
1616
BIGINT = 7,
17+
INT8 = 8,
1718
}
1819

1920
const VALUE_KIND_NAMES = [
@@ -25,6 +26,7 @@ const VALUE_KIND_NAMES = [
2526
'null',
2627
'Bytes',
2728
'BigInt',
29+
'Int8',
2830
];
2931

3032
/**
@@ -66,6 +68,14 @@ export class Value {
6668
return this.data as i32;
6769
}
6870

71+
toI64(): i64 {
72+
if (this.kind == ValueKind.NULL) {
73+
return 0;
74+
}
75+
assert(this.kind == ValueKind.INT8, 'Value is not an i64.');
76+
return this.data as i64;
77+
}
78+
6979
toString(): string {
7080
assert(this.kind == ValueKind.STRING, 'Value is not a string.');
7181
return changetype<string>(this.data as u32);
@@ -131,6 +141,15 @@ export class Value {
131141
return output;
132142
}
133143

144+
toI64Array(): Array<i64> {
145+
const values = this.toArray();
146+
const output = new Array<i64>(values.length);
147+
for (let i: i32 = 0; i < values.length; i++) {
148+
output[i] = values[i].toI32();
149+
}
150+
return output;
151+
}
152+
134153
toBigIntArray(): Array<BigInt> {
135154
const values = this.toArray();
136155
const output = new Array<BigInt>(values.length);
@@ -209,6 +228,19 @@ export class Value {
209228
return out;
210229
}
211230

231+
toI64Matrix(): Array<Array<i64>> {
232+
const valueMatrix = this.toMatrix();
233+
const out = new Array<Array<i64>>(valueMatrix.length);
234+
235+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
236+
out[i] = new Array<i64>(valueMatrix[i].length);
237+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
238+
out[i][j] = valueMatrix[i][j].toI64();
239+
}
240+
}
241+
return out;
242+
}
243+
212244
toBigIntMatrix(): Array<Array<BigInt>> {
213245
const valueMatrix = this.toMatrix();
214246
const out = new Array<Array<BigInt>>(valueMatrix.length);
@@ -238,6 +270,8 @@ export class Value {
238270
return this.toString();
239271
case ValueKind.INT:
240272
return this.toI32().toString();
273+
case ValueKind.INT8:
274+
return this.toI64().toString();
241275
case ValueKind.BIGDECIMAL:
242276
return this.toBigDecimal().toString();
243277
case ValueKind.BOOL:
@@ -338,6 +372,10 @@ export class Value {
338372
return new Value(ValueKind.INT, n as u64);
339373
}
340374

375+
static fromI64(n: i64): Value {
376+
return new Value(ValueKind.INT8, n as i64);
377+
}
378+
341379
static fromString(s: string): Value {
342380
return new Value(ValueKind.STRING, changetype<u32>(s));
343381
}
@@ -413,6 +451,17 @@ export class Value {
413451
return Value.fromMatrix(out);
414452
}
415453

454+
static fromI64Matrix(values: Array<Array<i64>>): Value {
455+
const out = new Array<Array<Value>>(values.length);
456+
for (let i: i32 = 0; i < values.length; i++) {
457+
out[i] = new Array<Value>(values[i].length);
458+
for (let j: i32 = 0; j < values[i].length; j++) {
459+
out[i][j] = Value.fromI64(values[i][j]);
460+
}
461+
}
462+
return Value.fromMatrix(out);
463+
}
464+
416465
static fromBigIntMatrix(values: Array<Array<BigInt>>): Value {
417466
const out = new Array<Array<Value>>(values.length);
418467
for (let i: i32 = 0; i < values.length; i++) {

0 commit comments

Comments
 (0)