use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.
the class PropertiesServiceTest method buildProperties.
@Test
void buildProperties() {
final String[] i18nPackages = { Config.class.getPackage().getName() };
final ParameterMeta host = new ParameterMeta(null, Config.class, ParameterMeta.Type.STRING, "configuration.host", "host", i18nPackages, emptyList(), null, emptyMap());
final ParameterMeta port = new ParameterMeta(null, Config.class, ParameterMeta.Type.NUMBER, "configuration.port", "port", i18nPackages, emptyList(), null, emptyMap());
final ParameterMeta username = new ParameterMeta(null, Config.class, ParameterMeta.Type.STRING, "configuration.username", "username", i18nPackages, emptyList(), null, emptyMap());
final ParameterMeta password = new ParameterMeta(null, Config.class, ParameterMeta.Type.STRING, "configuration.password", "password", i18nPackages, emptyList(), null, emptyMap());
final ParameterMeta config = new ParameterMeta(null, Config.class, ParameterMeta.Type.OBJECT, "configuration", "configuration", i18nPackages, asList(host, port, username, password), null, emptyMap());
final List<SimplePropertyDefinition> props = propertiesService.buildProperties(singletonList(config), getClass().getClassLoader(), Locale.getDefault(), null).collect(toList());
assertEquals(5, props.size());
assertEquals("Configuration", props.get(0).getDisplayName());
assertEquals("Server Host Name", props.get(1).getDisplayName());
assertEquals("Enter the server host name...", props.get(1).getPlaceholder());
assertEquals("Password", props.get(2).getDisplayName());
assertEquals("Server Port", props.get(3).getDisplayName());
assertEquals("Enter the server port...", props.get(3).getPlaceholder());
assertEquals("User Name", props.get(4).getDisplayName());
}
use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.
the class TaCoKitGuessSchema method buildActionConfig.
private Map<String, String> buildActionConfig(final ServiceMeta.ActionMeta action, final Map<String, String> configuration) {
if (configuration == null || configuration.isEmpty()) {
// no-mapping
return configuration;
}
final ParameterMeta dataSet = action.getParameters().stream().filter(param -> param.getMetadata().containsKey("tcomp::configurationtype::type") && "dataset".equals(param.getMetadata().get("tcomp::configurationtype::type"))).findFirst().orElse(null);
if (dataSet == null) {
// no mapping to do
return configuration;
}
// action configuration prefix.
final String prefix = dataSet.getPath();
final int dotIndex = configuration.keySet().iterator().next().indexOf(".");
return configuration.entrySet().stream().collect(toMap(e -> prefix + "." + e.getKey().substring(dotIndex + 1, e.getKey().length()), Map.Entry::getValue));
}
use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.
the class ConfigurationMapper method map.
private Map<String, String> map(final List<ParameterMeta> nestedParameters, final Object instance, final Map<Integer, Integer> indexes) {
if (nestedParameters == null) {
return emptyMap();
}
return nestedParameters.stream().map(param -> {
final Object value = getValue(instance, param.getName());
if (value == null) {
return Collections.<String, String>emptyMap();
}
switch(param.getType()) {
case OBJECT:
return map(param.getNestedParameters(), value, indexes);
case ARRAY:
final Collection<Object> values = Collection.class.isInstance(value) ? Collection.class.cast(value) : /* array */
asList(Object[].class.cast(value));
final int arrayIndex = indexes.keySet().size();
final AtomicInteger valuesIndex = new AtomicInteger(0);
final Map<String, String> config = values.stream().map((Object item) -> {
indexes.put(arrayIndex, valuesIndex.getAndIncrement());
final Map<String, String> res = param.getNestedParameters().stream().filter(ConfigurationMapper::isPrimitive).collect(toMap(p -> evaluateIndexes(p.getPath(), indexes), p -> getValue(item, p.getName()).toString()));
res.putAll(map(param.getNestedParameters().stream().filter(p -> !isPrimitive(p)).collect(toList()), item, indexes));
return res;
}).flatMap(m -> m.entrySet().stream()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
// clear index after the end of array handling
indexes.clear();
return config;
default:
// primitives
return singletonMap(evaluateIndexes(param.getPath(), indexes), value.toString());
}
}).flatMap(m -> m.entrySet().stream()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}
use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.
the class PropertiesServiceTest method validateProp.
// the class BaseConfig don't contains attribute
@Test
void validateProp() {
assertThrows(IllegalArgumentException.class, () -> {
ParameterMeta attribute = new ParameterMeta(null, BadConfig.class, ParameterMeta.Type.STRING, "configuration.attribute", "attribute", null, emptyList(), null, emptyMap());
ParameterMeta config = new ParameterMeta(null, Config.class, ParameterMeta.Type.OBJECT, "configuration", "configuration", null, singletonList(attribute), null, emptyMap());
propertiesService.buildProperties(singletonList(config), getClass().getClassLoader(), Locale.ROOT, null).forEach(Objects::nonNull);
});
}
use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.
the class MigrationHandlerFactory method findMigrationHandler.
public MigrationHandler findMigrationHandler(final List<ParameterMeta> parameterMetas, final Class<?> type, final ComponentManager.AllServices services) {
final MigrationHandler implicitMigrationHandler = ofNullable(parameterMetas).map(Collection::stream).orElseGet(Stream::empty).filter(p -> p.getMetadata().keySet().stream().anyMatch(k -> k.startsWith("tcomp::configurationtype::"))).filter(p -> Class.class.isInstance(p.getJavaType())).map(p -> {
// for now we can assume it is not in arrays
final MigrationHandler handler = findMigrationHandler(emptyList(), Class.class.cast(p.getJavaType()), services);
if (handler == NO_MIGRATION) {
return null;
}
final String prefix = p.getPath();
return (Function<Map<String, String>, Map<String, String>>) map -> {
final String version = map.get(String.format("%s.__version", prefix));
final Map<String, String> result;
if (version != null) {
final Map<String, String> migrated = ofNullable(handler.migrate(Integer.parseInt(version.trim()), map.entrySet().stream().filter(e -> e.getKey().startsWith(prefix + '.')).collect(toMap(e -> e.getKey().substring(prefix.length() + 1), Map.Entry::getValue)))).orElseGet(Collections::emptyMap);
result = migrated.entrySet().stream().collect(toMap(e -> prefix + '.' + e.getKey(), Map.Entry::getValue));
} else {
log.debug("No version for {} so skipping any potential migration", p.getJavaType().toString());
result = map;
}
return result;
};
}).filter(Objects::nonNull).reduce(NO_MIGRATION, (current, partial) -> (incomingVersion, incomingData) -> current.migrate(incomingVersion, partial.apply(incomingData)), (migrationHandler, migrationHandler2) -> (incomingVersion, incomingData) -> migrationHandler2.migrate(incomingVersion, migrationHandler.migrate(incomingVersion, incomingData)));
return ofNullable(type.getAnnotation(Version.class)).map(Version::migrationHandler).filter(t -> t != MigrationHandler.class).flatMap(t -> Stream.of(t.getConstructors()).sorted((o1, o2) -> o2.getParameterCount() - o1.getParameterCount()).findFirst()).map(t -> services.getServices().computeIfAbsent(t.getDeclaringClass(), k -> {
try {
return t.newInstance(reflections.parameterFactory(t, services.getServices()).apply(emptyMap()));
} catch (final InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
} catch (final InvocationTargetException e) {
throw toRuntimeException(e);
}
})).map(MigrationHandler.class::cast).map(h -> {
if (implicitMigrationHandler == NO_MIGRATION) {
return h;
}
return (MigrationHandler) (incomingVersion, incomingData) -> h.migrate(incomingVersion, implicitMigrationHandler.migrate(incomingVersion, incomingData));
}).orElse(implicitMigrationHandler);
}
Aggregations