Coder Social home page Coder Social logo

claymore's Introduction

๐Ÿ—ก๏ธ CLAYMORE

A library that writes an hilt module for a requested interface implementation.

๐ŸŽฌ Scenario

Prerequisite: project using Hilt for dependencies injection.

In a typical use case, you have:

interface MyInterface

---

class MyImplementation @Inject constructor() : MyInterface

and you need to write a simple module only to bind the right implementation:

@Module
@InstallIn(SingletonComponent::class)
interface MyModule {
  @Binds
  fun binding(impl: MyImplementation): MyInterface
}

๐Ÿง™ Annotation

With claymore you can avoid to manually write that Module, by using @AutoBinds annotation:

@AutoBinds
class MyImplementation: MyInterface

clamyore will automatically generate the necessary module for you.

Component

You can optionally request claymore to install the binding in a specific hilt component, using the component parameter.

@AutoBinds(component = ActivityComponent::class)
class MyImplementation: MyInterface

If not set, the SingletonComponent will be used by default.

Annotations support

You can also request claymore to attach any annotation to the binding method, using the annotations parameter.

@AutoBinds(annotations = [IntoSet::class])
class MyImplementation: MyInterface

// generates a module with a binding function like

@Binds
@IntoSet
fun binding(impl: MyImplementation): MyInterface

AutoUninstall annotation

When in tests you need to uninstall module generated by AutoBinds annotation and so replace real implementations with fakes, you can avoid to reference module generated by claymore using the AutoUninstall annotation instead.

@AutoUninstall(
  implementations = [
    MyImplementation::class,
    MyOtherImplementation::class,
  ]
)
@InstallIn(SingletonComponent::class)
@Module
object TestModule {
  @Provides
  fun fakeImplementation(): MyInterface = FakeImplementation()
  
  @Provides
  fun otherFakeImplementation(): MyOtherInterface = OtherFakeImplementation()  
}

claymore will generate a module that replaces generated MyImplementationModule and MyOtherImplementationModule for you. Again, you can set the component where replace the modules, otherwise SingletonComponent is used by default.

๐ŸŽฎ Demo

Take a look at the :demo module for a sample usage.

In this simple project we have:

  • api module where a single service (interface) lives;
  • impl module where the service implementation is defined, and annotated with AutoBinds;
  • app module where DI starts and service is requested.

Moreover the annotations module define other services to show the usage of AutoBinds.annotations parameter.

In the annotations module the AutoUninstall annotation is used in the test source set to replace the implementations with fakes without referencing the generated modules.

๐Ÿ› ๏ธ Installation

Download

Claymore is available in Maven Central Repository:

Gradle

repositories {
  mavenCentral()
}

dependencies {
  compileOnly 'io.github.alecarnevale:claymore-annotations:x.y.z'
}

KSP integration

In order to completely enable claymore integration you need to apply the ksp plugin

plugins {
  id 'com.google.devtools.ksp'
}

dependencies {
  ksp 'io.github.alecarnevale:claymore-processors:x.y.z'
}

๐Ÿ™ Thanks to

claymore's People

Contributors

alecarnevale avatar dependabot[bot] avatar

Stargazers

 avatar Pierluigi Caruso avatar Daniele Campogiani avatar  avatar

Watchers

 avatar

claymore's Issues

An annotation for implementation class

Currently @Autobind can only be used for interface, specifying what is the implementation to bind.

That's limited since sometimes you can have an interface in module A and implementation in module B, but A cannot depend on B.

Evaluate to provide an annotation that works for the implementation side.

Add tests

Test coverage is currently 0 ๐Ÿ˜ฌ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.