-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: support plugin loading in conifg #4974
Merged
chenqi0805
merged 12 commits into
opensearch-project:main
from
chenqi0805:enh/4838-support-plugin-loading-in-conifg
Oct 2, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9620bc9
ADD: initial impl on resolving target type
chenqi0805 35e0c9c
ENH: validate embedded pluginModel and generated embedded schemas
chenqi0805 4030ee1
MAINT: backfill annotations
chenqi0805 80ef670
STY: fix errors
chenqi0805 a4d4a08
Merge branch 'main' into enh/4838-support-plugin-loading-in-conifg
chenqi0805 1eaa56d
MAINT: fix dependency
chenqi0805 254b737
FIX: bump reflection version
chenqi0805 07b69ba
REF: use scan for plugins in plugin-framework
chenqi0805 323773f
RMV: unused files
chenqi0805 817d764
FIX: sty and broken tests
chenqi0805 60ad37d
FIX: sty
chenqi0805 3c9aa7f
MAINT: remove unused dependency
chenqi0805 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 23 additions & 0 deletions
23
...api/src/main/java/org/opensearch/dataprepper/model/annotations/UsesDataPrepperPlugin.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,23 @@ | ||
package org.opensearch.dataprepper.model.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotates a field that uses Data Prepper plugin config as its value. | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD}) | ||
public @interface UsesDataPrepperPlugin { | ||
/** | ||
* The class type for this plugin. | ||
* | ||
* @return The Java class | ||
* @since 1.2 | ||
*/ | ||
Class<?> pluginType(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,16 +10,27 @@ | |
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; | ||
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; | ||
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart; | ||
import com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart; | ||
import com.github.victools.jsonschema.generator.SchemaVersion; | ||
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin; | ||
import org.opensearch.dataprepper.model.annotations.UsesDataPrepperPlugin; | ||
import org.opensearch.dataprepper.plugin.PluginProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class JsonSchemaConverter { | ||
private static final Logger LOG = LoggerFactory.getLogger(JsonSchemaConverter.class); | ||
static final String DEPRECATED_SINCE_KEY = "deprecated"; | ||
private final List<Module> jsonSchemaGeneratorModules; | ||
private final PluginProvider pluginProvider; | ||
|
||
public JsonSchemaConverter(final List<Module> jsonSchemaGeneratorModules) { | ||
public JsonSchemaConverter(final List<Module> jsonSchemaGeneratorModules, final PluginProvider pluginProvider) { | ||
this.jsonSchemaGeneratorModules = jsonSchemaGeneratorModules; | ||
this.pluginProvider = pluginProvider; | ||
} | ||
|
||
public ObjectNode convertIntoJsonSchema( | ||
|
@@ -30,7 +41,9 @@ public ObjectNode convertIntoJsonSchema( | |
loadJsonSchemaGeneratorModules(configBuilder); | ||
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart = configBuilder.forFields(); | ||
overrideInstanceAttributeWithDeprecated(scopeSchemaGeneratorConfigPart); | ||
overrideTargetTypeWithUsesDataPrepperPlugin(scopeSchemaGeneratorConfigPart); | ||
resolveDefaultValueFromJsonProperty(scopeSchemaGeneratorConfigPart); | ||
overrideDataPrepperPluginTypeAttribute(configBuilder.forTypesInGeneral(), schemaVersion, optionPreset); | ||
|
||
final SchemaGeneratorConfig config = configBuilder.build(); | ||
final SchemaGenerator generator = new SchemaGenerator(config); | ||
|
@@ -52,6 +65,37 @@ private void overrideInstanceAttributeWithDeprecated( | |
}); | ||
} | ||
|
||
private void overrideTargetTypeWithUsesDataPrepperPlugin( | ||
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart) { | ||
scopeSchemaGeneratorConfigPart.withTargetTypeOverridesResolver(field -> Optional | ||
.ofNullable(field.getAnnotationConsideringFieldAndGetterIfSupported(UsesDataPrepperPlugin.class)) | ||
.map(usesDataPrepperPlugin -> | ||
pluginProvider.findPluginClasses(usesDataPrepperPlugin.pluginType()).stream()) | ||
.map(stream -> stream.map(specificSubtype -> field.getContext().resolve(specificSubtype))) | ||
.map(stream -> stream.collect(Collectors.toList())) | ||
.orElse(null)); | ||
} | ||
|
||
private void overrideDataPrepperPluginTypeAttribute( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scanning for plugins should only be done in the plugin framework. Make modifications there and use those. |
||
final SchemaGeneratorGeneralConfigPart schemaGeneratorGeneralConfigPart, | ||
final SchemaVersion schemaVersion, final OptionPreset optionPreset) { | ||
schemaGeneratorGeneralConfigPart.withTypeAttributeOverride((node, scope, context) -> { | ||
final DataPrepperPlugin dataPrepperPlugin = scope.getType().getErasedType() | ||
.getAnnotation(DataPrepperPlugin.class); | ||
if (dataPrepperPlugin != null) { | ||
final ObjectNode propertiesNode = node.putObject("properties"); | ||
try { | ||
final ObjectNode schemaNode = this.convertIntoJsonSchema( | ||
schemaVersion, optionPreset, dataPrepperPlugin.pluginConfigurationType()); | ||
propertiesNode.set(dataPrepperPlugin.name(), schemaNode); | ||
} catch (JsonProcessingException e) { | ||
LOG.error("Encountered error retrieving JSON schema for {}", dataPrepperPlugin.name(), e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void resolveDefaultValueFromJsonProperty( | ||
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart) { | ||
scopeSchemaGeneratorConfigPart.withDefaultResolver(field -> { | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't need this if we support it on the target itself.