Tests & adjustments for encodeTypes (#2853)

* Tests & adjustments for encodeTypes

* explicit wrap types

* Remove params

* cleanups
This commit is contained in:
Jaco Greeff
2020-11-16 06:40:49 +01:00
committed by GitHub
parent 165381d79a
commit ce6c25e4e2
10 changed files with 273 additions and 502 deletions
+1 -1
View File
@@ -119,7 +119,7 @@ export class MetaRegistry extends TypeRegistry {
: {}
),
...(type.params.length > 0
? { params: type.params.map((type) => this.getMetaTypeDef({ type })) }
? { sub: type.params.map((type) => this.getMetaTypeDef({ type })) }
: {}
),
...typeDef
@@ -29,15 +29,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"displayName": "Hash",
"namespace": "ink_env::types",
"info": 6,
"type": "Hash"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -66,6 +57,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"Hash\"}}"
},
{
@@ -108,15 +100,6 @@
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 9,
"sub": [
{
"displayName": "AccountId",
@@ -131,6 +114,7 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"AccountId\",\"key_index\":\"u32\"}"
},
{
@@ -141,38 +125,6 @@
},
{
"displayName": "Result",
"params": [
{
"info": 10,
"sub": [],
"type": "()"
},
{
"displayName": "Error",
"namespace": "dns::dns",
"info": 3,
"sub": [
{
"ext": {
"discriminant": 0
},
"info": 6,
"name": "NameAlreadyExists",
"type": "Null"
},
{
"ext": {
"discriminant": 1
},
"info": 6,
"name": "CallerIsNotOwner",
"type": "Null"
}
],
"type": "{ \"_enum\": [\"NameAlreadyExists\", \"CallerIsNotOwner\"] }"
}
],
"info": 7,
"sub": [
{
"name": "Ok",
@@ -203,9 +155,10 @@
"type": "Null"
}
],
"type": "{ \"_enum\": [\"NameAlreadyExists\", \"CallerIsNotOwner\"] }"
"type": "{\"_enum\":[\"NameAlreadyExists\",\"CallerIsNotOwner\"]}"
}
],
"info": 7,
"type": "Result<(), Error>"
},
{
@@ -235,25 +188,17 @@
"type": "Null"
}
],
"type": "{ \"_enum\": [\"NameAlreadyExists\", \"CallerIsNotOwner\"] }"
"type": "{\"_enum\":[\"NameAlreadyExists\",\"CallerIsNotOwner\"]}"
},
{
"displayName": "Option",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 5,
"sub": {
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
},
"info": 5,
"type": "Option<AccountId>"
}
]
@@ -33,15 +33,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -70,6 +61,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"AccountId\"}}"
},
{
@@ -112,13 +104,6 @@
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"info": 6,
"type": "u128"
}
],
"info": 9,
"sub": [
{
"info": 6,
@@ -131,32 +116,12 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"u128\",\"key_index\":\"u32\"}"
},
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"info": 10,
"sub": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
},
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"type": "(AccountId, AccountId)"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -197,6 +162,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"(AccountId, AccountId)\"}}"
},
{
@@ -223,21 +189,13 @@
},
{
"displayName": "Option",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 5,
"sub": {
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
},
"info": 5,
"type": "Option<AccountId>"
}
]
@@ -29,13 +29,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"info": 6,
"type": "u32"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -62,6 +55,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"u32\"}}"
},
{
@@ -85,15 +79,6 @@
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 9,
"sub": [
{
"displayName": "AccountId",
@@ -108,6 +93,7 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"AccountId\",\"key_index\":\"u32\"}"
},
{
@@ -132,15 +118,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -169,18 +146,12 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"AccountId\"}}"
},
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"info": 6,
"type": "u32"
}
],
"info": 9,
"sub": [
{
"info": 6,
@@ -193,32 +164,12 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"u32\",\"key_index\":\"u32\"}"
},
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"info": 10,
"sub": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
},
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"type": "(AccountId, AccountId)"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -259,6 +210,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"(AccountId, AccountId)\"}}"
},
{
@@ -282,13 +234,6 @@
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"info": 6,
"type": "bool"
}
],
"info": 9,
"sub": [
{
"info": 6,
@@ -301,6 +246,7 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"bool\",\"key_index\":\"u32\"}"
},
{
@@ -309,105 +255,17 @@
},
{
"displayName": "Option",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 5,
"sub": {
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
},
"info": 5,
"type": "Option<AccountId>"
},
{
"displayName": "Result",
"params": [
{
"info": 10,
"sub": [],
"type": "()"
},
{
"displayName": "Error",
"namespace": "erc721::erc721",
"info": 3,
"sub": [
{
"ext": {
"discriminant": 0
},
"info": 6,
"name": "NotOwner",
"type": "Null"
},
{
"ext": {
"discriminant": 1
},
"info": 6,
"name": "NotApproved",
"type": "Null"
},
{
"ext": {
"discriminant": 2
},
"info": 6,
"name": "TokenExists",
"type": "Null"
},
{
"ext": {
"discriminant": 3
},
"info": 6,
"name": "TokenNotFound",
"type": "Null"
},
{
"ext": {
"discriminant": 4
},
"info": 6,
"name": "CannotInsert",
"type": "Null"
},
{
"ext": {
"discriminant": 5
},
"info": 6,
"name": "CannotRemove",
"type": "Null"
},
{
"ext": {
"discriminant": 6
},
"info": 6,
"name": "CannotFetchValue",
"type": "Null"
},
{
"ext": {
"discriminant": 7
},
"info": 6,
"name": "NotAllowed",
"type": "Null"
}
],
"type": "{ \"_enum\": [\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"] }"
}
],
"info": 7,
"sub": [
{
"name": "Ok",
@@ -486,9 +344,10 @@
"type": "Null"
}
],
"type": "{ \"_enum\": [\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"] }"
"type": "{\"_enum\":[\"NotOwner\",\"NotApproved\",\"TokenExists\",\"TokenNotFound\",\"CannotInsert\",\"CannotRemove\",\"CannotFetchValue\",\"NotAllowed\"]}"
}
],
"info": 7,
"type": "Result<(), Error>"
},
{
@@ -566,6 +425,6 @@
"type": "Null"
}
],
"type": "{ \"_enum\": [\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"] }"
"type": "{\"_enum\":[\"NotOwner\",\"NotApproved\",\"TokenExists\",\"TokenNotFound\",\"CannotInsert\",\"CannotRemove\",\"CannotFetchValue\",\"NotAllowed\"]}"
}
]
@@ -29,25 +29,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"info": 10,
"sub": [
{
"info": 6,
"type": "u32"
},
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"type": "(u32, AccountId)"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -86,6 +67,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"(u32, AccountId)\"}}"
},
{
@@ -144,14 +126,6 @@
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"info": 10,
"sub": [],
"type": "()"
}
],
"info": 9,
"sub": [
{
"info": 10,
@@ -165,6 +139,7 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"()\",\"key_index\":\"u32\"}"
},
{
@@ -175,13 +150,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"info": 6,
"type": "u32"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -208,18 +176,12 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"u32\"}}"
},
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"info": 6,
"type": "u32"
}
],
"info": 9,
"sub": [
{
"info": 6,
@@ -232,58 +194,12 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"u32\",\"key_index\":\"u32\"}"
},
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"displayName": "Transaction",
"namespace": "multisig_plain::multisig_plain",
"info": 9,
"sub": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId",
"name": "callee"
},
{
"info": 12,
"length": 4,
"sub": {
"info": 6,
"type": "u8"
},
"type": "[u8;4]",
"name": "selector"
},
{
"info": 11,
"sub": {
"info": 6,
"type": "u8"
},
"type": "Vec<u8>",
"name": "input"
},
{
"info": 6,
"type": "u128",
"name": "transferred_value"
},
{
"info": 6,
"type": "u64",
"name": "gas_limit"
}
],
"type": "{\"callee\":\"AccountId\",\"selector\":\"[u8;4]\",\"input\":\"Vec<u8>\",\"transferred_value\":\"u128\",\"gas_limit\":\"u64\"}"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -350,6 +266,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"Transaction\"}}"
},
{
@@ -424,15 +341,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -461,6 +369,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"AccountId\"}}"
},
{
@@ -523,19 +432,6 @@
},
{
"displayName": "Result",
"params": [
{
"info": 10,
"sub": [],
"type": "()"
},
{
"info": 10,
"sub": [],
"type": "()"
}
],
"info": 7,
"sub": [
{
"name": "Ok",
@@ -550,26 +446,11 @@
"type": "()"
}
],
"info": 7,
"type": "Result<(), ()>"
},
{
"displayName": "Result",
"params": [
{
"info": 11,
"sub": {
"info": 6,
"type": "u8"
},
"type": "Vec<u8>"
},
{
"info": 10,
"sub": [],
"type": "()"
}
],
"info": 7,
"sub": [
{
"name": "Ok",
@@ -587,56 +468,15 @@
"type": "()"
}
],
"info": 7,
"type": "Result<Vec<u8>, ()>"
},
{
"displayName": "Result",
"params": [
{
"displayName": "Option",
"params": [
{
"info": 11,
"sub": {
"info": 6,
"type": "u8"
},
"type": "Vec<u8>"
}
],
"info": 5,
"sub": {
"info": 11,
"sub": {
"info": 6,
"type": "u8"
},
"type": "Vec<u8>"
},
"type": "Option<Vec<u8>>"
},
{
"info": 10,
"sub": [],
"type": "()"
}
],
"info": 7,
"sub": [
{
"name": "Ok",
"displayName": "Option",
"params": [
{
"info": 11,
"sub": {
"info": 6,
"type": "u8"
},
"type": "Vec<u8>"
}
],
"info": 5,
"sub": {
"info": 11,
"sub": {
@@ -645,6 +485,7 @@
},
"type": "Vec<u8>"
},
"info": 5,
"type": "Option<Vec<u8>>"
},
{
@@ -654,21 +495,11 @@
"type": "()"
}
],
"info": 7,
"type": "Result<Option<Vec<u8>>, ()>"
},
{
"displayName": "Option",
"params": [
{
"info": 11,
"sub": {
"info": 6,
"type": "u8"
},
"type": "Vec<u8>"
}
],
"info": 5,
"sub": {
"info": 11,
"sub": {
@@ -677,6 +508,7 @@
},
"type": "Vec<u8>"
},
"info": 5,
"type": "Option<Vec<u8>>"
}
]
@@ -84,7 +84,7 @@
"type": "Null"
}
],
"type": "{ \"_enum\": [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"] }",
"type": "{\"_enum\":[\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\",\"Sunday\"]}",
"name": "Weekday"
},
{
@@ -286,7 +286,7 @@
"type": "Null"
}
],
"type": "{ \"_enum\": [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"] }"
"type": "{\"_enum\":[\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\",\"Sunday\"]}"
},
{
"displayName": "TupleMaybeSigned",
@@ -33,15 +33,6 @@
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -70,6 +61,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"AccountId\"}}"
},
{
@@ -112,13 +104,6 @@
{
"displayName": "ValueEntry",
"namespace": "ink_storage::collections::hashmap",
"params": [
{
"info": 6,
"type": "u128"
}
],
"info": 9,
"sub": [
{
"info": 6,
@@ -131,32 +116,12 @@
"name": "key_index"
}
],
"info": 9,
"type": "{\"value\":\"u128\",\"key_index\":\"u32\"}"
},
{
"displayName": "Entry",
"namespace": "ink_storage::collections::stash",
"params": [
{
"info": 10,
"sub": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
},
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"type": "(AccountId, AccountId)"
}
],
"info": 3,
"sub": [
{
"displayName": "VacantEntry",
@@ -197,6 +162,7 @@
"name": "Occupied"
}
],
"info": 3,
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"(AccountId, AccountId)\"}}"
},
{
@@ -227,21 +193,13 @@
},
{
"displayName": "Option",
"params": [
{
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
}
],
"info": 5,
"sub": {
"displayName": "AccountId",
"namespace": "ink_env::types",
"info": 6,
"type": "AccountId"
},
"info": 5,
"type": "Option<AccountId>"
}
]
@@ -0,0 +1,214 @@
// Copyright 2017-2020 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeDefInfo } from './types';
import { encodeTypeDef } from './encodeTypes';
describe('encodeTypeDef', (): void => {
it('correctly encodes a complex struct', (): void => {
expect(
JSON.parse(encodeTypeDef({
info: TypeDefInfo.Struct,
sub: [
{
info: TypeDefInfo.Plain,
name: 'a',
type: 'u32'
},
{
info: TypeDefInfo.Struct,
name: 'b',
sub: [
{
info: TypeDefInfo.Plain,
name: 'c',
type: 'u32'
},
{
info: TypeDefInfo.Vec,
name: 'd',
sub: {
info: TypeDefInfo.Plain,
type: 'u32'
},
type: ''
}
],
type: ''
}
],
type: ''
}))
).toEqual({
a: 'u32',
b: '{"c":"u32","d":"Vec<u32>"}'
});
});
it('correctly encodes a complex struct (named)', (): void => {
expect(
JSON.parse(encodeTypeDef({
info: TypeDefInfo.Struct,
sub: [
{
info: TypeDefInfo.Plain,
name: 'a',
type: 'u32'
},
{
info: TypeDefInfo.Struct,
name: 'b',
sub: [
{
info: TypeDefInfo.Plain,
name: 'c',
type: 'u32'
},
{
displayName: 'Something',
info: TypeDefInfo.Vec,
name: 'd',
sub: {
info: TypeDefInfo.Plain,
type: 'u32'
},
type: ''
}
],
type: ''
}
],
type: ''
}))
).toEqual({
a: 'u32',
b: '{"c":"u32","d":"Something"}'
});
});
it('correctly encodes a complex enum', (): void => {
expect(
JSON.parse(encodeTypeDef({
info: TypeDefInfo.Enum,
sub: [
{
info: TypeDefInfo.Plain,
name: 'a',
type: 'u32'
},
{
info: TypeDefInfo.Struct,
name: 'b',
sub: [
{
info: TypeDefInfo.Plain,
name: 'c',
type: 'u32'
},
{
info: TypeDefInfo.Vec,
name: 'd',
sub: {
info: TypeDefInfo.Plain,
type: 'u32'
},
type: ''
}
],
type: ''
},
{
info: TypeDefInfo.Enum,
name: 'f',
sub: [
{
info: TypeDefInfo.Plain,
name: 'g',
type: 'Null'
},
{
info: TypeDefInfo.Plain,
name: 'h',
type: 'Null'
}
],
type: ''
}
],
type: ''
}))
).toEqual({
_enum: {
a: 'u32',
b: '{"c":"u32","d":"Vec<u32>"}',
f: '{"_enum":["g","h"]}'
}
});
});
it('correctly encodes a complex enum (named)', (): void => {
expect(
JSON.parse(encodeTypeDef({
info: TypeDefInfo.Enum,
sub: [
{
info: TypeDefInfo.Plain,
name: 'a',
type: 'u32'
},
{
displayName: 'Something',
info: TypeDefInfo.Struct,
name: 'b',
sub: [
{
info: TypeDefInfo.Plain,
name: 'c',
type: 'u32'
},
{
info: TypeDefInfo.Vec,
name: 'd',
sub: {
info: TypeDefInfo.Plain,
type: 'u32'
},
type: ''
}
],
type: ''
},
{
displayName: 'Option',
info: TypeDefInfo.Option,
name: 'e',
sub: {
displayName: 'Result',
info: TypeDefInfo.Result,
sub: [
{
info: TypeDefInfo.Null,
type: ''
},
{
info: TypeDefInfo.Plain,
type: 'u32'
}
],
type: ''
},
type: ''
}
],
type: ''
}))
).toEqual({
_enum: {
a: 'u32',
b: 'Something',
e: 'Option<Result<Null, u32>>'
}
});
});
});
+21 -15
View File
@@ -7,6 +7,8 @@ import { assert, isNumber, isUndefined } from '@polkadot/util';
const stringIdentity = <T extends { toString: () => string }> (value: T): string => value.toString();
const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
export function paramsNotation <T> (outer: string, inner?: T | T[], transform: (_: T) => string = stringIdentity): string {
return `${outer}${
inner
@@ -15,9 +17,8 @@ export function paramsNotation <T> (outer: string, inner?: T | T[], transform: (
}`;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
function encodeWithParams (typeDef: TypeDef, outer = typeDef.displayName || typeDef.type): string {
const { info, params, sub } = typeDef;
function encodeWithParams (typeDef: TypeDef, outer: string): string {
const { info, sub } = typeDef;
switch (info) {
case TypeDefInfo.BTreeMap:
@@ -28,22 +29,23 @@ function encodeWithParams (typeDef: TypeDef, outer = typeDef.displayName || type
case TypeDefInfo.Option:
case TypeDefInfo.Result:
case TypeDefInfo.Vec:
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return paramsNotation(outer, params || sub, (param) => encodeTypeDef(param));
default:
return outer;
return paramsNotation(outer, sub, (param) => encodeTypeDef(param));
}
throw new Error(`Unable to encode ${JSON.stringify(typeDef)} with params`);
}
function encodeDoNotConstruct ({ displayName }: TypeDef): string {
return `DoNotEncode<${displayName || 'Unknown'}>`;
return `DoNotConstruct<${displayName || 'Unknown'}>`;
}
function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string {
const names = sub.map(({ name }) => name);
assert(names.every((n) => !!n), `Subtypes does not have consistent names, ${names.join(', ')}`);
const inner = sub.reduce((result: Record<string, string>, type): Record<string, string> => ({
...result,
// eslint-disable-next-line @typescript-eslint/no-use-before-define
[type.name as string]: encodeTypeDef(type)
}), {});
@@ -62,7 +64,7 @@ function encodeEnum (typeDef: TypeDef): string {
// c-like enums have all Null entries
// TODO We need to take the disciminant into account and auto-add empty entries
return sub.every(({ type }) => type === 'Null')
? `{ "_enum": [${sub.map(({ name }, index) => `"${name || `Empty${index}`}"`).join(', ')}] }`
? JSON.stringify({ _enum: sub.map(({ name }, index) => `${name || `Empty${index}`}`) })
: encodeSubTypes(sub, true);
}
@@ -75,7 +77,6 @@ function encodeStruct (typeDef: TypeDef): string {
function encodeTuple (typeDef: TypeDef): string {
assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Tuple type');
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return `(${typeDef.sub.map((type) => encodeTypeDef(type)).join(', ')})`;
}
@@ -126,9 +127,14 @@ function encodeType (typeDef:TypeDef): string {
export function encodeTypeDef (typeDef: TypeDef): string {
assert(!isUndefined(typeDef.info), `Invalid type definition with no instance info, ${JSON.stringify(typeDef)}`);
return typeDef.displayName || [TypeDefInfo.Enum, TypeDefInfo.Struct].includes(typeDef.info)
? encodeWithParams(typeDef)
: encodeType(typeDef);
// In the case of contracts we do have the unfortunate situation where the displayName would
// refer to "Option" when it is an option. For these, string it out, only using when actually
// not a top-level element to be used
if (typeDef.displayName && !INFO_WRAP.some((i) => typeDef.displayName === i)) {
return typeDef.displayName;
}
return encodeType(typeDef);
}
export function withTypeString (typeDef: Omit<TypeDef, 'type'>): TypeDef {
-1
View File
@@ -47,7 +47,6 @@ export interface TypeDef {
length?: number;
name?: string;
namespace?: string;
params?: TypeDef[];
sub?: TypeDef | TypeDef[];
type: string;
}