-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.ts
107 lines (94 loc) · 2.19 KB
/
utils.ts
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
import {
BigInt,
ipfs,
json,
JSONValue,
JSONValueKind,
log,
TypedMap,
} from "@graphprotocol/graph-ts"
export function JSONValueToString(value: JSONValue | null): string | null {
if (value == null || value.isNull()) {
return null
}
switch (value.kind) {
case JSONValueKind.BOOL:
return value.toBool().toString()
case JSONValueKind.STRING:
return value.toString()
case JSONValueKind.NUMBER:
return value.toBigInt().toString()
default:
return null
}
}
export function JSONValueToBool(value: JSONValue | null): boolean {
if (value == null || value.isNull()) {
return false
}
switch (value.kind) {
case JSONValueKind.BOOL:
return value.toBool()
case JSONValueKind.STRING:
if (value.toString() === "true") {
return true
} else {
return false
}
default:
return false
}
}
export function JSONValueToBigInt(value: JSONValue | null): BigInt | null {
if (value == null || value.isNull()) {
return null
}
switch (value.kind) {
case JSONValueKind.STRING:
return BigInt.fromString(value.toString())
case JSONValueKind.NUMBER:
return value.toBigInt()
default:
return null
}
}
export function JSONValueToObject(
value: JSONValue | null
): TypedMap<string, JSONValue> | null {
if (value == null || value.isNull()) {
return null
}
switch (value.kind) {
case JSONValueKind.OBJECT:
return value.toObject()
default:
return null
}
}
export function JSONValueToArray(
value: JSONValue | null
): JSONValue[] | null {
if (value == null || value.isNull()) {
return null
}
switch (value.kind) {
case JSONValueKind.ARRAY:
return value.toArray()
default:
return null
}
}
export function ipfsToJsonValueOrNull(uri: string): JSONValue | null {
let jsonBytes = ipfs.cat(uri)
// ipfsUri could be malformatted or file non-available.
if (!jsonBytes) {
log.warning("Failed to fetch JSON from uri {}", [uri])
return null
}
let result = json.try_fromBytes(jsonBytes)
if (result.isError) {
log.warning("IPFS file is not a json, from uri {}", [uri])
return null
}
return result.value
}