-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add AsyncGenericBinding (#370)
* feat(core): add AsyncGenericBinding * feat(core): refine AsyncGenericOperationBinding * feat(core): improve example * feat(core): add BindingProcessorPriority * feat(core): handle multiple bindings with same type Removed AsyncGenericOperationBinding * refactor(core): move binding processors to binding package Leave public annotations in current package to avoid breaking changes * feat(generic-binding): Move `@AsyncGenericOperationBinding` to own addon * feat(generic-binding): Update README.md * chore(generic-binding): fixup * chore(generic-binding): fixup * refactor(generic-binding): Use spring configuration * feat(generic-binding): Improve sqs example (overwrite existing binding) * chore(generic-binding): move to package addons * chore(generic-binding): spotless * chore(generic-binding): spotless * chore(generic-binding): move to addons package * chore(generic-binding): move to addons package * chore(generic-binding): move to generic_binding package * chore(generic-binding): add to sns plugin * chore(common-model-converters): fix auto configuration path * chore(generic-binding): fix log formatting * chore(gh): Run full matrix, do not abort early * test(sqs): update asyncapi.json (temporary output) Co-authored-by: sam0r040 <[email protected]>
- Loading branch information
Showing
51 changed files
with
608 additions
and
74 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
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
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
Empty file.
4 changes: 2 additions & 2 deletions
4
...mmonModelConvertersAutoConfiguration.java → ...mmonModelConvertersAutoConfiguration.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
2 changes: 1 addition & 1 deletion
2
...erters/monetaryamount/MonetaryAmount.java → ...erters/monetaryamount/MonetaryAmount.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
2 changes: 1 addition & 1 deletion
2
...netaryamount/MonetaryAmountConverter.java → ...netaryamount/MonetaryAmountConverter.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
2 changes: 1 addition & 1 deletion
2
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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 |
---|---|---|
@@ -1 +1 @@ | ||
io.github.stavshamir.springwolf.common_converters.configuration.CommonModelConvertersAutoConfiguration | ||
io.github.stavshamir.springwolf.addons.common_model_converters.configuration.CommonModelConvertersAutoConfiguration |
Empty file.
4 changes: 2 additions & 2 deletions
4
...nverters/MonetaryAmountConverterTest.java → ...nverters/MonetaryAmountConverterTest.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
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,48 @@ | ||
# Springwolf Generic Binding Add-on | ||
|
||
### Table Of Contents | ||
|
||
- [About](#about) | ||
- [Usage](#usage) | ||
- [Dependencies](#dependencies) | ||
- [Code](#code) | ||
|
||
### About | ||
|
||
This module allows to document any binding in a generic way. | ||
|
||
It is intended to document fields that are not yet support by Springwolf and/or AsyncApi. | ||
It can also be used to document vendor-specific protocols or properties. | ||
|
||
There exists no validation on key or value names. | ||
|
||
### Usage | ||
|
||
Add the following dependency: | ||
|
||
#### Dependencies | ||
|
||
```groovy | ||
dependencies { | ||
implementation 'io.github.springwolf:springwolf-generic-binding:<springwolf-version>' | ||
} | ||
``` | ||
|
||
#### Code | ||
|
||
```java | ||
class TestClass { | ||
@AsyncPublisher( | ||
// ... | ||
) | ||
@AsyncGenericOperationBinding( | ||
type = "custom-binding", | ||
fields = { | ||
"internal-field=customValue", | ||
"nested.key=nestedValue" | ||
}) | ||
public void sendMessage(AnotherPayloadDto msg) { | ||
// ... | ||
} | ||
} | ||
``` |
51 changes: 51 additions & 0 deletions
51
springwolf-add-ons/springwolf-generic-binding/build.gradle
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,51 @@ | ||
plugins { | ||
id 'java-library' | ||
|
||
id 'org.springframework.boot' | ||
id 'io.spring.dependency-management' | ||
id 'ca.cutterslade.analyze' | ||
} | ||
|
||
dependencies { | ||
api project(":springwolf-core") | ||
|
||
implementation "com.asyncapi:asyncapi-core:${asyncapiCoreVersion}" | ||
implementation "org.slf4j:slf4j-api:${slf4jApiVersion}" | ||
|
||
implementation "org.springframework:spring-context" | ||
implementation "org.springframework:spring-core" | ||
|
||
annotationProcessor "org.projectlombok:lombok:${lombokVersion}" | ||
|
||
testImplementation "org.assertj:assertj-core:${assertjCoreVersion}" | ||
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}" | ||
testRuntimeOnly "org.junit.jupiter:junit-jupiter:${junitJupiterVersion}" | ||
} | ||
|
||
jar { | ||
enabled = true | ||
archiveClassifier = '' | ||
} | ||
bootJar.enabled = false | ||
|
||
java { | ||
withJavadocJar() | ||
withSourcesJar() | ||
} | ||
|
||
test { | ||
dependsOn spotlessApply // Automatically fix code formatting if possible | ||
|
||
useJUnitPlatform() | ||
} | ||
|
||
publishing { | ||
publications { | ||
mavenJava(MavenPublication) { | ||
pom { | ||
name = 'springwolf-generic-binding' | ||
description = 'Document any AsyncApi binding in Springwolf using a generic way' | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...stavshamir/springwolf/addons/generic_binding/annotation/AsyncGenericOperationBinding.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,30 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.addons.generic_binding.annotation; | ||
|
||
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.annotation.AsyncOperationBinding; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Springwolf cannot support all available protocol bindings that exist. | ||
* To allow users to manually define them, {@link AsyncGenericOperationBinding} can be used. | ||
* <p> | ||
* Use the {@link AsyncGenericOperationBinding#fields()} to define the attributes | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(value = {ElementType.METHOD}) | ||
@AsyncOperationBinding | ||
public @interface AsyncGenericOperationBinding { | ||
/** | ||
* The name of the binding | ||
*/ | ||
String type(); | ||
|
||
/** | ||
* All binding fields | ||
*/ | ||
String[] fields() default {}; | ||
} |
18 changes: 18 additions & 0 deletions
18
...ddons/generic_binding/annotation/configuration/SpringwolfGenericBindingConfiguration.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,18 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.addons.generic_binding.annotation.configuration; | ||
|
||
import io.github.stavshamir.springwolf.addons.generic_binding.annotation.processor.AsyncGenericOperationBindingProcessor; | ||
import io.github.stavshamir.springwolf.asyncapi.scanners.bindings.BindingProcessorPriority; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.Order; | ||
|
||
@Configuration | ||
public class SpringwolfGenericBindingConfiguration { | ||
|
||
@Bean | ||
@Order(value = BindingProcessorPriority.GENERIC_BINDING) | ||
public AsyncGenericOperationBindingProcessor asyncGenericOperationBindingProcessor() { | ||
return new AsyncGenericOperationBindingProcessor(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...lf/addons/generic_binding/annotation/processor/AsyncGenericOperationBindingProcessor.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,28 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.addons.generic_binding.annotation.processor; | ||
|
||
import com.asyncapi.v2.binding.operation.OperationBinding; | ||
import io.github.stavshamir.springwolf.addons.generic_binding.annotation.AsyncGenericOperationBinding; | ||
import io.github.stavshamir.springwolf.asyncapi.scanners.bindings.ProcessedOperationBinding; | ||
import io.github.stavshamir.springwolf.asyncapi.scanners.bindings.processor.AbstractOperationBindingProcessor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class AsyncGenericOperationBindingProcessor | ||
extends AbstractOperationBindingProcessor<AsyncGenericOperationBinding> { | ||
|
||
@Override | ||
protected ProcessedOperationBinding mapToOperationBinding(AsyncGenericOperationBinding bindingAnnotation) { | ||
Map<String, Object> bindingData = PropertiesUtil.toMap(bindingAnnotation.fields()); | ||
|
||
return new ProcessedOperationBinding( | ||
bindingAnnotation.type(), new DefaultAsyncGenerialOperationBinding(bindingData)); | ||
} | ||
|
||
public static class DefaultAsyncGenerialOperationBinding extends OperationBinding { | ||
public DefaultAsyncGenerialOperationBinding(Map<String, Object> properties) { | ||
this.extensionFields = new HashMap<>(properties); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...hub/stavshamir/springwolf/addons/generic_binding/annotation/processor/PropertiesUtil.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,55 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.addons.generic_binding.annotation.processor; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
@Slf4j | ||
public class PropertiesUtil { | ||
|
||
public static Map<String, Object> toMap(String[] propertyStrings) { | ||
return convertPropertiesToNestedMap(buildPropertiesFrom((propertyStrings))); | ||
} | ||
|
||
private static Properties buildPropertiesFrom(String[] propertyStrings) { | ||
Properties properties = new Properties(); | ||
for (String bindingProperty : propertyStrings) { | ||
try { | ||
properties.load(new StringReader(bindingProperty)); | ||
} catch (IOException e) { | ||
log.warn("Unable to parse property {}", bindingProperty, e); | ||
} | ||
} | ||
return properties; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static Map<String, Object> convertPropertiesToNestedMap(Properties properties) { | ||
Map<String, Object> bindingData = new HashMap<>(); | ||
for (String propertyName : properties.stringPropertyNames()) { | ||
LinkedList<String> path = new LinkedList<>(Arrays.asList(propertyName.split("\\."))); | ||
|
||
Map<String, Object> mapNode = bindingData; | ||
while (path.size() > 1) { | ||
String pathElement = path.get(0); | ||
if (!mapNode.containsKey(pathElement)) { | ||
mapNode.put(pathElement, new HashMap<>()); | ||
} | ||
|
||
mapNode = (Map<String, Object>) mapNode.get(pathElement); | ||
|
||
path.pop(); | ||
} | ||
|
||
mapNode.put(path.get(0), properties.get(propertyName)); | ||
} | ||
return bindingData; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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 @@ | ||
io.github.stavshamir.springwolf.addons.generic_binding.annotation.processor.AsyncGenericOperationBindingProcessor |
Oops, something went wrong.