Skip to content

Commit

Permalink
Merge pull request #1 from strategyobject/feature/scale-unions
Browse files Browse the repository at this point in the history
Adds tagged union (rust enum) support for scale codec implementation
  • Loading branch information
kalaninja authored Nov 10, 2021
2 parents 8bce3cc + ce8be4b commit c864e80
Show file tree
Hide file tree
Showing 82 changed files with 4,397 additions and 2 deletions.
1 change: 0 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ api.pallete(MyPallet.class)
- [x] Transport - layer that interacts with a node. It provides async API for RPC requests.
- [ ] Scale
- [x] Scale codec - implementation of the [SCALE](https://docs.substrate.io/v3/advanced/scale-codec/) for standard types.
- [ ] Support scale for *union* types
- [x] Scale code generation - approach to generate scale encoders/decoders for annotated classes.
- [ ] Signing:
- [x] SR25519
Expand Down
19 changes: 19 additions & 0 deletions scale/scale-codegen/src/test/resources/Annotated.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public class Annotated<T1, T2, T3> {
@Scale(U128.class)
private BigInteger testU128;

@ScaleGeneric(
template = "Union<I32, Bool, String>",
types = {
@Scale(value = Union3.class, name = "Union"),
@Scale(I32.class),
@Scale(Bool.class),
@Scale(ScaleType.String.class)
}
)
private com.strategyobject.substrateclient.types.union.Union3<Integer, Boolean, String> testUnion;

@ScaleGeneric(
template = "Vec<i32>",
types = {
Expand Down Expand Up @@ -235,6 +246,14 @@ public void setTestU128(BigInteger testU128) {
this.testU128 = testU128;
}

public com.strategyobject.substrateclient.types.union.Union3<Integer, Boolean, String> getTestUnion() {
return testUnion;
}

public void setTestUnion(com.strategyobject.substrateclient.types.union.Union3<Integer, Boolean, String> testUnion) {
this.testUnion = testUnion;
}

public List<Integer> getTestVec() {
return testVec;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ class U128 implements ScaleType {
class Vec<T> implements ScaleType {
}

class Union implements ScaleType {
class Union1<T1> implements ScaleType {
}

class Union2<T1, T2> implements ScaleType {
}

class Union3<T1, T2, T3> implements ScaleType {
}

class Union4<T1, T2, T3, T4> implements ScaleType {
}

class Union5<T1, T2, T3, T4, T5> implements ScaleType {
}

class Union6<T1, T2, T3, T4, T5, T6> implements ScaleType {
}

class Union7<T1, T2, T3, T4, T5, T6, T7> implements ScaleType {
}

class Union8<T1, T2, T3, T4, T5, T6, T7, T8> implements ScaleType {
}

class Union9<T1, T2, T3, T4, T5, T6, T7, T8, T9> implements ScaleType {
}

class Union10<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> implements ScaleType {
}

class Union11<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> implements ScaleType {
}

class Union12<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> implements ScaleType {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.google.common.base.Preconditions;
import com.strategyobject.substrateclient.common.io.Streamer;
import com.strategyobject.substrateclient.scale.ScaleReader;
import com.strategyobject.substrateclient.types.union.Union;
import lombok.NonNull;
import lombok.val;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.function.Function;

public abstract class BaseUnionReader<T extends Union> implements ScaleReader<T> {
private final int unionSize;
@SuppressWarnings("rawtypes")
private final Function[] createUnion;

@SafeVarargs
protected BaseUnionReader(int unionSize, Function<?, T>... createUnion) {
Preconditions.checkArgument(createUnion.length == unionSize);

this.unionSize = unionSize;
this.createUnion = createUnion;
}

@SuppressWarnings("unchecked")
@Override
public T read(@NonNull InputStream stream, ScaleReader<?>... readers) throws IOException {
Preconditions.checkArgument(readers.length == unionSize);
Arrays.stream(readers).forEach(Preconditions::checkNotNull);

val index = Streamer.readByte(stream);
if (index >= unionSize) {
throw new NoSuchElementException("Union index is out of bound.");
}

return (T) createUnion[index].apply(readers[index].read(stream));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union10;

public class Union10Reader extends BaseUnionReader<Union10<?, ?, ?, ?, ?, ?, ?, ?, ?, ?>> {
public Union10Reader() {
super(10,
Union10::withItem0,
Union10::withItem1,
Union10::withItem2,
Union10::withItem3,
Union10::withItem4,
Union10::withItem5,
Union10::withItem6,
Union10::withItem7,
Union10::withItem8,
Union10::withItem9);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union11;

public class Union11Reader extends BaseUnionReader<Union11<?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?>> {
public Union11Reader() {
super(11,
Union11::withItem0,
Union11::withItem1,
Union11::withItem2,
Union11::withItem3,
Union11::withItem4,
Union11::withItem5,
Union11::withItem6,
Union11::withItem7,
Union11::withItem8,
Union11::withItem9,
Union11::withItem10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union12;

public class Union12Reader extends BaseUnionReader<Union12<?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?>> {
public Union12Reader() {
super(12,
Union12::withItem0,
Union12::withItem1,
Union12::withItem2,
Union12::withItem3,
Union12::withItem4,
Union12::withItem5,
Union12::withItem6,
Union12::withItem7,
Union12::withItem8,
Union12::withItem9,
Union12::withItem10,
Union12::withItem11);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union1;

public class Union1Reader extends BaseUnionReader<Union1<?>> {
public Union1Reader() {
super(1, Union1::withItem0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union2;

public class Union2Reader extends BaseUnionReader<Union2<?, ?>> {
public Union2Reader() {
super(2, Union2::withItem0, Union2::withItem1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union3;

public class Union3Reader extends BaseUnionReader<Union3<?, ?, ?>> {
public Union3Reader() {
super(3,
Union3::withItem0,
Union3::withItem1,
Union3::withItem2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union4;

public class Union4Reader extends BaseUnionReader<Union4<?, ?, ?, ?>> {
public Union4Reader() {
super(4,
Union4::withItem0,
Union4::withItem1,
Union4::withItem2,
Union4::withItem3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union5;

public class Union5Reader extends BaseUnionReader<Union5<?, ?, ?, ?, ?>> {
public Union5Reader() {
super(5,
Union5::withItem0,
Union5::withItem1,
Union5::withItem2,
Union5::withItem3,
Union5::withItem4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union6;

public class Union6Reader extends BaseUnionReader<Union6<?, ?, ?, ?, ?, ?>> {
public Union6Reader() {
super(6,
Union6::withItem0,
Union6::withItem1,
Union6::withItem2,
Union6::withItem3,
Union6::withItem4,
Union6::withItem5);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union7;

public class Union7Reader extends BaseUnionReader<Union7<?, ?, ?, ?, ?, ?, ?>> {
public Union7Reader() {
super(7,
Union7::withItem0,
Union7::withItem1,
Union7::withItem2,
Union7::withItem3,
Union7::withItem4,
Union7::withItem5,
Union7::withItem6);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union8;

public class Union8Reader extends BaseUnionReader<Union8<?, ?, ?, ?, ?, ?, ?, ?>> {
public Union8Reader() {
super(8,
Union8::withItem0,
Union8::withItem1,
Union8::withItem2,
Union8::withItem3,
Union8::withItem4,
Union8::withItem5,
Union8::withItem6,
Union8::withItem7);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.strategyobject.substrateclient.scale.readers.union;

import com.strategyobject.substrateclient.types.union.Union9;

public class Union9Reader extends BaseUnionReader<Union9<?, ?, ?, ?, ?, ?, ?, ?, ?>> {
public Union9Reader() {
super(9,
Union9::withItem0,
Union9::withItem1,
Union9::withItem2,
Union9::withItem3,
Union9::withItem4,
Union9::withItem5,
Union9::withItem6,
Union9::withItem7,
Union9::withItem8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.strategyobject.substrateclient.scale.ScaleType;
import com.strategyobject.substrateclient.scale.annotations.AutoRegister;
import com.strategyobject.substrateclient.scale.readers.*;
import com.strategyobject.substrateclient.scale.readers.union.*;
import com.strategyobject.substrateclient.types.Result;
import com.strategyobject.substrateclient.types.union.*;
import lombok.NonNull;
import lombok.val;
import org.slf4j.Logger;
Expand Down Expand Up @@ -43,6 +45,18 @@ private ScaleReaderRegistry() {
register(new U32Reader(), ScaleType.U32.class);
register(new U64Reader(), ScaleType.U64.class);
register(new U128Reader(), ScaleType.U128.class);
register(new Union1Reader(), Union1.class, ScaleType.Union1.class);
register(new Union2Reader(), Union2.class, ScaleType.Union2.class);
register(new Union3Reader(), Union3.class, ScaleType.Union3.class);
register(new Union4Reader(), Union4.class, ScaleType.Union4.class);
register(new Union5Reader(), Union5.class, ScaleType.Union5.class);
register(new Union6Reader(), Union6.class, ScaleType.Union6.class);
register(new Union7Reader(), Union7.class, ScaleType.Union7.class);
register(new Union8Reader(), Union8.class, ScaleType.Union8.class);
register(new Union9Reader(), Union9.class, ScaleType.Union9.class);
register(new Union10Reader(), Union10.class, ScaleType.Union10.class);
register(new Union11Reader(), Union11.class, ScaleType.Union11.class);
register(new Union12Reader(), Union12.class, ScaleType.Union12.class);
register(new VecReader(), ScaleType.Vec.class, List.class);
register(new VoidReader(), Void.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import com.strategyobject.substrateclient.scale.ScaleWriter;
import com.strategyobject.substrateclient.scale.annotations.AutoRegister;
import com.strategyobject.substrateclient.scale.writers.*;
import com.strategyobject.substrateclient.scale.writers.union.*;
import com.strategyobject.substrateclient.types.PublicKey;
import com.strategyobject.substrateclient.types.Result;
import com.strategyobject.substrateclient.types.SignatureData;
import com.strategyobject.substrateclient.types.union.*;
import lombok.NonNull;
import lombok.val;
import org.slf4j.Logger;
Expand Down Expand Up @@ -46,6 +48,18 @@ private ScaleWriterRegistry() {
register(new U32Writer(), ScaleType.U32.class);
register(new U64Writer(), ScaleType.U64.class);
register(new U128Writer(), ScaleType.U128.class);
register(new Union1Writer(), Union1.class, ScaleType.Union1.class);
register(new Union2Writer(), Union2.class, ScaleType.Union2.class);
register(new Union3Writer(), Union3.class, ScaleType.Union3.class);
register(new Union4Writer(), Union4.class, ScaleType.Union4.class);
register(new Union5Writer(), Union5.class, ScaleType.Union5.class);
register(new Union6Writer(), Union6.class, ScaleType.Union6.class);
register(new Union7Writer(), Union7.class, ScaleType.Union7.class);
register(new Union8Writer(), Union8.class, ScaleType.Union8.class);
register(new Union9Writer(), Union9.class, ScaleType.Union9.class);
register(new Union10Writer(), Union10.class, ScaleType.Union10.class);
register(new Union11Writer(), Union11.class, ScaleType.Union11.class);
register(new Union12Writer(), Union12.class, ScaleType.Union12.class);
register(new VecWriter(), ScaleType.Vec.class, List.class);
register(new VoidWriter(), Void.class);
register(new SelfWriter(), ScaleSelfWritable.class);
Expand Down
Loading

0 comments on commit c864e80

Please sign in to comment.