Skip to content

Commit 874d232

Browse files
Merge pull request #60 from LimeChain/feature/map
Feature/map
2 parents c5b16a4 + f8ed8c4 commit 874d232

30 files changed

+504
-180
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The following table shows the status of the types and their arrays:
2424
| `Bool` |||
2525
| `Hash` || :heavy_minus_sign: |
2626
| `String` |||
27+
| `Map` || :heavy_minus_sign: |
2728

2829
The following table shows the status of the fixed width numbers:
2930

@@ -37,7 +38,6 @@ The following table shows the status of the fixed width numbers:
3738

3839
- **Compact Int** - [Documentation](https://substrate.dev/docs/en/knowledgebase/advanced/codec#compactgeneral-integers)
3940

40-
4141
## **Getting Started**
4242
*You can find more information on AssemblyScript and how to get started with it in the AssemblyScript docs -> [https://www.assemblyscript.org/introduction.html](https://www.assemblyscript.org/introduction.html)*
4343

@@ -61,6 +61,10 @@ Every type has а **toU8a** function. It encodes type value into an array of byt
6161
import { Bool, Byte, ScaleString, Hash, CompactInt } from "as-scale-codec"
6262
import { Int8, Int16, Int32, Int64 } from "as-scale-codec"
6363
import { UInt8, UInt16, UInt32, UInt64, UInt128 } from "as-scale-codec"
64+
// ScaleMap
65+
const scaleMap = new ScaleMap<Int32, Bool>();
66+
scaleMap.set(new Int32(1), new Bool(false));
67+
scaleMap.toU8a() // => [4, 1, 0, 0, 0, 0];
6468
6569
// Bool
6670
const scaleBool = new Bool(true);
@@ -117,7 +121,7 @@ scaleUInt128.toU8a() // => [0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
117121
Every type has a **static** function **fromU8a**. It decodes an array of bytes to the desired type
118122
119123
```jsx
120-
import { Bool, Byte, ScaleString, Hash, CompactInt } from "as-scale-codec"
124+
import { ScaleMap, Bool, Byte, ScaleString, Hash, CompactInt } from "as-scale-codec"
121125
import { Int8, Int16, Int32, Int64 } from "as-scale-codec"
122126
import { UInt8, UInt16, UInt32, UInt64, UInt128 } from "as-scale-codec"
123127
@@ -134,6 +138,10 @@ Byte.fromU8a([0x04, 0x61]); // => new ScaleString('a')
134138
Hash.fromU8a([0xff, 0x00, 0xab]);
135139
// => [0xff, 0x00, 0xab, 0x00, ... 0x00] (32 bytes long)
136140
141+
ScaleMap<Int32, Bool>.fromU8a([4, 1, 0, 0, 0, 0]);
142+
// => const scaleMap = new ScaleMap<Int32, Bool>()
143+
// => scaleMap.set(new Int32(1), new Bool(false))
144+
137145
// Compact Int
138146
CompactInt.fromU8a([0x04]); // => new CompactInt(1)
139147
@@ -256,6 +264,13 @@ const hashBytes: u8[] = [0xff, 0x00, 0xab];
256264
BytesReader.decodeInto<Hash>(hashBytes);
257265
// new Hash([0xff, 0x00, 0xab])
258266
267+
const mapBytes: u8[] = [2, 1, 0, 1, 0, 0, 0, 3, 0, 3, 0, 0, 0];
268+
// Read ScaleMap
269+
BytesReader.decodeInto<ScaleMap<UInt16, UInt32>>(mapBytes);
270+
// => const scaleMap = new ScaleMap<UInt16, UInt32>();
271+
// => scaleMap.set(new UInt16(1), new UInt32(1))
272+
// => scaleMap.set(new UInt16(3), new UInt32(3))
273+
259274
const cmpBytes: u8[] = [169, 2];
260275
// Read CompactInt
261276
BytesReader.decodeInto<CompactInt>(cmpBytes);

assembly/AbstractInt.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,41 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Codec } from "./interfaces/Codec";
15+
import { UnwrappableCodec } from "./interfaces/UnwrappableCodec";
1616
import { Bytes } from "./utils/Bytes";
1717

1818
/** Representation for a Int value in the system. */
19-
export abstract class AbstractInt<T extends number> implements Codec {
19+
export abstract class AbstractInt<T extends number> implements UnwrappableCodec<T> {
2020

2121
protected bitLength: i32;
2222
private _value: T;
23-
24-
get value(): T{
25-
return this._value;
26-
}
2723

2824
constructor (value: T, bitLength: i32) {
2925
this._value = value;
3026
this.bitLength = bitLength;
3127
}
3228

29+
/**
30+
* @description Returns the inner native value
31+
*/
32+
public unwrap(): T{
33+
return this._value;
34+
}
35+
3336
/** Encodes the value as u8[] as per the SCALE codec specification */
3437
public toU8a (): u8[] {
3538
let bytesEncoded = new Array<u8>(this.bitLength);
36-
Bytes.putUint<T>(bytesEncoded, this.value, this.bitLength);
39+
Bytes.putUint<T>(bytesEncoded, this.unwrap(), this.bitLength);
3740
return bytesEncoded;
3841
}
3942

43+
public eq(other: AbstractInt<T>): bool{
44+
return this.unwrap() == other.unwrap();
45+
}
46+
public notEq(other: AbstractInt<T>): bool{
47+
return this.unwrap() != other.unwrap();
48+
}
49+
4050
/**
4151
* @description Non-static constructor method used to populate defined properties of the model
4252
* @param bytes SCALE encoded bytes
@@ -51,7 +61,7 @@ export abstract class AbstractInt<T extends number> implements Codec {
5161
* @description Returns the string representation of the value
5262
*/
5363
toString (): string {
54-
return this.value.toString();
64+
return this.unwrap().toString();
5565
}
5666
/**
5767
* @description The length of Uint8Array when the value is encoded

assembly/Arrays/AbstractArray.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,35 @@
1313
// limitations under the License.
1414

1515
import { Codec } from "../interfaces/Codec";
16+
import { DecodedData } from "../interfaces/DecodedData";
17+
import { UnwrappableCodec } from "../interfaces/UnwrappableCodec";
18+
import { ArrayUtils } from "../utils/Arrays";
1619
import { Bytes } from "../utils/Bytes";
1720
import { BytesBuffer } from "../utils/BytesBuffer";
18-
import { DecodedData } from "../interfaces/DecodedData";
19-
import { CompactInt } from "..";
2021

21-
export abstract class AbstractArray<ScaleType extends Codec, NativeType> implements Codec{
22+
export abstract class AbstractArray<ScaleType extends Codec, NativeType> implements UnwrappableCodec<Array<NativeType>>{
2223

2324
public values: Array<NativeType>;
2425
constructor(input: NativeType[] = []) {
2526
this.values = new Array<NativeType>(input.length);
2627
Bytes.copy<NativeType>(input, this.values);
2728
}
29+
30+
/**
31+
* @description Returns the inner native value
32+
*/
33+
public unwrap(): Array<NativeType>{
34+
return this.values;
35+
}
2836

37+
public eq(other: AbstractArray<ScaleType, NativeType>): bool{
38+
return ArrayUtils.areArraysEqual(this.values, other.values);
39+
};
40+
41+
public notEq(other: AbstractArray<ScaleType, NativeType>): bool{
42+
return !ArrayUtils.areArraysEqual(this.values, other.values);
43+
};
44+
2945
/**
3046
* @description Encodes values of all elements in u8[] successively as per the SCALE codec specification
3147
*/

assembly/Arrays/BoolArray.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import { BytesReader, CompactInt } from "..";
16+
import { DecodedData } from "../interfaces/DecodedData";
1517
import { Bool } from "./../Bool";
16-
import { AbstractArray } from "./AbstractArray"
18+
import { AbstractArray } from "./AbstractArray";
1719

18-
import { DecodedData } from "../interfaces/DecodedData";
19-
import { ArrayUtils } from "../utils/Arrays";
20-
import { BytesReader, CompactInt } from "..";
2120

2221
// @ts-ignore
2322
export class BoolArray extends AbstractArray<Bool, bool> {
@@ -29,7 +28,7 @@ export class BoolArray extends AbstractArray<Bool, bool> {
2928
const scaleBool = Bool.fromU8a([value[0]]);
3029

3130
return new DecodedData<bool>(
32-
scaleBool.value,
31+
scaleBool.unwrap(),
3332
scaleBool.encodedLength()
3433
)
3534
}
@@ -40,7 +39,7 @@ export class BoolArray extends AbstractArray<Bool, bool> {
4039
public encodedLength(): i32{
4140
return (new CompactInt(this.values.length).encodedLength()) + super.values.length;
4241
}
43-
42+
4443
/**
4544
* @description Non-static constructor method used to populate defined properties of the model
4645
* @param bytes SCALE encoded bytes
@@ -50,9 +49,9 @@ export class BoolArray extends AbstractArray<Bool, bool> {
5049
const bytesReader = new BytesReader(bytes.slice(index));
5150
const data = bytesReader.readInto<CompactInt>();
5251

53-
for(let i: i32 = 0; i < data.value - index; i++){
52+
for(let i: i32 = 0; i < data.unwrap() - index; i++){
5453
const element = bytesReader.readInto<Bool>();
55-
this.values.push(element.value);
54+
this.values.push(element.unwrap());
5655
}
5756
}
5857
/**
@@ -64,11 +63,11 @@ export class BoolArray extends AbstractArray<Bool, bool> {
6463

6564
@inline @operator('==')
6665
static eq(a: BoolArray, b: BoolArray): bool {
67-
return ArrayUtils.areEqual(a, b);
66+
return a.eq(b);
6867
}
6968

7069
@inline @operator('!=')
7170
static notEq(a: BoolArray, b: BoolArray): bool {
72-
return !ArrayUtils.areEqual(a, b);
71+
return a.notEq(b);
7372
}
7473
}

assembly/Arrays/ByteArray.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import { BytesReader } from "..";
1516
import { Byte } from "../Byte";
1617
import { CompactInt } from "../Int";
17-
import { AbstractArray } from "./AbstractArray";
1818
import { DecodedData } from "../interfaces/DecodedData";
19-
import { ArrayUtils } from "../utils/Arrays";
20-
import { BytesReader } from "..";
19+
import { AbstractArray } from "./AbstractArray";
2120

2221
// @ts-ignore
2322
export class ByteArray extends AbstractArray<Byte, u8> {
@@ -45,7 +44,7 @@ export class ByteArray extends AbstractArray<Byte, u8> {
4544
const scaleByte = Byte.fromU8a([value[0]]);
4645

4746
return new DecodedData<u8>(
48-
scaleByte.value,
47+
scaleByte.unwrap(),
4948
scaleByte.encodedLength()
5049
)
5150
}
@@ -66,9 +65,9 @@ export class ByteArray extends AbstractArray<Byte, u8> {
6665
const bytesReader = new BytesReader(bytes.slice(index));
6766
const data = bytesReader.readInto<CompactInt>();
6867

69-
for(let i: i32 = 0; i < data.value; i++){
68+
for(let i: i32 = 0; i < data.unwrap(); i++){
7069
const element: Byte = bytesReader.readInto<Byte>();
71-
this.values.push(element.value);
70+
this.values.push(element.unwrap());
7271
}
7372
}
7473
/**
@@ -80,13 +79,12 @@ export class ByteArray extends AbstractArray<Byte, u8> {
8079

8180
@inline @operator('==')
8281
static eq(a: ByteArray, b: ByteArray): bool {
83-
return ArrayUtils.areEqual(a, b);
82+
return a.eq(b);
8483
}
8584

8685
@inline @operator('!=')
8786
static notEq(a: ByteArray, b: ByteArray): bool {
88-
return !ArrayUtils.areEqual(a, b);
87+
return a.notEq(b);
8988
}
90-
9189
}
9290

assembly/Arrays/IntArray.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import { BytesReader } from "..";
1516
import { CompactInt } from "../Int/CompactInt";
17+
import { DecodedData } from "../interfaces/DecodedData";
1618
import { AbstractArray } from "./AbstractArray";
1719

18-
import { DecodedData } from "../interfaces/DecodedData";
19-
import { ArrayUtils } from "../utils/Arrays";
20-
import { BytesReader } from "..";
2120

2221
// @ts-ignore
2322
export class IntArray extends AbstractArray<CompactInt, i64> {
@@ -29,7 +28,7 @@ export class IntArray extends AbstractArray<CompactInt, i64> {
2928
const compactInt = CompactInt.fromU8a(value);
3029

3130
return new DecodedData<u64>(
32-
compactInt.value,
31+
compactInt.unwrap(),
3332
compactInt.encodedLength()
3433
)
3534
}
@@ -54,9 +53,9 @@ export class IntArray extends AbstractArray<CompactInt, i64> {
5453
const bytesReader = new BytesReader(bytes.slice(index));
5554
const data = bytesReader.readInto<CompactInt>();
5655

57-
for(let i: i32 = 0; i < data.value; i++){
56+
for(let i: i32 = 0; i < data.unwrap(); i++){
5857
const element: CompactInt = bytesReader.readInto<CompactInt>();
59-
this.values.push(element.value);
58+
this.values.push(element.unwrap());
6059
}
6160
}
6261
/**
@@ -68,12 +67,12 @@ export class IntArray extends AbstractArray<CompactInt, i64> {
6867

6968
@inline @operator('==')
7069
static eq(a: IntArray, b: IntArray): bool {
71-
return ArrayUtils.areEqual(a, b);
70+
return a.eq(b);
7271
}
7372

7473
@inline @operator('!=')
7574
static notEq(a: IntArray, b: IntArray): bool {
76-
return !ArrayUtils.areEqual(a, b);
75+
return a.notEq(b);
7776
}
7877
}
7978

assembly/Arrays/StringArray.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import { BytesReader, CompactInt } from "..";
16+
import { DecodedData } from "../interfaces/DecodedData";
17+
import { ScaleString } from "../ScaleString";
1518
import { Bytes } from "../utils/Bytes";
1619
import { AbstractArray } from "./AbstractArray";
17-
import { ScaleString } from "../ScaleString";
1820

19-
import { DecodedData } from "../interfaces/DecodedData";
20-
import { ArrayUtils } from "../utils/Arrays";
21-
import { BytesReader, CompactInt } from "..";
2221

2322
// @ts-ignore
2423
export class StringArray extends AbstractArray<ScaleString, string>{
@@ -31,7 +30,7 @@ export class StringArray extends AbstractArray<ScaleString, string>{
3130
const encodedStringLength = i32(stringLength.decBytes + stringLength.value);
3231

3332
return new DecodedData<string>(
34-
ScaleString.fromU8a(value.slice(0, encodedStringLength)).valueStr,
33+
ScaleString.fromU8a(value.slice(0, encodedStringLength)).toString(),
3534
encodedStringLength
3635
)
3736
}
@@ -57,9 +56,9 @@ export class StringArray extends AbstractArray<ScaleString, string>{
5756
const bytesReader = new BytesReader(bytes.slice(index));
5857
const data = bytesReader.readInto<CompactInt>();
5958

60-
for(let i: i32 = 0; i < data.value; i++){
59+
for(let i: i32 = 0; i < data.unwrap(); i++){
6160
const element: ScaleString = bytesReader.readInto<ScaleString>();
62-
this.values.push(element.valueStr);
61+
this.values.push(element.toString());
6362
}
6463
}
6564

@@ -70,14 +69,13 @@ export class StringArray extends AbstractArray<ScaleString, string>{
7069
return AbstractArray.fromU8a<StringArray>(input);
7170
}
7271

73-
7472
@inline @operator('==')
7573
static eq(a: StringArray, b: StringArray): bool {
76-
return ArrayUtils.areEqual(a, b);
74+
return a.eq(b);
7775
}
7876

7977
@inline @operator('!=')
8078
static notEq(a: StringArray, b: StringArray): bool {
81-
return !ArrayUtils.areEqual(a, b);
79+
return a.notEq(b);
8280
}
8381
}

0 commit comments

Comments
 (0)