Skip to content

Commit

Permalink
feat: remove immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Dec 14, 2022
1 parent 09b4c2a commit 3d181db
Show file tree
Hide file tree
Showing 33 changed files with 801 additions and 852 deletions.
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"glob": "7.1.6",
"gluegun": "https://github.com/edgeandnode/gluegun#v4.3.1-pin-colors-dep",
"graphql": "15.5.0",
"immutable": "3.8.2",
"ipfs-http-client": "34.0.0",
"jayson": "3.6.6",
"js-yaml": "3.13.1",
Expand Down
50 changes: 24 additions & 26 deletions packages/cli/src/codegen/schema.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
const immutable = require('immutable')

const tsCodegen = require('./typescript')
const typesCodegen = require('./types')

const List = immutable.List

class IdField {
static BYTES = Symbol("Bytes")
static STRING = Symbol("String")
static BYTES = Symbol('Bytes')
static STRING = Symbol('String')

constructor(idField) {
const typeName = idField.getIn(['type', 'type', 'name', 'value'])
this.kind = typeName === "Bytes" ? IdField.BYTES : IdField.STRING
this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING
}

typeName() {
return this.kind === IdField.BYTES ? "Bytes" : "string"
return this.kind === IdField.BYTES ? 'Bytes' : 'string'
}

gqlTypeName() {
return this.kind === IdField.BYTES ? "Bytes" : "String"
return this.kind === IdField.BYTES ? 'Bytes' : 'String'
}

tsNamedType() {
return tsCodegen.namedType(this.typeName())
}

tsValueFrom() {
return this.kind === IdField.BYTES ? "Value.fromBytes(id)" : "Value.fromString(id)"
return this.kind === IdField.BYTES ? 'Value.fromBytes(id)' : 'Value.fromString(id)'
}

tsValueKind() {
return this.kind === IdField.BYTES ? "ValueKind.BYTES" : "ValueKind.STRING"
return this.kind === IdField.BYTES ? 'ValueKind.BYTES' : 'ValueKind.STRING'
}

tsValueToString() {
return this.kind == IdField.BYTES ? "id.toBytes().toHexString()" : "id.toString()"
return this.kind == IdField.BYTES ? 'id.toBytes().toHexString()' : 'id.toString()'
}

tsToString() {
return this.kind == IdField.BYTES ? "id.toHexString()" : "id"
return this.kind == IdField.BYTES ? 'id.toHexString()' : 'id'
}

static fromFields(fields) {
Expand All @@ -48,7 +45,7 @@ class IdField {
}

static fromTypeDef(def) {
return IdField.fromFields(def.get("fields"))
return IdField.fromFields(def.get('fields'))
}
}

Expand Down Expand Up @@ -117,7 +114,7 @@ module.exports = class SchemaCodeGenerator {
.get('fields')
.reduce(
(methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)),
List(),
[],
)
.forEach(method => klass.addMethod(method))

Expand All @@ -138,7 +135,7 @@ module.exports = class SchemaCodeGenerator {
}

_generateStoreMethods(entityName, idField) {
return List.of(
return [
tsCodegen.method(
'save',
[],
Expand All @@ -162,14 +159,14 @@ module.exports = class SchemaCodeGenerator {
return changetype<${entityName} | null>(store.get('${entityName}', ${idField.tsToString()}))
`,
),
)
]
}

_generateEntityFieldMethods(entityDef, fieldDef) {
return List([
return [
this._generateEntityFieldGetter(entityDef, fieldDef),
this._generateEntityFieldSetter(entityDef, fieldDef),
])
]
}

_generateEntityFieldGetter(entityDef, fieldDef) {
Expand Down Expand Up @@ -206,17 +203,13 @@ module.exports = class SchemaCodeGenerator {
let paramTypeString = isNullable ? paramType.inner.toString() : paramType.toString()
let isArray = paramType instanceof tsCodegen.ArrayType

if (
isArray &&
paramType.inner instanceof tsCodegen.NullableType
) {
if (isArray && paramType.inner instanceof tsCodegen.NullableType) {
let baseType = this._baseType(gqlType)

throw new Error(`
GraphQL schema can't have List's with Nullable members.
Error in '${name}' field of type '[${baseType}]'.
Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`
)
Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`)
}

let setNonNullable = `
Expand Down Expand Up @@ -246,8 +239,13 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}

// If this is a reference to another type, the field has the type of
// the referred type's id field
const typeDef = this.schema.ast.get("definitions").
find(def => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) && def.getIn(["name", "value"]) === typeName)
const typeDef = this.schema.ast
.get('definitions')
.find(
def =>
(this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
def.getIn(['name', 'value']) === typeName,
)
if (typeDef) {
return IdField.fromTypeDef(typeDef).typeName()
} else {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/codegen/schema.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const prettier = require('prettier')
const graphql = require('graphql/language')
const immutable = require('immutable')
const SchemaCodeGenerator = require('./schema')
const {
Class,
Expand All @@ -20,7 +19,7 @@ const formatTS = code =>

const createSchemaCodeGen = schema =>
new SchemaCodeGenerator({
ast: immutable.fromJS(graphql.parse(schema)),
ast: graphql.parse(schema),
})

const testEntity = (generatedTypes, expectedEntity) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/codegen/template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const immutable = require('immutable')
const IpfsFileTemplateCodeGen = require('../protocols/ipfs/codegen/file_template')

const tsCodegen = require('./typescript')
Expand Down Expand Up @@ -29,7 +28,7 @@ module.exports = class DataSourceTemplateCodeGenerator {
}

generateTypes() {
return immutable.List([this._generateTemplateType()])
return [this._generateTemplateType()]
}

_generateTemplateType() {
Expand Down
57 changes: 43 additions & 14 deletions packages/cli/src/codegen/types/conversions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const immutable = require('immutable')

/**
* ethereum.Value -> AssemblyScript conversions
*/
Expand Down Expand Up @@ -55,29 +53,56 @@ const ETHEREUM_VALUE_TO_ASSEMBLYSCRIPT = [

// Multi dimensional arrays

[/^address\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<Address>>', code => `${code}.toAddressMatrix()`],
[/^bool\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<boolean>>', code => `${code}.toBooleanMatrix()`],
[/^byte\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<Bytes>>', code => `${code}.toBytesMatrix()`],
[
/^address\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<Address>>',
code => `${code}.toAddressMatrix()`,
],
[
/^bool\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<boolean>>',
code => `${code}.toBooleanMatrix()`,
],
[
/^byte\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<Bytes>>',
code => `${code}.toBytesMatrix()`,
],
[
/^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]+)?\]$/,
'Array<Array<Bytes>>',
code => `${code}.toBytesMatrix()`,
],
[/^int(8|16|24|32)\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<i32>>', code => `${code}.toI32Matrix()`],
[/^uint(8|16|24)\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<i32>>', code => `${code}.toI32Matrix()`],
[/^uint32\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<BigInt>>', code => `${code}.toBigIntMatrix()`],
[
/^int(8|16|24|32)\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<i32>>',
code => `${code}.toI32Matrix()`,
],
[
/^uint(8|16|24)\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<i32>>',
code => `${code}.toI32Matrix()`,
],
[
/^uint32\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<BigInt>>',
code => `${code}.toBigIntMatrix()`,
],
[
/^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]+)?\]$/,
'Array<Array<BigInt>>',
code => `${code}.toBigIntMatrix()`,
],
[/^string\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<string>>', code => `${code}.toStringMatrix()`],
[
/^string\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<string>>',
code => `${code}.toStringMatrix()`,
],
[
/^tuple\[([0-9]+)?\]\[([0-9]+)?\]$/,
'Array<Array<ethereum.Tuple>>',
(code, type) => `${code}.toTupleMatrix<${type}>()`,
],

]

/**
Expand Down Expand Up @@ -178,7 +203,7 @@ const ASSEMBLYSCRIPT_TO_ETHEREUM_VALUE = [
/^tuple\[([0-9]+)?\]$/,
code => `ethereum.Value.fromTupleArray(${code})`,
],

// Multi dimentional arrays

[
Expand Down Expand Up @@ -282,9 +307,13 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
['Array<Array<BigInt>>', '[[BigInt]]', code => `Value.fromBigIntMatrix(${code})`],
['Array<Array<string>>', '[[String]]', code => `Value.fromStringMatrix(${code})`],
['Array<Array<string>>', '[[ID]]', code => `Value.fromStringMatrix(${code})`],
['Array<Array<BigDecimal>>', '[[BigDecimal]]', code => `Value.fromBigDecimalMatrix(${code})`],
[
'Array<Array<BigDecimal>>',
'[[BigDecimal]]',
code => `Value.fromBigDecimalMatrix(${code})`,
],
['Array<Array<string>>', /\[\[.*\]\]/, code => `Value.fromStringMatrix(${code})`],
['Array<Array<string | null>>', null, code => `Value.fromStringMatrix(${code})`],// is this overwriting the Array null below?
['Array<Array<string | null>>', null, code => `Value.fromStringMatrix(${code})`], // is this overwriting the Array null below?

// Arrays

Expand Down Expand Up @@ -315,7 +344,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
/**
* Type conversions
*/
module.exports = immutable.fromJS({
module.exports = Object.freeze({
ethereum: {
AssemblyScript: ETHEREUM_VALUE_TO_ASSEMBLYSCRIPT,
},
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/codegen/types/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const immutable = require('immutable')

const TYPE_CONVERSIONS = require('./conversions')

// Conversion utilities
Expand All @@ -15,7 +13,7 @@ const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
}

const objectifyConversion = (fromTypeSystem, toTypeSystem, conversion) => {
return immutable.fromJS({
return Object.freeze({
from: {
typeSystem: fromTypeSystem,
type: conversion.get(0),
Expand Down Expand Up @@ -72,8 +70,7 @@ const ascTypeForProtocol = (protocol, protocolType) =>
findConversionFromType(protocol, 'AssemblyScript', protocolType).getIn(['to', 'type'])

// TODO: this can be removed/replaced by the function above
const ascTypeForEthereum = ethereumType =>
ascTypeForProtocol('ethereum', ethereumType)
const ascTypeForEthereum = ethereumType => ascTypeForProtocol('ethereum', ethereumType)

const ethereumTypeForAsc = ascType =>
findConversionFromType('AssemblyScript', 'ethereum', ascType).getIn(['to', 'type'])
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/codegen/typescript.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
let immutable = require('immutable')
let Map = immutable.Map

class Param {
constructor(name, type) {
this.name = name
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/command-helpers/abi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { withSpinner } = require('./spinner')
const fetch = require('node-fetch')
const immutable = require('immutable')

const loadAbiFromEtherscan = async (ABI, network, address) =>
await withSpinner(
Expand All @@ -18,7 +17,7 @@ const loadAbiFromEtherscan = async (ABI, network, address) =>
// a `result` field. The `status` is '0' in case of errors and '1' in
// case of success
if (json.status === '1') {
return new ABI('Contract', undefined, immutable.fromJS(JSON.parse(json.result)))
return new ABI('Contract', undefined, JSON.parse(json.result))
} else {
throw new Error('ABI not found, try loading it from a local file')
}
Expand All @@ -42,7 +41,7 @@ const loadAbiFromBlockScout = async (ABI, network, address) =>
// a `result` field. The `status` is '0' in case of errors and '1' in
// case of success
if (json.status === '1') {
return new ABI('Contract', undefined, immutable.fromJS(JSON.parse(json.result)))
return new ABI('Contract', undefined, JSON.parse(json.result))
} else {
throw new Error('ABI not found, try loading it from a local file')
}
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/command-helpers/data-sources.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const immutable = require('immutable')
const { loadManifest } = require('../migrations/util/load-manifest')

// Loads manifest from file path and returns all:
Expand All @@ -13,15 +12,15 @@ const fromFilePath = async manifestPath => {

const extractDataSourceByType = (manifest, dataSourceType, protocol) =>
manifest
.get(dataSourceType, immutable.List())
.get(dataSourceType, [])
.reduce(
(dataSources, dataSource, dataSourceIndex) =>
protocol.isValidKindName(dataSource.get('kind'))
? dataSources.push(
immutable.Map({ path: [dataSourceType, dataSourceIndex], dataSource }),
{ path: [dataSourceType, dataSourceIndex], dataSource },
)
: dataSources,
immutable.List(),
[]
)

// Extracts data sources and templates from a immutable manifest data structure
Expand Down
Loading

0 comments on commit 3d181db

Please sign in to comment.