Skip to content

Commit 1e09c79

Browse files
authored
Merge pull request #10 from strategyobject/feature/rpc-contracts
Rpc contracts
2 parents a03ef19 + edbb6af commit 1e09c79

56 files changed

Lines changed: 612 additions & 169 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ subprojects {
2828

2929
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
3030
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.2'
31+
testImplementation 'org.assertj:assertj-core:3.22.0'
3132
testImplementation 'org.mockito:mockito-core:3.12.4'
3233
testImplementation 'org.mockito:mockito-inline:3.12.4'
34+
testImplementation 'org.slf4j:slf4j-simple:1.7.32'
3335

3436
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
3537
}

common/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dependencies {
2-
implementation 'org.reflections:reflections:0.10.1'
2+
implementation 'org.reflections:reflections:0.10.2'
33
implementation 'com.squareup:javapoet:1.13.0'
44
}

rpc/rpc-codegen/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ dependencies {
55
implementation project(':scale:scale-codegen')
66
implementation project(':transport')
77

8-
compileOnly 'com.google.auto.service:auto-service-annotations:1.0'
9-
annotationProcessor 'com.google.auto.service:auto-service:1.0'
8+
compileOnly 'com.google.auto.service:auto-service-annotations:1.0.1'
9+
annotationProcessor 'com.google.auto.service:auto-service:1.0.1'
1010

1111
implementation 'com.squareup:javapoet:1.13.0'
1212
testImplementation 'com.google.testing.compile:compile-testing:0.19'
13-
testImplementation 'com.google.code.gson:gson:2.8.8'
13+
testImplementation 'com.google.code.gson:gson:2.8.9'
1414

1515
testCompileOnly project(':rpc:rpc-codegen')
1616
testAnnotationProcessor project(':rpc:rpc-codegen')

rpc/rpc-codegen/src/main/java/com/strategyobject/substrateclient/rpc/codegen/decoder/RpcDecoderAnnotatedClass.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.strategyobject.substrateclient.scale.codegen.ScaleAnnotationParser;
1717
import com.strategyobject.substrateclient.scale.codegen.reader.ReaderCompositor;
1818
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry;
19+
import com.strategyobject.substrateclient.transport.RpcObject;
1920
import lombok.NonNull;
2021
import lombok.val;
2122
import lombok.var;
@@ -83,14 +84,15 @@ private MethodSpec generateDecodeMethod(ProcessorContext context, TypeName class
8384
.addAnnotation(suppressWarnings("unchecked"))
8485
.addModifiers(Modifier.PUBLIC)
8586
.returns(classWildcardTyped)
86-
.addParameter(Object.class, VALUE_ARG)
87+
.addParameter(RpcObject.class, VALUE_ARG)
8788
.addParameter(ArrayTypeName.of(
8889
ParameterizedTypeName.get(
8990
ClassName.get(DecoderPair.class),
9091
WildcardTypeName.subtypeOf(Object.class))),
9192
DECODERS_ARG)
9293
.varargs(true);
9394

95+
shortcutIfNull(methodSpec);
9496
addValidationRules(methodSpec);
9597
addMethodBody(methodSpec, context);
9698

@@ -100,10 +102,9 @@ private MethodSpec generateDecodeMethod(ProcessorContext context, TypeName class
100102
private void addMethodBody(MethodSpec.Builder methodSpec, ProcessorContext context) throws ProcessingException {
101103
val resultType = JavaPoet.setEachGenericParameterAs(classElement, TypeName.OBJECT);
102104
methodSpec
103-
.addStatement("if ($L == null) { return null; }", VALUE_ARG)
104105
.addStatement("$1T $2L = $1T.getInstance()", RpcDecoderRegistry.class, DECODER_REGISTRY)
105106
.addStatement("$1T $2L = $1T.getInstance()", ScaleReaderRegistry.class, SCALE_READER_REGISTRY)
106-
.addStatement("$1T<$2T, ?> $3L = ($1T<$2T, ?>)$4L", Map.class, String.class, MAP_VAR, VALUE_ARG)
107+
.addStatement("$T<$T, $T> $L = $L.asMap()", Map.class, String.class, RpcObject.class, MAP_VAR, VALUE_ARG)
107108
.addStatement("$1T $2L = new $1T()", resultType, RESULT_VAR)
108109
.beginControlFlow("try");
109110

@@ -166,13 +167,8 @@ private void setScaleField(MethodSpec.Builder methodSpec,
166167
readerCompositor.traverse(fieldType);
167168
methodSpec
168169
.addStatement(code
169-
.add("$T.$L(",
170-
ScaleUtils.class,
171-
FROM_HEX_STRING)
172-
.add("($T)$L.get($S), ",
173-
String.class,
174-
MAP_VAR,
175-
field)
170+
.add("$T.$L(", ScaleUtils.class, FROM_HEX_STRING)
171+
.add("$L.get($S).asString(), ", MAP_VAR, field)
176172
.add("($T)", ScaleReader.class)
177173
.add(readerCode)
178174
.add("))")
@@ -225,4 +221,8 @@ private void addValidationRules(MethodSpec.Builder methodSpec) {
225221
}
226222
}
227223
}
224+
225+
private void shortcutIfNull(MethodSpec.Builder methodSpec) {
226+
methodSpec.addStatement("if ($L.isNull()) { return null; }", VALUE_ARG);
227+
}
228228
}

rpc/rpc-codegen/src/main/java/com/strategyobject/substrateclient/rpc/codegen/encoder/RpcEncoderAnnotatedClass.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private MethodSpec generateEncodeMethod(ProcessorContext context, TypeName class
9292
ENCODERS_ARG)
9393
.varargs(true);
9494

95+
shortcutIfNull(methodSpec);
9596
addValidationRules(methodSpec, context);
9697
addMethodBody(methodSpec, context);
9798

@@ -100,7 +101,6 @@ private MethodSpec generateEncodeMethod(ProcessorContext context, TypeName class
100101

101102
private void addMethodBody(MethodSpec.Builder methodSpec, ProcessorContext context) throws ProcessingException {
102103
methodSpec
103-
.addStatement("if ($L == null) { return null; }", SOURCE_ARG)
104104
.addStatement("$1T $2L = $1T.getInstance()", RpcEncoderRegistry.class, ENCODER_REGISTRY)
105105
.addStatement("$1T $2L = $1T.getInstance()", ScaleWriterRegistry.class, SCALE_WRITER_REGISTRY)
106106
.addStatement("$1T<$2T, $3T> $4L = new $1T<>()", HashMap.class, String.class, Object.class, RESULT_VAR)
@@ -223,4 +223,8 @@ private void addValidationRules(MethodSpec.Builder methodSpec, ProcessorContext
223223
}
224224
}
225225
}
226+
227+
private void shortcutIfNull(MethodSpec.Builder methodSpec) {
228+
methodSpec.addStatement("if ($L == null) { return null; }", SOURCE_ARG);
229+
}
226230
}

rpc/rpc-codegen/src/main/java/com/strategyobject/substrateclient/rpc/codegen/sections/RpcMethodProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private CodeBlock getScaleReadCodeBlock(AnnotatedConstruct annotated,
158158
return CodeBlock.builder()
159159
.add("($T) (", resultType)
160160
.add("$T.$L(", ScaleUtils.class, FROM_HEX_STRING)
161-
.add("($T) $L, ($T) ", String.class, arg, ScaleReader.class)
161+
.add("$L.asString(), ($T) ", arg, ScaleReader.class)
162162
.add(readerCode)
163163
.add("))")
164164
.build();

rpc/rpc-codegen/src/main/java/com/strategyobject/substrateclient/rpc/codegen/sections/RpcSubscriptionProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.strategyobject.substrateclient.common.codegen.ProcessingException;
77
import com.strategyobject.substrateclient.common.codegen.ProcessorContext;
88
import com.strategyobject.substrateclient.rpc.core.annotations.RpcSubscription;
9+
import com.strategyobject.substrateclient.transport.RpcObject;
910
import lombok.NonNull;
1011
import lombok.val;
1112

@@ -58,7 +59,7 @@ protected void callProviderInterface(MethodSpec.Builder methodSpecBuilder,
5859
.add("$1T<$2T, $3T> $4L = ($5L, $6L) -> { $7N.$8L($5L, ",
5960
BiConsumer.class,
6061
Exception.class,
61-
Object.class,
62+
RpcObject.class,
6263
CALL_BACK_PROXY,
6364
CALL_BACK_EX_ARG,
6465
CALL_BACK_ARG,

rpc/rpc-codegen/src/test/java/com/strategyobject/substrateclient/rpc/codegen/decoder/RpcDecoderProcessorTests.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
import com.strategyobject.substrateclient.rpc.codegen.substitutes.TestDecodable;
66
import com.strategyobject.substrateclient.rpc.core.DecoderPair;
77
import com.strategyobject.substrateclient.rpc.core.registries.RpcDecoderRegistry;
8+
import com.strategyobject.substrateclient.transport.RpcObject;
89
import lombok.val;
910
import org.junit.jupiter.api.Test;
1011

11-
import java.util.AbstractMap;
1212
import java.util.Arrays;
13-
import java.util.Map;
14-
import java.util.stream.Collectors;
15-
import java.util.stream.Stream;
13+
import java.util.HashMap;
1614

1715
import static com.google.testing.compile.CompilationSubject.assertThat;
1816
import static com.google.testing.compile.Compiler.javac;
@@ -62,23 +60,50 @@ void compilesAndDecodes() { // TODO move this test out of the project
6260
val decoder = registry.resolve(TestDecodable.class)
6361
.inject(DecoderPair.of(registry.resolve(String.class), null));
6462

65-
Object source = gson.fromJson("{\"a\":4,\"b\":\"123\",\"c\":\"some\"," +
66-
"\"d\":[\"1\",\"2\"],\"e\":{\"a\":1,\"b\":2},\"f\":\"0x04000000\"," +
67-
"\"g\":\"0x0c0500000002000000fdffffff\"}",
68-
Object.class);
63+
/*
64+
{
65+
"a": 4,
66+
"b": "123",
67+
"c": "some",
68+
"d": [
69+
"1",
70+
"2"
71+
],
72+
"e": {
73+
"a": null,
74+
"b": 2
75+
},
76+
"f": "0x04000000",
77+
"g": "0x0c0500000002000000fdffffff",
78+
"h": 1.5,
79+
}
80+
*/
81+
val source = RpcObject.of(new HashMap<String, RpcObject>() {{
82+
put("a", RpcObject.of(4));
83+
put("b", RpcObject.of("123"));
84+
put("c", RpcObject.of("some"));
85+
put("d", RpcObject.of(Arrays.asList(RpcObject.of("1"), RpcObject.of("2"))));
86+
put("e", RpcObject.of(new HashMap<String, RpcObject>() {{
87+
put("a", RpcObject.ofNull());
88+
put("b", RpcObject.of(2));
89+
}}));
90+
put("f", RpcObject.of("0x04000000"));
91+
put("g", RpcObject.of("0x0c0500000002000000fdffffff"));
92+
put("h", RpcObject.of(1.5));
93+
}});
94+
val actual = decoder.decode(source);
95+
6996
val expected = new TestDecodable<>(4,
7097
"123",
7198
"some",
7299
Arrays.asList("1", "2"),
73-
Stream.of(
74-
new AbstractMap.SimpleEntry<>("a", 1),
75-
new AbstractMap.SimpleEntry<>("b", 2))
76-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
100+
new HashMap<String, Float>() {{
101+
put("a", null);
102+
put("b", 2f);
103+
}},
77104
4,
78-
Arrays.asList(5, 2, -3));
79-
80-
val actual = decoder.decode(source);
81-
105+
Arrays.asList(5, 2, -3),
106+
1.5f);
82107
assertEquals(gson.toJson(expected), gson.toJson(actual));
83108
}
84109
}

rpc/rpc-codegen/src/test/java/com/strategyobject/substrateclient/rpc/codegen/sections/RpcSectionFactoryTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.strategyobject.substrateclient.rpc.codegen.substitutes.TestSection;
44
import com.strategyobject.substrateclient.transport.ProviderInterface;
5+
import com.strategyobject.substrateclient.transport.RpcBoolean;
6+
import com.strategyobject.substrateclient.transport.RpcObject;
57
import lombok.val;
68
import org.junit.jupiter.api.Test;
79

@@ -19,7 +21,7 @@ public class RpcSectionFactoryTest {
1921
@Test
2022
void createsRpcSectionAndCallsMethod() throws ExecutionException, InterruptedException, RpcInterfaceInitializationException {
2123
val expected = true;
22-
val sendFuture = CompletableFuture.completedFuture((Object) Boolean.valueOf(expected));
24+
val sendFuture = CompletableFuture.completedFuture((RpcObject) new RpcBoolean(expected));
2325
val provider = mock(ProviderInterface.class);
2426
when(provider.send(anyString(), anyList()))
2527
.thenReturn(sendFuture);

rpc/rpc-codegen/src/test/java/com/strategyobject/substrateclient/rpc/codegen/substitutes/TestDecodable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public class TestDecodable<T> {
2020
private String b;
2121
private T c;
2222
private List<String> d;
23-
private Map<String, Integer> e;
23+
private Map<String, Float> e;
2424
@Scale
2525
private int f;
2626
@Scale
2727
private List<Integer> g;
28+
private float h;
2829
}

0 commit comments

Comments
 (0)