@graphprotocol/graph-cli 0.37.1 → 0.37.2-alpha-20221219142030-0cf2a37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -2
- package/src/codegen/schema.js +49 -58
- package/src/codegen/schema.test.js +32 -27
- package/src/codegen/template.js +1 -2
- package/src/codegen/types/conversions.js +43 -14
- package/src/codegen/types/index.js +20 -23
- package/src/codegen/typescript.js +0 -3
- package/src/command-helpers/abi.js +2 -3
- package/src/command-helpers/data-sources.js +3 -4
- package/src/command-helpers/scaffold.js +18 -14
- package/src/command-helpers/spinner.js +0 -1
- package/src/commands/add.js +42 -25
- package/src/compiler/index.js +12 -13
- package/src/protocols/arweave/subgraph.js +2 -7
- package/src/protocols/cosmos/subgraph.js +2 -9
- package/src/protocols/ethereum/abi.js +6 -7
- package/src/protocols/ethereum/codegen/abi.js +217 -223
- package/src/protocols/ethereum/codegen/abi.test.js +15 -16
- package/src/protocols/ethereum/subgraph.js +55 -67
- package/src/protocols/ethereum/type-generator.js +16 -25
- package/src/protocols/index.js +5 -6
- package/src/protocols/near/subgraph.js +3 -5
- package/src/protocols/substreams/subgraph.js +2 -4
- package/src/scaffold/cosmos.test.js +0 -1
- package/src/scaffold/ethereum.test.js +2 -3
- package/src/scaffold/near.test.js +0 -1
- package/src/schema.js +1 -2
- package/src/subgraph.js +44 -53
- package/src/type-generator.js +6 -7
- package/src/validation/contract.js +6 -11
- package/src/validation/manifest.js +77 -84
- package/src/validation/schema.js +284 -294
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @graphprotocol/graph-cli
|
|
2
2
|
|
|
3
|
+
## 0.37.2-alpha-20221219142030-0cf2a37
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1021](https://github.com/graphprotocol/graph-cli/pull/1021) [`bb2a64f`](https://github.com/graphprotocol/graph-cli/commit/bb2a64f89ca704c2f6bc46bb9f3d53a9581bf765) Thanks [@saihaj](https://github.com/saihaj)! - dependencies updates:
|
|
8
|
+
- Removed dependency [`immutable@3.8.2` ↗︎](https://www.npmjs.com/package/immutable/v/3.8.2) (from `dependencies`)
|
|
9
|
+
|
|
3
10
|
## 0.37.1
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphprotocol/graph-cli",
|
|
3
|
-
"version": "0.37.
|
|
3
|
+
"version": "0.37.2-alpha-20221219142030-0cf2a37",
|
|
4
4
|
"license": "(Apache-2.0 OR MIT)",
|
|
5
5
|
"description": "CLI for building for and deploying to The Graph",
|
|
6
6
|
"dependencies": {
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"glob": "7.1.6",
|
|
17
17
|
"gluegun": "https://github.com/edgeandnode/gluegun#v4.3.1-pin-colors-dep",
|
|
18
18
|
"graphql": "15.5.0",
|
|
19
|
-
"immutable": "3.8.2",
|
|
20
19
|
"ipfs-http-client": "34.0.0",
|
|
21
20
|
"jayson": "3.6.6",
|
|
22
21
|
"js-yaml": "3.13.1",
|
package/src/codegen/schema.js
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
const immutable = require('immutable')
|
|
2
|
-
|
|
3
1
|
const tsCodegen = require('./typescript')
|
|
4
2
|
const typesCodegen = require('./types')
|
|
5
3
|
|
|
6
|
-
const List = immutable.List
|
|
7
|
-
|
|
8
4
|
class IdField {
|
|
9
|
-
static BYTES = Symbol(
|
|
10
|
-
static STRING = Symbol(
|
|
5
|
+
static BYTES = Symbol('Bytes')
|
|
6
|
+
static STRING = Symbol('String')
|
|
11
7
|
|
|
12
8
|
constructor(idField) {
|
|
13
|
-
const typeName = idField.
|
|
14
|
-
this.kind = typeName ===
|
|
9
|
+
const typeName = idField.type?.type?.name?.value
|
|
10
|
+
this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING
|
|
15
11
|
}
|
|
16
12
|
|
|
17
13
|
typeName() {
|
|
18
|
-
return this.kind === IdField.BYTES ?
|
|
14
|
+
return this.kind === IdField.BYTES ? 'Bytes' : 'string'
|
|
19
15
|
}
|
|
20
16
|
|
|
21
17
|
gqlTypeName() {
|
|
22
|
-
return this.kind === IdField.BYTES ?
|
|
18
|
+
return this.kind === IdField.BYTES ? 'Bytes' : 'String'
|
|
23
19
|
}
|
|
24
20
|
|
|
25
21
|
tsNamedType() {
|
|
@@ -27,28 +23,28 @@ class IdField {
|
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
tsValueFrom() {
|
|
30
|
-
return this.kind === IdField.BYTES ?
|
|
26
|
+
return this.kind === IdField.BYTES ? 'Value.fromBytes(id)' : 'Value.fromString(id)'
|
|
31
27
|
}
|
|
32
28
|
|
|
33
29
|
tsValueKind() {
|
|
34
|
-
return this.kind === IdField.BYTES ?
|
|
30
|
+
return this.kind === IdField.BYTES ? 'ValueKind.BYTES' : 'ValueKind.STRING'
|
|
35
31
|
}
|
|
36
32
|
|
|
37
33
|
tsValueToString() {
|
|
38
|
-
return this.kind == IdField.BYTES ?
|
|
34
|
+
return this.kind == IdField.BYTES ? 'id.toBytes().toHexString()' : 'id.toString()'
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
tsToString() {
|
|
42
|
-
return this.kind == IdField.BYTES ?
|
|
38
|
+
return this.kind == IdField.BYTES ? 'id.toHexString()' : 'id'
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
static fromFields(fields) {
|
|
46
|
-
const idField = fields.find(field => field.
|
|
42
|
+
const idField = fields.find(field => field.name?.value === 'id')
|
|
47
43
|
return new IdField(idField)
|
|
48
44
|
}
|
|
49
45
|
|
|
50
46
|
static fromTypeDef(def) {
|
|
51
|
-
return IdField.fromFields(def.
|
|
47
|
+
return IdField.fromFields(def.fields)
|
|
52
48
|
}
|
|
53
49
|
}
|
|
54
50
|
|
|
@@ -81,29 +77,26 @@ module.exports = class SchemaCodeGenerator {
|
|
|
81
77
|
}
|
|
82
78
|
|
|
83
79
|
generateTypes() {
|
|
84
|
-
return this.schema.ast
|
|
85
|
-
.get('definitions')
|
|
80
|
+
return this.schema.ast.definitions
|
|
86
81
|
.filter(def => this._isEntityTypeDefinition(def))
|
|
87
82
|
.map(def => this._generateEntityType(def))
|
|
88
83
|
}
|
|
89
84
|
|
|
90
85
|
_isEntityTypeDefinition(def) {
|
|
91
86
|
return (
|
|
92
|
-
def.
|
|
93
|
-
def
|
|
94
|
-
.get('directives')
|
|
95
|
-
.find(directive => directive.getIn(['name', 'value']) === 'entity') !== undefined
|
|
87
|
+
def.kind === 'ObjectTypeDefinition' &&
|
|
88
|
+
def.directives.find(directive => directive.name?.value === 'entity') !== undefined
|
|
96
89
|
)
|
|
97
90
|
}
|
|
98
91
|
|
|
99
92
|
_isInterfaceDefinition(def) {
|
|
100
|
-
return def.
|
|
93
|
+
return def.kind === 'InterfaceTypeDefinition'
|
|
101
94
|
}
|
|
102
95
|
|
|
103
96
|
_generateEntityType(def) {
|
|
104
|
-
let name = def.
|
|
97
|
+
let name = def.name?.value
|
|
105
98
|
let klass = tsCodegen.klass(name, { export: true, extends: 'Entity' })
|
|
106
|
-
const fields = def.
|
|
99
|
+
const fields = def.fields
|
|
107
100
|
const idField = IdField.fromFields(fields)
|
|
108
101
|
|
|
109
102
|
// Generate and add a constructor
|
|
@@ -113,11 +106,10 @@ module.exports = class SchemaCodeGenerator {
|
|
|
113
106
|
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method))
|
|
114
107
|
|
|
115
108
|
// Generate and add entity field getters and setters
|
|
116
|
-
def
|
|
117
|
-
.get('fields')
|
|
109
|
+
def.fields
|
|
118
110
|
.reduce(
|
|
119
111
|
(methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)),
|
|
120
|
-
|
|
112
|
+
[],
|
|
121
113
|
)
|
|
122
114
|
.forEach(method => klass.addMethod(method))
|
|
123
115
|
|
|
@@ -138,7 +130,7 @@ module.exports = class SchemaCodeGenerator {
|
|
|
138
130
|
}
|
|
139
131
|
|
|
140
132
|
_generateStoreMethods(entityName, idField) {
|
|
141
|
-
return
|
|
133
|
+
return [
|
|
142
134
|
tsCodegen.method(
|
|
143
135
|
'save',
|
|
144
136
|
[],
|
|
@@ -162,19 +154,19 @@ module.exports = class SchemaCodeGenerator {
|
|
|
162
154
|
return changetype<${entityName} | null>(store.get('${entityName}', ${idField.tsToString()}))
|
|
163
155
|
`,
|
|
164
156
|
),
|
|
165
|
-
|
|
157
|
+
]
|
|
166
158
|
}
|
|
167
159
|
|
|
168
160
|
_generateEntityFieldMethods(entityDef, fieldDef) {
|
|
169
|
-
return
|
|
161
|
+
return [
|
|
170
162
|
this._generateEntityFieldGetter(entityDef, fieldDef),
|
|
171
163
|
this._generateEntityFieldSetter(entityDef, fieldDef),
|
|
172
|
-
]
|
|
164
|
+
]
|
|
173
165
|
}
|
|
174
166
|
|
|
175
167
|
_generateEntityFieldGetter(entityDef, fieldDef) {
|
|
176
|
-
let name = fieldDef.
|
|
177
|
-
let gqlType = fieldDef.
|
|
168
|
+
let name = fieldDef.name?.value
|
|
169
|
+
let gqlType = fieldDef.type
|
|
178
170
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
|
|
179
171
|
let returnType = this._typeFromGraphQl(gqlType)
|
|
180
172
|
let isNullable = returnType instanceof tsCodegen.NullableType
|
|
@@ -198,25 +190,21 @@ module.exports = class SchemaCodeGenerator {
|
|
|
198
190
|
}
|
|
199
191
|
|
|
200
192
|
_generateEntityFieldSetter(entityDef, fieldDef) {
|
|
201
|
-
let name = fieldDef.
|
|
202
|
-
let gqlType = fieldDef.
|
|
193
|
+
let name = fieldDef.name?.value
|
|
194
|
+
let gqlType = fieldDef.type
|
|
203
195
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
|
|
204
196
|
let paramType = this._typeFromGraphQl(gqlType)
|
|
205
197
|
let isNullable = paramType instanceof tsCodegen.NullableType
|
|
206
198
|
let paramTypeString = isNullable ? paramType.inner.toString() : paramType.toString()
|
|
207
199
|
let isArray = paramType instanceof tsCodegen.ArrayType
|
|
208
200
|
|
|
209
|
-
if (
|
|
210
|
-
isArray &&
|
|
211
|
-
paramType.inner instanceof tsCodegen.NullableType
|
|
212
|
-
) {
|
|
201
|
+
if (isArray && paramType.inner instanceof tsCodegen.NullableType) {
|
|
213
202
|
let baseType = this._baseType(gqlType)
|
|
214
203
|
|
|
215
204
|
throw new Error(`
|
|
216
205
|
GraphQL schema can't have List's with Nullable members.
|
|
217
206
|
Error in '${name}' field of type '[${baseType}]'.
|
|
218
|
-
Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`
|
|
219
|
-
)
|
|
207
|
+
Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`)
|
|
220
208
|
}
|
|
221
209
|
|
|
222
210
|
let setNonNullable = `
|
|
@@ -242,12 +230,15 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
242
230
|
}
|
|
243
231
|
|
|
244
232
|
_resolveFieldType(gqlType) {
|
|
245
|
-
let typeName = gqlType.
|
|
233
|
+
let typeName = gqlType.name?.value
|
|
246
234
|
|
|
247
235
|
// If this is a reference to another type, the field has the type of
|
|
248
236
|
// the referred type's id field
|
|
249
|
-
const typeDef = this.schema.ast.
|
|
250
|
-
|
|
237
|
+
const typeDef = this.schema.ast.definitions.find(
|
|
238
|
+
def =>
|
|
239
|
+
(this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
|
|
240
|
+
def.name?.value === typeName,
|
|
241
|
+
)
|
|
251
242
|
if (typeDef) {
|
|
252
243
|
return IdField.fromTypeDef(typeDef).typeName()
|
|
253
244
|
} else {
|
|
@@ -260,10 +251,10 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
260
251
|
* other entity types, this is the same as the type of the id of the
|
|
261
252
|
* referred type, i.e., `string` or `Bytes`*/
|
|
262
253
|
_valueTypeFromGraphQl(gqlType) {
|
|
263
|
-
if (gqlType.
|
|
264
|
-
return this._valueTypeFromGraphQl(gqlType.
|
|
265
|
-
} else if (gqlType.
|
|
266
|
-
return '[' + this._valueTypeFromGraphQl(gqlType.
|
|
254
|
+
if (gqlType.kind === 'NonNullType') {
|
|
255
|
+
return this._valueTypeFromGraphQl(gqlType.type, false)
|
|
256
|
+
} else if (gqlType.kind === 'ListType') {
|
|
257
|
+
return '[' + this._valueTypeFromGraphQl(gqlType.type) + ']'
|
|
267
258
|
} else {
|
|
268
259
|
return this._resolveFieldType(gqlType)
|
|
269
260
|
}
|
|
@@ -272,20 +263,20 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
272
263
|
/** Determine the base type of `gqlType` by removing any non-null
|
|
273
264
|
* constraints and using the type of elements of lists */
|
|
274
265
|
_baseType(gqlType) {
|
|
275
|
-
if (gqlType.
|
|
276
|
-
return this._baseType(gqlType.
|
|
277
|
-
} else if (gqlType.
|
|
278
|
-
return this._baseType(gqlType.
|
|
266
|
+
if (gqlType.kind === 'NonNullType') {
|
|
267
|
+
return this._baseType(gqlType.type)
|
|
268
|
+
} else if (gqlType.kind === 'ListType') {
|
|
269
|
+
return this._baseType(gqlType.type)
|
|
279
270
|
} else {
|
|
280
|
-
return gqlType.
|
|
271
|
+
return gqlType.name?.value
|
|
281
272
|
}
|
|
282
273
|
}
|
|
283
274
|
|
|
284
275
|
_typeFromGraphQl(gqlType, nullable = true) {
|
|
285
|
-
if (gqlType.
|
|
286
|
-
return this._typeFromGraphQl(gqlType.
|
|
287
|
-
} else if (gqlType.
|
|
288
|
-
let type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.
|
|
276
|
+
if (gqlType.kind === 'NonNullType') {
|
|
277
|
+
return this._typeFromGraphQl(gqlType.type, false)
|
|
278
|
+
} else if (gqlType.kind === 'ListType') {
|
|
279
|
+
let type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.type))
|
|
289
280
|
return nullable ? tsCodegen.nullableType(type) : type
|
|
290
281
|
} else {
|
|
291
282
|
// NamedType
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const prettier = require('prettier')
|
|
2
2
|
const graphql = require('graphql/language')
|
|
3
|
-
const immutable = require('immutable')
|
|
4
3
|
const SchemaCodeGenerator = require('./schema')
|
|
5
4
|
const {
|
|
6
5
|
Class,
|
|
@@ -12,15 +11,11 @@ const {
|
|
|
12
11
|
ArrayType,
|
|
13
12
|
} = require('./typescript')
|
|
14
13
|
|
|
15
|
-
const formatTS = code =>
|
|
16
|
-
prettier.format(
|
|
17
|
-
code,
|
|
18
|
-
{ parser: 'typescript', semi: false }
|
|
19
|
-
)
|
|
14
|
+
const formatTS = code => prettier.format(code, { parser: 'typescript', semi: false })
|
|
20
15
|
|
|
21
16
|
const createSchemaCodeGen = schema =>
|
|
22
17
|
new SchemaCodeGenerator({
|
|
23
|
-
ast:
|
|
18
|
+
ast: graphql.parse(schema),
|
|
24
19
|
})
|
|
25
20
|
|
|
26
21
|
const testEntity = (generatedTypes, expectedEntity) => {
|
|
@@ -61,7 +56,7 @@ describe('Schema code generator', () => {
|
|
|
61
56
|
}
|
|
62
57
|
`)
|
|
63
58
|
|
|
64
|
-
expect(codegen.generateTypes().
|
|
59
|
+
expect(codegen.generateTypes().length).toBe(0)
|
|
65
60
|
})
|
|
66
61
|
|
|
67
62
|
describe('Should generate correct classes for each entity', () => {
|
|
@@ -99,7 +94,7 @@ describe('Schema code generator', () => {
|
|
|
99
94
|
const foo = generatedTypes.find(type => type.name === 'Foo')
|
|
100
95
|
expect(foo).toBe(undefined)
|
|
101
96
|
// Account and Wallet
|
|
102
|
-
expect(generatedTypes.
|
|
97
|
+
expect(generatedTypes.length).toBe(2)
|
|
103
98
|
})
|
|
104
99
|
|
|
105
100
|
test('Account is an entity with the correct methods', () => {
|
|
@@ -248,7 +243,12 @@ describe('Schema code generator', () => {
|
|
|
248
243
|
},
|
|
249
244
|
{
|
|
250
245
|
name: 'set wallets',
|
|
251
|
-
params: [
|
|
246
|
+
params: [
|
|
247
|
+
new Param(
|
|
248
|
+
'value',
|
|
249
|
+
new NullableType(new ArrayType(new NamedType('string'))),
|
|
250
|
+
),
|
|
251
|
+
],
|
|
252
252
|
returnType: undefined,
|
|
253
253
|
body: `
|
|
254
254
|
if (!value) {
|
|
@@ -377,20 +377,21 @@ describe('Schema code generator', () => {
|
|
|
377
377
|
|
|
378
378
|
const generatedTypes = codegen.generateTypes()
|
|
379
379
|
testEntity(generatedTypes, {
|
|
380
|
-
name:
|
|
380
|
+
name: 'Task',
|
|
381
381
|
members: [],
|
|
382
382
|
methods: [
|
|
383
383
|
{
|
|
384
384
|
name: 'constructor',
|
|
385
385
|
params: [new Param('id', new NamedType('Bytes'))],
|
|
386
386
|
returnType: undefined,
|
|
387
|
-
body: "\n super()\n this.set('id', Value.fromBytes(id))\n "
|
|
387
|
+
body: "\n super()\n this.set('id', Value.fromBytes(id))\n ",
|
|
388
388
|
},
|
|
389
389
|
{
|
|
390
390
|
name: 'save',
|
|
391
391
|
params: [],
|
|
392
392
|
returnType: new NamedType('void'),
|
|
393
|
-
body:
|
|
393
|
+
body:
|
|
394
|
+
'\n' +
|
|
394
395
|
" let id = this.get('id')\n" +
|
|
395
396
|
' assert(id != null,\n' +
|
|
396
397
|
" 'Cannot save Task entity without an ID')\n" +
|
|
@@ -398,63 +399,67 @@ describe('Schema code generator', () => {
|
|
|
398
399
|
' assert(id.kind == ValueKind.BYTES,\n' +
|
|
399
400
|
" `Entities of type Task must have an ID of type Bytes but the id '${id.displayData()}' is of type ${id.displayKind()}`)\n" +
|
|
400
401
|
" store.set('Task', id.toBytes().toHexString(), this)\n" +
|
|
401
|
-
' }'
|
|
402
|
+
' }',
|
|
402
403
|
},
|
|
403
404
|
{
|
|
404
405
|
name: 'load',
|
|
405
406
|
static: true,
|
|
406
407
|
params: [new Param('id', new NamedType('Bytes'))],
|
|
407
408
|
returnType: new NullableType(new NamedType('Task')),
|
|
408
|
-
body:
|
|
409
|
+
body:
|
|
410
|
+
'\n' +
|
|
409
411
|
" return changetype<Task | null>(store.get('Task', id.toHexString()))\n" +
|
|
410
|
-
' '
|
|
412
|
+
' ',
|
|
411
413
|
},
|
|
412
414
|
{
|
|
413
415
|
name: 'get id',
|
|
414
416
|
params: [],
|
|
415
417
|
returnType: new NamedType('Bytes'),
|
|
416
|
-
body:
|
|
418
|
+
body:
|
|
419
|
+
'\n' +
|
|
417
420
|
" let value = this.get('id')\n" +
|
|
418
421
|
' return value!.toBytes()\n' +
|
|
419
|
-
' '
|
|
422
|
+
' ',
|
|
420
423
|
},
|
|
421
424
|
{
|
|
422
425
|
name: 'set id',
|
|
423
426
|
params: [new Param('value', new NamedType('Bytes'))],
|
|
424
427
|
returnType: undefined,
|
|
425
|
-
body: "\n this.set('id', Value.fromBytes(value))\n "
|
|
428
|
+
body: "\n this.set('id', Value.fromBytes(value))\n ",
|
|
426
429
|
},
|
|
427
430
|
{
|
|
428
431
|
name: 'get employee',
|
|
429
432
|
params: [],
|
|
430
433
|
returnType: new NamedType('Bytes'),
|
|
431
|
-
body:
|
|
434
|
+
body:
|
|
435
|
+
'\n' +
|
|
432
436
|
" let value = this.get('employee')\n" +
|
|
433
437
|
' return value!.toBytes()\n' +
|
|
434
|
-
' '
|
|
438
|
+
' ',
|
|
435
439
|
},
|
|
436
440
|
{
|
|
437
441
|
name: 'set employee',
|
|
438
442
|
params: [new Param('value', new NamedType('Bytes'))],
|
|
439
443
|
returnType: undefined,
|
|
440
|
-
body: "\n this.set('employee', Value.fromBytes(value))\n "
|
|
444
|
+
body: "\n this.set('employee', Value.fromBytes(value))\n ",
|
|
441
445
|
},
|
|
442
446
|
{
|
|
443
447
|
name: 'get worker',
|
|
444
448
|
params: [],
|
|
445
449
|
returnType: new NamedType('Bytes'),
|
|
446
|
-
body:
|
|
450
|
+
body:
|
|
451
|
+
'\n' +
|
|
447
452
|
" let value = this.get('worker')\n" +
|
|
448
453
|
' return value!.toBytes()\n' +
|
|
449
|
-
' '
|
|
454
|
+
' ',
|
|
450
455
|
},
|
|
451
456
|
{
|
|
452
457
|
name: 'set worker',
|
|
453
458
|
params: [new Param('value', new NamedType('Bytes'))],
|
|
454
459
|
returnType: undefined,
|
|
455
|
-
body: "\n this.set('worker', Value.fromBytes(value))\n "
|
|
456
|
-
}
|
|
457
|
-
]
|
|
460
|
+
body: "\n this.set('worker', Value.fromBytes(value))\n ",
|
|
461
|
+
},
|
|
462
|
+
],
|
|
458
463
|
})
|
|
459
464
|
})
|
|
460
465
|
})
|
package/src/codegen/template.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const immutable = require('immutable')
|
|
2
1
|
const IpfsFileTemplateCodeGen = require('../protocols/ipfs/codegen/file_template')
|
|
3
2
|
|
|
4
3
|
const tsCodegen = require('./typescript')
|
|
@@ -29,7 +28,7 @@ module.exports = class DataSourceTemplateCodeGenerator {
|
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
generateTypes() {
|
|
32
|
-
return
|
|
31
|
+
return [this._generateTemplateType()]
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
_generateTemplateType() {
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const immutable = require('immutable')
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* ethereum.Value -> AssemblyScript conversions
|
|
5
3
|
*/
|
|
@@ -55,29 +53,56 @@ const ETHEREUM_VALUE_TO_ASSEMBLYSCRIPT = [
|
|
|
55
53
|
|
|
56
54
|
// Multi dimensional arrays
|
|
57
55
|
|
|
58
|
-
[
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
[
|
|
57
|
+
/^address\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
58
|
+
'Array<Array<Address>>',
|
|
59
|
+
code => `${code}.toAddressMatrix()`,
|
|
60
|
+
],
|
|
61
|
+
[
|
|
62
|
+
/^bool\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
63
|
+
'Array<Array<boolean>>',
|
|
64
|
+
code => `${code}.toBooleanMatrix()`,
|
|
65
|
+
],
|
|
66
|
+
[
|
|
67
|
+
/^byte\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
68
|
+
'Array<Array<Bytes>>',
|
|
69
|
+
code => `${code}.toBytesMatrix()`,
|
|
70
|
+
],
|
|
61
71
|
[
|
|
62
72
|
/^bytes(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)?\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
63
73
|
'Array<Array<Bytes>>',
|
|
64
74
|
code => `${code}.toBytesMatrix()`,
|
|
65
75
|
],
|
|
66
|
-
[
|
|
67
|
-
|
|
68
|
-
|
|
76
|
+
[
|
|
77
|
+
/^int(8|16|24|32)\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
78
|
+
'Array<Array<i32>>',
|
|
79
|
+
code => `${code}.toI32Matrix()`,
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
/^uint(8|16|24)\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
83
|
+
'Array<Array<i32>>',
|
|
84
|
+
code => `${code}.toI32Matrix()`,
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
/^uint32\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
88
|
+
'Array<Array<BigInt>>',
|
|
89
|
+
code => `${code}.toBigIntMatrix()`,
|
|
90
|
+
],
|
|
69
91
|
[
|
|
70
92
|
/^u?int(40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
71
93
|
'Array<Array<BigInt>>',
|
|
72
94
|
code => `${code}.toBigIntMatrix()`,
|
|
73
95
|
],
|
|
74
|
-
[
|
|
96
|
+
[
|
|
97
|
+
/^string\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
98
|
+
'Array<Array<string>>',
|
|
99
|
+
code => `${code}.toStringMatrix()`,
|
|
100
|
+
],
|
|
75
101
|
[
|
|
76
102
|
/^tuple\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
77
103
|
'Array<Array<ethereum.Tuple>>',
|
|
78
104
|
(code, type) => `${code}.toTupleMatrix<${type}>()`,
|
|
79
105
|
],
|
|
80
|
-
|
|
81
106
|
]
|
|
82
107
|
|
|
83
108
|
/**
|
|
@@ -178,7 +203,7 @@ const ASSEMBLYSCRIPT_TO_ETHEREUM_VALUE = [
|
|
|
178
203
|
/^tuple\[([0-9]+)?\]$/,
|
|
179
204
|
code => `ethereum.Value.fromTupleArray(${code})`,
|
|
180
205
|
],
|
|
181
|
-
|
|
206
|
+
|
|
182
207
|
// Multi dimentional arrays
|
|
183
208
|
|
|
184
209
|
[
|
|
@@ -282,9 +307,13 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
|
|
|
282
307
|
['Array<Array<BigInt>>', '[[BigInt]]', code => `Value.fromBigIntMatrix(${code})`],
|
|
283
308
|
['Array<Array<string>>', '[[String]]', code => `Value.fromStringMatrix(${code})`],
|
|
284
309
|
['Array<Array<string>>', '[[ID]]', code => `Value.fromStringMatrix(${code})`],
|
|
285
|
-
[
|
|
310
|
+
[
|
|
311
|
+
'Array<Array<BigDecimal>>',
|
|
312
|
+
'[[BigDecimal]]',
|
|
313
|
+
code => `Value.fromBigDecimalMatrix(${code})`,
|
|
314
|
+
],
|
|
286
315
|
['Array<Array<string>>', /\[\[.*\]\]/, code => `Value.fromStringMatrix(${code})`],
|
|
287
|
-
['Array<Array<string | null>>', null, code => `Value.fromStringMatrix(${code})`]
|
|
316
|
+
['Array<Array<string | null>>', null, code => `Value.fromStringMatrix(${code})`], // is this overwriting the Array null below?
|
|
288
317
|
|
|
289
318
|
// Arrays
|
|
290
319
|
|
|
@@ -315,7 +344,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
|
|
|
315
344
|
/**
|
|
316
345
|
* Type conversions
|
|
317
346
|
*/
|
|
318
|
-
module.exports =
|
|
347
|
+
module.exports = Object.freeze({
|
|
319
348
|
ethereum: {
|
|
320
349
|
AssemblyScript: ETHEREUM_VALUE_TO_ASSEMBLYSCRIPT,
|
|
321
350
|
},
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
const immutable = require('immutable')
|
|
2
|
-
|
|
3
1
|
const TYPE_CONVERSIONS = require('./conversions')
|
|
4
2
|
|
|
5
3
|
// Conversion utilities
|
|
6
4
|
|
|
7
5
|
const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
|
|
8
|
-
let conversions = TYPE_CONVERSIONS
|
|
6
|
+
let conversions = TYPE_CONVERSIONS[fromTypeSystem]?.[toTypeSystem]
|
|
9
7
|
if (conversions === undefined) {
|
|
10
8
|
throw new Error(
|
|
11
9
|
`Conversions from '${fromTypeSystem}' to '${toTypeSystem}' are not supported`,
|
|
@@ -15,16 +13,16 @@ const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
|
|
|
15
13
|
}
|
|
16
14
|
|
|
17
15
|
const objectifyConversion = (fromTypeSystem, toTypeSystem, conversion) => {
|
|
18
|
-
return
|
|
16
|
+
return Object.freeze({
|
|
19
17
|
from: {
|
|
20
18
|
typeSystem: fromTypeSystem,
|
|
21
|
-
type: conversion
|
|
19
|
+
type: conversion[0],
|
|
22
20
|
},
|
|
23
21
|
to: {
|
|
24
22
|
typeSystem: toTypeSystem,
|
|
25
|
-
type: conversion
|
|
23
|
+
type: conversion[1],
|
|
26
24
|
},
|
|
27
|
-
convert: conversion
|
|
25
|
+
convert: conversion[2],
|
|
28
26
|
})
|
|
29
27
|
}
|
|
30
28
|
|
|
@@ -32,9 +30,9 @@ const findConversionFromType = (fromTypeSystem, toTypeSystem, fromType) => {
|
|
|
32
30
|
let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
|
|
33
31
|
|
|
34
32
|
let conversion = conversions.find(conversion =>
|
|
35
|
-
typeof conversion
|
|
36
|
-
? conversion
|
|
37
|
-
: fromType.match(conversion
|
|
33
|
+
typeof conversion[0] === 'string'
|
|
34
|
+
? conversion[0] === fromType
|
|
35
|
+
: fromType.match(conversion[0]),
|
|
38
36
|
)
|
|
39
37
|
|
|
40
38
|
if (conversion === undefined) {
|
|
@@ -51,9 +49,9 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
|
|
|
51
49
|
let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
|
|
52
50
|
|
|
53
51
|
let conversion = conversions.find(conversion =>
|
|
54
|
-
typeof conversion
|
|
55
|
-
? conversion
|
|
56
|
-
: toType.match(conversion
|
|
52
|
+
typeof conversion[1] === 'string'
|
|
53
|
+
? conversion[1] === toType
|
|
54
|
+
: toType.match(conversion[1]),
|
|
57
55
|
)
|
|
58
56
|
|
|
59
57
|
if (conversion === undefined) {
|
|
@@ -69,35 +67,34 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
|
|
|
69
67
|
// High-level type system API
|
|
70
68
|
|
|
71
69
|
const ascTypeForProtocol = (protocol, protocolType) =>
|
|
72
|
-
findConversionFromType(protocol, 'AssemblyScript', protocolType).
|
|
70
|
+
findConversionFromType(protocol, 'AssemblyScript', protocolType).to?.type
|
|
73
71
|
|
|
74
72
|
// TODO: this can be removed/replaced by the function above
|
|
75
|
-
const ascTypeForEthereum = ethereumType =>
|
|
76
|
-
ascTypeForProtocol('ethereum', ethereumType)
|
|
73
|
+
const ascTypeForEthereum = ethereumType => ascTypeForProtocol('ethereum', ethereumType)
|
|
77
74
|
|
|
78
75
|
const ethereumTypeForAsc = ascType =>
|
|
79
|
-
findConversionFromType('AssemblyScript', 'ethereum', ascType).
|
|
76
|
+
findConversionFromType('AssemblyScript', 'ethereum', ascType).to?.type
|
|
80
77
|
|
|
81
78
|
const ethereumToAsc = (code, ethereumType, internalType) =>
|
|
82
|
-
findConversionFromType('ethereum', 'AssemblyScript', ethereumType).
|
|
79
|
+
findConversionFromType('ethereum', 'AssemblyScript', ethereumType).convert(
|
|
83
80
|
code,
|
|
84
81
|
internalType,
|
|
85
82
|
)
|
|
86
83
|
|
|
87
84
|
const ethereumFromAsc = (code, ethereumType) =>
|
|
88
|
-
findConversionToType('AssemblyScript', 'ethereum', ethereumType).
|
|
85
|
+
findConversionToType('AssemblyScript', 'ethereum', ethereumType).convert(code)
|
|
89
86
|
|
|
90
87
|
const ascTypeForValue = valueType =>
|
|
91
|
-
findConversionFromType('Value', 'AssemblyScript', valueType).
|
|
88
|
+
findConversionFromType('Value', 'AssemblyScript', valueType).to?.type
|
|
92
89
|
|
|
93
90
|
const valueTypeForAsc = ascType =>
|
|
94
|
-
findConversionFromType('AssemblyScript', 'Value', ascType).
|
|
91
|
+
findConversionFromType('AssemblyScript', 'Value', ascType).to?.type
|
|
95
92
|
|
|
96
93
|
const valueToAsc = (code, valueType) =>
|
|
97
|
-
findConversionFromType('Value', 'AssemblyScript', valueType).
|
|
94
|
+
findConversionFromType('Value', 'AssemblyScript', valueType).convert(code)
|
|
98
95
|
|
|
99
96
|
const valueFromAsc = (code, valueType) =>
|
|
100
|
-
findConversionToType('AssemblyScript', 'Value', valueType).
|
|
97
|
+
findConversionToType('AssemblyScript', 'Value', valueType).convert(code)
|
|
101
98
|
|
|
102
99
|
module.exports = {
|
|
103
100
|
// protocol <-> AssemblyScript
|