Enum serializes to camelCase (#3024)
* Enum serializes to camelCase * Dedupe * rewrite static json * test for StorageKind enum
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 4.0.1-x
|
||||
|
||||
Changes:
|
||||
|
||||
- Enum `.toSON()` now encodes all keys as `camelCase` (aligning with the output from Substrate)
|
||||
|
||||
|
||||
## 3.11.1 Feb 28, 2021
|
||||
|
||||
Upgrade priority: Low. Recommended for users wanting to keep updated with chain changes.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -30,7 +30,7 @@ describe('Enum', (): void => {
|
||||
{ Text, U32 },
|
||||
new Uint8Array([1, 2 << 2, 49, 50])
|
||||
).toString()
|
||||
).toEqual('{"U32":3289352}');
|
||||
).toEqual('{"u32":3289352}');
|
||||
});
|
||||
|
||||
it('decodes from a JSON input (lowercase)', (): void => {
|
||||
@@ -43,7 +43,7 @@ describe('Enum', (): void => {
|
||||
).toEqual('some text value');
|
||||
});
|
||||
|
||||
it('decodes reusing instanciated inputs', (): void => {
|
||||
it('decodes reusing instantiated inputs', (): void => {
|
||||
const foo = new Text(registry, 'bar');
|
||||
|
||||
expect(
|
||||
@@ -144,8 +144,8 @@ describe('Enum', (): void => {
|
||||
class C extends Null { }
|
||||
const Test = Enum.with({ A, B, C });
|
||||
|
||||
expect(new Test(registry).toJSON()).toEqual({ A: null });
|
||||
expect(new Test(registry, 1234, 1).toJSON()).toEqual({ B: 1234 });
|
||||
expect(new Test(registry).toJSON()).toEqual({ a: null });
|
||||
expect(new Test(registry, 1234, 1).toJSON()).toEqual({ b: 1234 });
|
||||
expect(new Test(registry, 0x1234, 1).toU8a()).toEqual(new Uint8Array([1, 0x34, 0x12, 0x00, 0x00]));
|
||||
expect(new Test(registry, 0x1234, 1).toU8a(true)).toEqual(new Uint8Array([0x34, 0x12, 0x00, 0x00]));
|
||||
});
|
||||
|
||||
@@ -99,7 +99,7 @@ function createFromValue (registry: Registry, def: TypesDef, index = 0, value?:
|
||||
}
|
||||
|
||||
function decodeFromJSON (registry: Registry, def: TypesDef, key: string, value?: any): Decoded {
|
||||
// JSON comes in the form of { "<type (lowercased)>": "<value for type>" }, here we
|
||||
// JSON comes in the form of { "<type (camelCase)>": "<value for type>" }, here we
|
||||
// additionally force to lower to ensure forward compat
|
||||
const keys = Object.keys(def).map((k) => k.toLowerCase());
|
||||
const keyLower = key.toLowerCase();
|
||||
@@ -343,7 +343,7 @@ export class Enum implements Codec {
|
||||
public toJSON (): AnyJson {
|
||||
return this.#isBasic
|
||||
? this.type
|
||||
: { [this.type]: this.#raw.toJSON() };
|
||||
: { [stringCamelCase(this.type)]: this.#raw.toJSON() };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@ describe('Result', (): void => {
|
||||
expect(result.asOk.toU8a()).toEqual(new Uint8Array([1, 2, 3, 4]));
|
||||
expect(result.toHex()).toEqual('0x0001020304');
|
||||
expect(result.toJSON()).toEqual({
|
||||
Ok: 0x04030201
|
||||
ok: 0x04030201
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,7 +33,7 @@ describe('Result', (): void => {
|
||||
expect(result.asErr.toU8a()).toEqual(new Uint8Array([4 << 2, 100, 101, 102, 103]));
|
||||
expect(result.toHex()).toEqual('0x011064656667');
|
||||
expect(result.toJSON()).toEqual({
|
||||
Err: hexToString('0x64656667')
|
||||
err: hexToString('0x64656667')
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ describe('createType', (): void => {
|
||||
it('allows creation of a Enum (with types)', (): void => {
|
||||
expect(
|
||||
createTypeUnsafe(registry, '{"_enum": {"A": null, "B": "u32", "C": null} }', [1]).toJSON()
|
||||
).toEqual({ B: 0 });
|
||||
).toEqual({ b: 0 });
|
||||
});
|
||||
|
||||
it('allows creation of a Enum (with indexes)', (): void => {
|
||||
@@ -72,7 +72,7 @@ describe('createType', (): void => {
|
||||
it('allows creation of a Result', (): void => {
|
||||
expect(
|
||||
createTypeUnsafe(registry, 'Result<u32,Text>', ['0x011064656667']).toJSON()
|
||||
).toEqual({ Err: 'defg' });
|
||||
).toEqual({ err: 'defg' });
|
||||
});
|
||||
|
||||
it('allows creation of a Set', (): void => {
|
||||
|
||||
@@ -11,7 +11,7 @@ describe('ExtrinsicEra', (): void => {
|
||||
const extrinsicEra = new ExtrinsicEra(registry, new Uint8Array([0]));
|
||||
|
||||
expect(extrinsicEra.asImmortalEra).toBeDefined();
|
||||
expect(extrinsicEra.toJSON()).toEqual({ ImmortalEra: '0x00' });
|
||||
expect(extrinsicEra.toJSON()).toEqual({ immortalEra: '0x00' });
|
||||
});
|
||||
|
||||
it('decodes an Extrinsic Era from u8 as mortal', (): void => {
|
||||
@@ -35,13 +35,13 @@ describe('ExtrinsicEra', (): void => {
|
||||
expect(extrinsicEra.asMortalEra.phase.toNumber()).toEqual(120);
|
||||
});
|
||||
|
||||
it('serializes and de-serializes from JSON', (): void => {
|
||||
it.only('serializes and de-serializes from JSON', (): void => {
|
||||
const extrinsicEra = new ExtrinsicEra(registry, new Uint8Array([78, 156]));
|
||||
const u8a = extrinsicEra.toU8a();
|
||||
const json = extrinsicEra.toJSON();
|
||||
|
||||
expect(u8a).toEqual(new Uint8Array([78, 156]));
|
||||
expect(json).toEqual({ MortalEra: '0x4e9c' });
|
||||
expect(json).toEqual({ mortalEra: '0x4e9c' });
|
||||
expect(new ExtrinsicEra(registry, json).toU8a()).toEqual(u8a);
|
||||
});
|
||||
|
||||
|
||||
@@ -218,11 +218,15 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
|
||||
? new Uint8Array([0])
|
||||
: new Uint8Array([1, value[0], value[1]]);
|
||||
} else if (isObject(value)) {
|
||||
const entries = Object.entries(value as MortalEnumDef).map(([k, v]): [string, any] => [k.toLowerCase(), v]);
|
||||
const mortal = entries.find(([k]) => k.toLowerCase() === 'mortalera');
|
||||
const immortal = entries.find(([k]) => k.toLowerCase() === 'immortalera');
|
||||
|
||||
// this is to de-serialize from JSON
|
||||
return (value as MortalEnumDef).MortalEra
|
||||
? { MortalEra: (value as MortalEnumDef).MortalEra }
|
||||
: (value as ImmortalEnumDef).ImmortalEra
|
||||
? { ImmortalEra: (value as ImmortalEnumDef).ImmortalEra }
|
||||
return mortal
|
||||
? { MortalEra: mortal[1] as string }
|
||||
: immortal
|
||||
? { ImmortalEra: immortal[1] as string }
|
||||
: { MortalEra: value };
|
||||
}
|
||||
|
||||
|
||||
@@ -20,14 +20,14 @@ describe('Registration', (): void => {
|
||||
deposit: 10000000000000, // '0x0000000000000000000009184e72a000',
|
||||
info: {
|
||||
additional: [],
|
||||
display: { Raw: '0x9911' },
|
||||
email: { Raw: '0x9933' },
|
||||
image: { None: null },
|
||||
legal: { Raw: '0x9922' },
|
||||
display: { raw: '0x9911' },
|
||||
email: { raw: '0x9933' },
|
||||
image: { none: null },
|
||||
legal: { raw: '0x9922' },
|
||||
pgpFingerprint: null,
|
||||
riot: { Raw: '0x9955' },
|
||||
twitter: { Raw: '0x9966' },
|
||||
web: { Raw: '0x9944' }
|
||||
riot: { raw: '0x9955' },
|
||||
twitter: { raw: '0x9966' },
|
||||
web: { raw: '0x9944' }
|
||||
},
|
||||
judgements: []
|
||||
});
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TypeRegistry } from '../../create';
|
||||
|
||||
describe('offchain definitions', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
describe('StorageKind', (): void => {
|
||||
it('has the correct indexes', (): void => {
|
||||
expect(registry.createType('StorageKind').index).toEqual(1);
|
||||
expect(registry.createType('StorageKind', 'LOCAL').index).toEqual(2);
|
||||
});
|
||||
|
||||
it('has a correct JSON for the type', (): void => {
|
||||
expect(registry.createType('StorageKind').toJSON()).toEqual('PERSISTENT');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user