Browse Source

Make defs.json fully runtime-compatible

Leszek Wiesner 4 years ago
parent
commit
e13f39412e

+ 2 - 2
types/.eslintrc.js

@@ -3,6 +3,6 @@ module.exports = {
     '@typescript-eslint/class-name-casing': 'off',
     'no-unused-vars': 'off', // Required by the typescript rule below
     '@typescript-eslint/no-unused-vars': ['error'],
-    "@typescript-eslint/naming-convention": 'off'
-  }
+    '@typescript-eslint/naming-convention': 'off',
+  },
 }

+ 27 - 2
types/augment/all/definitions.ts

@@ -1,5 +1,30 @@
-const defs = require('./defs.json');
+import { Struct, TypeRegistry } from '@polkadot/types'
+import { RegistryTypes } from '@polkadot/types/types'
+import defs from'./defs.json'
+
+const emptyStruct = new Struct(new TypeRegistry(), {})
+
+// Prevents errors in interfaces that will be generated based on those types
+// ie. an interface cannot have fields like "values" and extend Struct (which also has "values") at the same time
+const normalizedDefs = {} as RegistryTypes
+Object.entries(defs).forEach(([typeName, typeDef]) => {
+  if (typeof typeDef !== 'string' && !typeDef.hasOwnProperty('_enum') && !typeDef.hasOwnProperty('_set')) {
+     // definition is a struct:
+    const normalizedDef = {} as Record<string, string>
+    Object.entries(typeDef).forEach(([key, value]) => {
+      if ((emptyStruct as any)[key] !== undefined) {
+        return
+      }
+      normalizedDef[key] = value
+    })
+    normalizedDefs[typeName] = normalizedDef;
+  }
+  else {
+    normalizedDefs[typeName] = typeDef
+  }
+})
+
 
 export default {
-  types: defs
+  types: normalizedDefs
 }

+ 2 - 0
types/augment/all/defs.json

@@ -561,6 +561,7 @@
         "owner": "MemberId",
         "added_at": "BlockAndTime",
         "type_id": "DataObjectTypeId",
+        "size": "u64",
         "liaison": "StorageProviderId",
         "liaison_judgement": "LiaisonJudgement",
         "ipfs_content_id": "Text"
@@ -848,6 +849,7 @@
         "entity_permissions": "EntityPermissions",
         "class_id": "ClassId",
         "supported_schemas": "Vec<SchemaId>",
+        "values": "BTreeMap<PropertyId,StoredPropertyValue>",
         "reference_counter": "InboundReferenceCounter"
     },
     "CuratorGroup": {

+ 0 - 6
types/src/scripts/generateRegistryJson.ts

@@ -2,7 +2,6 @@
 
 import { types } from '../index'
 import { Constructor, Codec, RegistryTypes, Registry } from '@polkadot/types/types'
-import { Struct } from '@polkadot/types/codec'
 import { TypeRegistry } from '@polkadot/types'
 import fs from 'fs'
 import path from 'path'
@@ -49,13 +48,8 @@ function normalizeDef(registry: Registry, defOrConstructor: any, typeName: strin
       throw new Error('_set definitions are not supported yet!')
     } else {
       // Struct - normalize properties
-      const emptyStruct = new Struct(registry, {}) as any
       for (const [key, value] of Object.entries(obj)) {
         // Prevent interface clashes
-        // FIXME: Would be way better if this was actually done by @polkadot/typegen tool
-        if (emptyStruct[key] !== undefined) {
-          continue
-        }
         const normalizedValue = normalizeDef(registry, value, `${typeName}[${key}]`)
         if (typeof normalizedValue !== 'string') {
           throw new Error(