-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from strategyobject/feature/di
Implement DI with Guice
- Loading branch information
Showing
189 changed files
with
1,541 additions
and
985 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
api/src/main/java/com/strategyobject/substrateclient/api/ApiBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Module; | ||
import com.strategyobject.substrateclient.transport.ProviderInterface; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
@RequiredArgsConstructor | ||
public class ApiBuilder<M extends Module> { | ||
private final @NonNull M module; | ||
|
||
public ApiBuilder<M> configure(@NonNull Consumer<M> configuration) { | ||
configuration.accept(module); | ||
return this; | ||
} | ||
|
||
public <N extends Module> ApiBuilder<N> reconfigure(@NonNull Function<M, N> configuration) { | ||
return new ApiBuilder<>(configuration.apply(this.module)); | ||
} | ||
|
||
public CompletableFuture<Api> build() { | ||
val injector = Guice.createInjector(new RequireModule(), module); | ||
val provider = injector.getInstance(ProviderInterface.class); | ||
val result = provider.isConnected() ? | ||
CompletableFuture.<Void>completedFuture(null) : | ||
provider.connect(); | ||
|
||
return result.thenApply(ignored -> injector.getInstance(Api.class)); | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
api/src/main/java/com/strategyobject/substrateclient/api/DefaultModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Provides; | ||
import com.google.inject.Singleton; | ||
import com.strategyobject.substrateclient.common.types.AutoRegistry; | ||
import com.strategyobject.substrateclient.pallet.GeneratedPalletFactory; | ||
import com.strategyobject.substrateclient.pallet.PalletFactory; | ||
import com.strategyobject.substrateclient.rpc.GeneratedRpcSectionFactory; | ||
import com.strategyobject.substrateclient.rpc.RpcSectionFactory; | ||
import com.strategyobject.substrateclient.rpc.api.section.State; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcDecoderRegistry; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcEncoderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleWriterRegistry; | ||
import com.strategyobject.substrateclient.transport.ProviderInterface; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
|
||
import java.util.function.Consumer; | ||
|
||
@RequiredArgsConstructor | ||
public class DefaultModule extends AbstractModule { | ||
private final @NonNull ProviderInterface providerInterface; | ||
|
||
private Consumer<ScaleReaderRegistry> configureScaleReaderRegistry = this::autoRegister; | ||
private Consumer<ScaleWriterRegistry> configureScaleWriterRegistry = this::autoRegister; | ||
private Consumer<RpcDecoderRegistry> configureRpcDecoderRegistry = this::autoRegister; | ||
private Consumer<RpcEncoderRegistry> configureRpcEncoderRegistry = this::autoRegister; | ||
|
||
private void autoRegister(AutoRegistry registry) { | ||
registry.registerAnnotatedFrom("com.strategyobject.substrateclient"); | ||
} | ||
|
||
public DefaultModule configureScaleReaderRegistry(Consumer<ScaleReaderRegistry> configure) { | ||
configureScaleReaderRegistry = configureScaleReaderRegistry.andThen(configure); | ||
return this; | ||
} | ||
|
||
public DefaultModule configureScaleWriterRegistry(Consumer<ScaleWriterRegistry> configure) { | ||
configureScaleWriterRegistry = configureScaleWriterRegistry.andThen(configure); | ||
return this; | ||
} | ||
|
||
public DefaultModule configureRpcDecoderRegistry(Consumer<RpcDecoderRegistry> configure) { | ||
configureRpcDecoderRegistry = configureRpcDecoderRegistry.andThen(configure); | ||
return this; | ||
} | ||
|
||
public DefaultModule configureRpcEncoderRegistry(Consumer<RpcEncoderRegistry> configure) { | ||
configureRpcEncoderRegistry = configureRpcEncoderRegistry.andThen(configure); | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
try { | ||
bind(ProviderInterface.class).toInstance(providerInterface); | ||
bind(RpcSectionFactory.class) | ||
.toConstructor( | ||
GeneratedRpcSectionFactory.class.getConstructor( | ||
ProviderInterface.class, | ||
RpcEncoderRegistry.class, | ||
ScaleWriterRegistry.class, | ||
RpcDecoderRegistry.class, | ||
ScaleReaderRegistry.class)) | ||
.asEagerSingleton(); | ||
bind(PalletFactory.class) | ||
.toConstructor( | ||
GeneratedPalletFactory.class.getConstructor( | ||
ScaleReaderRegistry.class, | ||
ScaleWriterRegistry.class, | ||
State.class | ||
)) | ||
.asEagerSingleton(); | ||
bind(Api.class) | ||
.toConstructor( | ||
Api.class.getConstructor( | ||
RpcSectionFactory.class, | ||
PalletFactory.class)) | ||
.asEagerSingleton(); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public ScaleReaderRegistry provideScaleReaderRegistry() { | ||
val registry = new ScaleReaderRegistry(); | ||
configureScaleReaderRegistry.accept(registry); | ||
return registry; | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public ScaleWriterRegistry provideScaleWriterRegistry() { | ||
val registry = new ScaleWriterRegistry(); | ||
configureScaleWriterRegistry.accept(registry); | ||
return registry; | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public RpcDecoderRegistry provideRpcDecoderRegistry(ScaleReaderRegistry scaleReaderRegistry) { | ||
val registry = new RpcDecoderRegistry(scaleReaderRegistry); | ||
configureRpcDecoderRegistry.accept(registry); | ||
return registry; | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public RpcEncoderRegistry provideRpcEncoderRegistry(ScaleWriterRegistry scaleWriterRegistry) { | ||
val registry = new RpcEncoderRegistry(scaleWriterRegistry); | ||
configureRpcEncoderRegistry.accept(registry); | ||
return registry; | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public State provideState(RpcSectionFactory rpcSectionFactory) { | ||
return rpcSectionFactory.create(State.class); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
api/src/main/java/com/strategyobject/substrateclient/api/RequireModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.strategyobject.substrateclient.pallet.PalletFactory; | ||
import com.strategyobject.substrateclient.rpc.RpcSectionFactory; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcDecoderRegistry; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcEncoderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleWriterRegistry; | ||
import com.strategyobject.substrateclient.transport.ProviderInterface; | ||
|
||
public class RequireModule extends AbstractModule { | ||
@Override | ||
protected void configure() { | ||
requireBinding(ProviderInterface.class); | ||
requireBinding(ScaleReaderRegistry.class); | ||
requireBinding(ScaleWriterRegistry.class); | ||
requireBinding(RpcDecoderRegistry.class); | ||
requireBinding(RpcEncoderRegistry.class); | ||
requireBinding(RpcSectionFactory.class); | ||
requireBinding(PalletFactory.class); | ||
requireBinding(Api.class); | ||
} | ||
} |
Oops, something went wrong.