-
Notifications
You must be signed in to change notification settings - Fork 202
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
Changes from 11 commits
9620bc9
35e0c9c
4030ee1
80ef670
a4d4a08
1eaa56d
254b737
07b69ba
323773f
817d764
60ad37d
3c9aa7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
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. We probably don't need this if we support it on the target itself. |
||
} |
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 -> { | ||
|
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 shouldn't need this anymore.