# auto-pipeline
**Repository Path**: whilewon/auto-pipeline
## Basic Information
- **Project Name**: auto-pipeline
- **Description**: auto-pipeline
- **Primary Language**: Kotlin
- **License**: Apache-2.0
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-10-29
- **Last Updated**: 2022-10-29
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
#
`auto-pipeline` is a source code generator that auto generate the component's pipeline. Help you to keep your project smaller, simpler, and more extensible. 💡
`auto-pipeline` is an [`annotation-processor`](https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/package-summary.html) for `Pipeline` generation, which is inspired by
Google's [`Auto`](https://github.com/google/auto). ❤️
for more information, please check out the [auto-pipeline documents](https://foldright.io/auto-pipeline/).
## quick examples
below is a brief introduction. please check the [examples project](auto-pipeline-examples), and it's test cases for details.
## quick start
`auto-pipeline` require java 8 or above.
### 0. add `auto-pipeline` dependencies
for `maven` project:
```xml
com.foldright.auto-pipeline
auto-pipeline-processor
0.2.0
provided
```
for `gradle` project:
```groovy
/*
* Gradle Kotlin DSL
*/
// the auto-pipeline annotation will be used in your interface type
compileOnly("com.foldright.auto-pipeline:auto-pipeline-annotations:0.1.0")
// the auto-pipeline annotation processor will generate the pipeline classes for the interface.
// use "annotationProcessor" scope because it's only needed at annotation processing time.
annotationProcessor("com.foldright.auto-pipeline:auto-pipeline-processor:0.1.0")
/*
* Gradle Groovy DSL
*/
compileOnly 'com.foldright.auto-pipeline:auto-pipeline-annotations:0.1.0'
annotationProcessor 'com.foldright.auto-pipeline:auto-pipeline-processor:0.1.0'
```
`auto-pipeline` has published to maven central, click here
to [find the latest version](https://search.maven.org/search?q=g:com.foldright.auto-pipeline).
### 1. using `@AutoPipeline` to auto generate pipeline for your interface
annotate `@AutoPipeline` to your interface, and `auto-pipeline` will generate some java files for the interface at compile time.
let's check the [`ConfigSource`](auto-pipeline-examples/src/main/java/com/foldright/examples/config/ConfigSource.java) as an example:
given an interface named `ConfigSource`, the `ConfigSource` has the `get()` method, input a string as key and output a string as the value.
like this:
```java
public interface ConfigSource {
String get(String key);
}
```
say, we want `ConfigSource#get()` has some extensibility, so we decide to apply the `chain of responsibility` pattern to it for extensibility.
Now it's `auto-pipeline`'s turn to play a role, we simply add `@AutoPipelin` to `ConfigSource`:
```java
@AutoPipeline
public interface ConfigSource {
String get(String key);
}
```
`auto-pipeline-processor` will auto generate pipeline java files for `ConfigSource` into subpackage `pipeline` when compiled:
- `ConfigSourceHandler`
the responsibility interface we want to implement for extensibility
- `ConfigSourcePipeline`
the chain
- `ConfigSourceHandlerContext`
- `AbstractConfigSourceHandlerContext`
- `DefaultConfigSourceHandlerContext`
### 2. implementing your handler for pipeline
we can implement `MapConfigSourceHandler` and `SystemConfigSourceHandler` (they are all in the [ConfigSource handler example](auto-pipeline-examples/src/main/java/com/foldright/examples/config/handler)):
```java
public class MapConfigSourceHandler implements ConfigSourceHandler {
private final Map map;
public MapConfigSourceHandler(Map map) {
this.map = map;
}
@Override
public String get(String key, ConfigSourceHandlerContext context) {
String value = map.get(key);
if (StringUtils.isNotBlank(value)) {
return value;
}
return context.get(key);
}
}
public class SystemConfigSourceHandler implements ConfigSourceHandler {
public static final SystemConfigSourceHandler INSTANCE = new SystemConfigSourceHandler();
@Override
public String get(String key, ConfigSourceHandlerContext context) {
String value = System.getProperty(key);
if (StringUtils.isNotBlank(value)) {
return value;
}
return context.get(key);
}
}
```
### 3. use the pipeline
create a `ConfigSourcePipeline` by composing `ConfigSourceHandler`s which can ben an entrance of the `ConfigSource`:
```java
Map mapConfig = new HashMap();
mapConfig.put("hello", "world");
ConfigSourceHandler mapConfigSourceHandler = new MapConfigSourceHandler(mapConfig);
ConfigSource pipeline = new ConfigSourcePipeline()
.addLast(mapConfigSourceHandler)
.addLast(SystemConfigSourceHandler.INSTANCE);
```
now, we can use the `pipeline.get(...)` to invoke the chain! 🎉
```java
pipeline.get("hello");
// get value "world"
// from mapConfig / mapConfigSourceHandler
pipeline.get("java.specification.version")
// get value "1.8"
// from system properties / SystemConfigSourceHandler
```
check the runnable [test case](auto-pipeline-examples/src/test/java/com/foldright/examples/config/pipeline/ConfigSourceTest.kt) for details.
## License
Apache License 2.0