Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Constants {

public static final String AUTO_REGISTER_TYPES_ARG = "types";
public static final String PAIR_FACTORY_METHOD = "of";
public static final String RESOLVE_AND_INJECT_METHOD = "resolveAndInjectOrNull";

public static final String ENCODE_METHOD_NAME = "encode";
public static final String DECODE_METHOD_NAME = "decode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.strategyobject.substrateclient.common.codegen.TypeTraverser;
import com.strategyobject.substrateclient.rpc.core.DecoderPair;
import com.strategyobject.substrateclient.rpc.core.RpcDecoder;
import com.strategyobject.substrateclient.rpc.core.RpcRegistryHelper;
import com.strategyobject.substrateclient.scale.ScaleReader;
import com.strategyobject.substrateclient.scale.ScaleRegistryHelper;
import lombok.NonNull;
import lombok.var;

Expand All @@ -16,26 +18,30 @@
import java.util.Map;

import static com.strategyobject.substrateclient.rpc.codegen.Constants.PAIR_FACTORY_METHOD;
import static com.strategyobject.substrateclient.rpc.codegen.Constants.RESOLVE_AND_INJECT_METHOD;

public class DecoderCompositor extends TypeTraverser<CodeBlock> {
private final Types typeUtils;
private final Map<String, Integer> typeVarMap;
private final String decoderAccessor;
private final String readerAccessor;
private final String readerMethod;
private final String decoderRegistryVarName;
private final String scaleRegistryVarName;

public DecoderCompositor(@NonNull Types typeUtils,
@NonNull Map<String, Integer> typeVarMap,
@NonNull String decoderAccessor,
@NonNull String readerAccessor,
@NonNull String readerMethod,
@NonNull String decoderRegistryVarName,
@NonNull String scaleRegistryVarName) {
super(CodeBlock.class);
this.typeUtils = typeUtils;
this.typeVarMap = typeVarMap;
this.decoderAccessor = decoderAccessor;
this.readerAccessor = readerAccessor;
this.readerMethod = readerMethod;
this.decoderRegistryVarName = decoderRegistryVarName;
this.scaleRegistryVarName = scaleRegistryVarName;
}
Expand Down Expand Up @@ -77,16 +83,17 @@ protected CodeBlock whenGenericType(@NonNull DeclaredType type, TypeMirror _over

var builder = CodeBlock.builder()
.add("$T.$L(", DecoderPair.class, PAIR_FACTORY_METHOD)
.add("($T) $L.resolve($T.class).inject(", RpcDecoder.class, decoderRegistryVarName, resolveType);
.add("$T.$L($T.class, ", RpcRegistryHelper.class, RESOLVE_AND_INJECT_METHOD, resolveType);
for (var i = 0; i < subtypes.length; i++) {
if (i > 0) builder.add(", ");
builder.add(subtypes[i]);
}

builder.add("), ($T) $L.resolve($T.class).inject(", ScaleReader.class, scaleRegistryVarName, resolveType);
builder.add("), $T.$L($T.class, ", ScaleRegistryHelper.class, RESOLVE_AND_INJECT_METHOD, resolveType);
for (var i = 0; i < subtypes.length; i++) {
if (i > 0) builder.add(", ");
builder.add(subtypes[i]);
builder.add(".$L", readerMethod);
}
builder.add(")");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private void setFields(MethodSpec.Builder methodSpec, ProcessorContext context)
typeVarMap,
String.format("%s[$L].%s", DECODERS_ARG, DECODER_UNSAFE_ACCESSOR),
String.format("%s[$L].%s", DECODERS_ARG, READER_UNSAFE_ACCESSOR),
READER_UNSAFE_ACCESSOR,
DECODER_REGISTRY,
SCALE_READER_REGISTRY);
val scaleAnnotationParser = new ScaleAnnotationParser(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.strategyobject.substrateclient.common.codegen.TypeTraverser;
import com.strategyobject.substrateclient.rpc.core.EncoderPair;
import com.strategyobject.substrateclient.rpc.core.RpcEncoder;
import com.strategyobject.substrateclient.rpc.core.RpcRegistryHelper;
import com.strategyobject.substrateclient.scale.ScaleRegistryHelper;
import com.strategyobject.substrateclient.scale.ScaleWriter;
import lombok.NonNull;
import lombok.val;
Expand All @@ -16,22 +18,23 @@
import javax.lang.model.type.TypeVariable;
import java.util.Map;

import static com.strategyobject.substrateclient.rpc.codegen.Constants.PAIR_FACTORY_METHOD;
import static com.strategyobject.substrateclient.rpc.codegen.Constants.RPC_SELF_ENCODABLE;
import static com.strategyobject.substrateclient.rpc.codegen.Constants.*;

public class EncoderCompositor extends TypeTraverser<CodeBlock> {
private final ProcessorContext context;
private final Map<String, Integer> typeVarMap;
private final TypeMirror selfEncodable;
private final String encoderAccessor;
private final String writerAccessor;
private final String writerMethod;
private final String encoderRegistryVarName;
private final String scaleRegistryVarName;

public EncoderCompositor(@NonNull ProcessorContext context,
@NonNull Map<String, Integer> typeVarMap,
@NonNull String encoderAccessor,
@NonNull String writerAccessor,
@NonNull String writerMethod,
@NonNull String encoderRegistryVarName,
@NonNull String scaleRegistryVarName) {
super(CodeBlock.class);
Expand All @@ -40,6 +43,7 @@ public EncoderCompositor(@NonNull ProcessorContext context,
this.selfEncodable = context.erasure(context.getType(RPC_SELF_ENCODABLE));
this.encoderAccessor = encoderAccessor;
this.writerAccessor = writerAccessor;
this.writerMethod = writerMethod;
this.encoderRegistryVarName = encoderRegistryVarName;
this.scaleRegistryVarName = scaleRegistryVarName;
}
Expand Down Expand Up @@ -89,23 +93,23 @@ private CodeBlock getNonGenericCodeBlock(TypeMirror type) {
protected CodeBlock whenGenericType(@NonNull DeclaredType type, TypeMirror _override, @NonNull CodeBlock[] subtypes) {
TypeMirror resolveType = context.erasure(type);
val builder = CodeBlock.builder()
.add("$T.$L(($T) ", EncoderPair.class, PAIR_FACTORY_METHOD, RpcEncoder.class);
.add("$T.$L(", EncoderPair.class, PAIR_FACTORY_METHOD);

if (context.isSubtypeOf(resolveType, selfEncodable)) {
builder.add("registry.resolve($T.class)", selfEncodable);
builder.add("($T) registry.resolve($T.class)", selfEncodable, RpcEncoder.class);
} else {
builder.add("$L.resolve($T.class).inject(", encoderRegistryVarName, resolveType);
builder.add("$T.$L($T.class, ", RpcRegistryHelper.class, RESOLVE_AND_INJECT_METHOD, resolveType);
for (var i = 0; i < subtypes.length; i++) {
if (i > 0) builder.add(", ");
builder.add(subtypes[i]);
}
builder.add(")");
}

builder.add("), ($T) $L.resolve($T.class).inject(", ScaleWriter.class, scaleRegistryVarName, resolveType);
builder.add("), $T.$L($T.class, ", ScaleRegistryHelper.class, RESOLVE_AND_INJECT_METHOD, resolveType);
for (var i = 0; i < subtypes.length; i++) {
if (i > 0) builder.add(", ");
builder.add(subtypes[i]);
builder.add(".$L", writerMethod);
}
builder.add(")");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private void setFields(MethodSpec.Builder methodSpec, ProcessorContext context)
typeVarMap,
String.format("%s[$L].%s", ENCODERS_ARG, ENCODER_UNSAFE_ACCESSOR),
String.format("%s[$L].%s", ENCODERS_ARG, WRITER_UNSAFE_ACCESSOR),
WRITER_UNSAFE_ACCESSOR,
ENCODER_REGISTRY,
SCALE_WRITER_REGISTRY);
val scaleAnnotationParser = new ScaleAnnotationParser(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private CodeBlock getRpcDecodeCodeBlock(TypeMirror resultType, String arg, Proce
EMPTY_TYPE_VAR_MAP,
String.format("%s[$L].%s", DECODERS_ARG, DECODER_UNSAFE_ACCESSOR),
String.format("%s[$L].%s", DECODERS_ARG, READER_UNSAFE_ACCESSOR),
READER_UNSAFE_ACCESSOR,
DECODER_REGISTRY,
SCALE_READER_REGISTRY);

Expand Down Expand Up @@ -184,6 +185,7 @@ private void processParameters(MethodSpec.Builder methodSpecBuilder,
EMPTY_TYPE_VAR_MAP,
String.format("%s[$L].%s", ENCODERS_ARG, ENCODER_UNSAFE_ACCESSOR),
String.format("%s[$L].%s", ENCODERS_ARG, WRITER_UNSAFE_ACCESSOR),
WRITER_UNSAFE_ACCESSOR,
ENCODER_REGISTRY,
SCALE_WRITER_REGISTRY);

Expand All @@ -199,7 +201,7 @@ private void processParameters(MethodSpec.Builder methodSpecBuilder,
for (val param : method.getParameters()) {
try {
processParameter(methodSpecBuilder, method, param, encoderCompositor, writerCompositor, scaleAnnotationParser, context);
} catch (Exception e){
} catch (Exception e) {
throw new ProcessingException(e, param, e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import lombok.val;
import org.junit.jupiter.api.Test;

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -56,8 +62,20 @@ void compilesAndDecodes() { // TODO move this test out of the project
val decoder = registry.resolve(TestDecodable.class)
.inject(DecoderPair.of(registry.resolve(String.class), null));

Object source = gson.fromJson("{a: 4, b: \"123\", c: \"some\"}", Object.class);
val expected = new TestDecodable<>(4, "123", "some");
Object source = gson.fromJson("{\"a\":4,\"b\":\"123\",\"c\":\"some\"," +
"\"d\":[\"1\",\"2\"],\"e\":{\"a\":1,\"b\":2},\"f\":\"0x04000000\"," +
"\"g\":\"0x0c0500000002000000fdffffff\"}",
Object.class);
val expected = new TestDecodable<>(4,
"123",
"some",
Arrays.asList("1", "2"),
Stream.of(
new AbstractMap.SimpleEntry<>("a", 1),
new AbstractMap.SimpleEntry<>("b", 2))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
4,
Arrays.asList(5, 2, -3));

val actual = decoder.decode(source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import com.strategyobject.substrateclient.rpc.core.EncoderPair;
import com.strategyobject.substrateclient.rpc.core.RpcEncoder;
import com.strategyobject.substrateclient.rpc.core.registries.RpcEncoderRegistry;
import com.strategyobject.substrateclient.scale.ScaleUtils;
import com.strategyobject.substrateclient.scale.writers.I32Writer;
import lombok.val;
import org.junit.jupiter.api.Test;

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -68,9 +72,19 @@ void compilesAndDecodes() { // TODO move this test out of the project
)),
null));

val source = new TestEncodable<>(4, "some", new TestEncodable.Subclass<>(123), true);
val source = new TestEncodable<>(4,
"some",
new TestEncodable.Subclass<>(123),
true,
Arrays.asList("a", "b"),
Stream.of(
new AbstractMap.SimpleEntry<>("a", 1),
new AbstractMap.SimpleEntry<>("b", 2))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
Arrays.asList(2, 3));
val expected = gson.fromJson(
"{\"a\":\"" + ScaleUtils.toHexString(4, new I32Writer()) + "\", \"b\": \"some\", \"c\": {\"a\": 123}, \"d\": true}",
"{\"a\":\"0x04000000\",\"b\":\"some\",\"c\":{\"a\":123},\"d\":true," +
"\"e\":[\"a\",\"b\"],\"f\":{\"a\":1,\"b\":2},\"h\":\"0x080200000003000000\"}",
Object.class);

val actual = gson.fromJson(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.strategyobject.substrateclient.rpc.codegen.substitutes;

import com.strategyobject.substrateclient.rpc.core.annotations.RpcDecoder;
import com.strategyobject.substrateclient.scale.annotations.Scale;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@RpcDecoder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class TestDecodable<T> {
public int a;
public String b;
public T c;

public TestDecodable() {

}

public TestDecodable(int a, String b, T c) {
this.a = a;
this.b = b;
this.c = c;
}
private int a;
private String b;
private T c;
private List<String> d;
private Map<String, Integer> e;
@Scale
private int f;
@Scale
private List<Integer> g;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@RpcEncoder
@Getter
@Setter
@AllArgsConstructor
public class TestEncodable<T> {
@Scale
public int a;
public String b;
public T c;
public boolean d;

public TestEncodable() {

}

public TestEncodable(int a, String b, T c, boolean d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
private int a;
private String b;
private T c;
private boolean d;
private List<String> e;
private Map<String, Integer> f;
@Scale
private List<Integer> h;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.strategyobject.substrateclient.rpc.core;

import com.strategyobject.substrateclient.rpc.core.registries.RpcDecoderRegistry;
import com.strategyobject.substrateclient.rpc.core.registries.RpcEncoderRegistry;
import lombok.val;

public final class RpcRegistryHelper {
private RpcRegistryHelper() {
}

@SuppressWarnings("unchecked")
public static <T> RpcDecoder<T> resolveAndInjectOrNull(Class<T> clazz, DecoderPair<?>... dependencies) {
val target = (RpcDecoder<T>) RpcDecoderRegistry.getInstance().resolve(clazz);

if (target == null) {
return null;
}

return target.inject(dependencies);
}

@SuppressWarnings("unchecked")
public static <T> RpcEncoder<T> resolveAndInjectOrNull(Class<T> clazz, EncoderPair<?>... dependencies) {
val target = (RpcEncoder<T>) RpcEncoderRegistry.getInstance().resolve(clazz);

if (target == null) {
return null;
}

return target.inject(dependencies);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.strategyobject.substrateclient.rpc.core.annotations.RpcCall;
import com.strategyobject.substrateclient.rpc.core.annotations.RpcInterface;
import com.strategyobject.substrateclient.rpc.types.Metadata;
import com.strategyobject.substrateclient.rpc.types.RuntimeVersion;
import com.strategyobject.substrateclient.rpc.types.*;
import com.strategyobject.substrateclient.scale.annotations.Scale;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@RpcInterface(section = "state")
Expand All @@ -16,4 +16,13 @@ public interface State {
@RpcCall(method = "getMetadata")
@Scale
CompletableFuture<Metadata> getMetadata();

@RpcCall(method = "getKeys")
CompletableFuture<List<StorageKey>> getKeys(StorageKey key);

@RpcCall(method = "getStorage")
CompletableFuture<StorageData> getStorage(StorageKey key);

@RpcCall(method = "queryStorageAt")
CompletableFuture<List<StorageChangeSet>> queryStorageAt(List<StorageKey> keys);
}
Loading