private Object findCollectionField(final Object rootInstance, final ParameterMeta param) { final Collection<?> collection = Collection.class.cast(rootInstance); if (!collection.isEmpty()) { final Object next = collection.iterator().next(); if (param.getPath().endsWith("[${index}]")) { return next; } return findField(next, param); } return null; }
public ParameterMeta newBulkParameter() { return new ParameterMeta(new ParameterMeta.Source() { @Override public String name() { return name; } @Override public Class<?> declaringClass() { return MaxBatchSizeParamBuilder.class; } }, Integer.class, ParameterMeta.Type.NUMBER, root.getPath() + "." + name, name, concat(Stream.of(MaxBatchSizeParamBuilder.class.getPackage().getName()), Stream.of(ofNullable(root.getI18nPackages()).orElse(new String[0]))).toArray(String[]::new), emptyList(), emptyList(), new HashMap<String, String>() { { put("tcomp::ui::defaultvalue::value", String.valueOf(defaultValue)); put("tcomp::validation::min", "1"); } }, true); }
private <C, T, A> A create(final Class<A> api, final Class<T> componentType, final C configuration) { final ComponentFamilyMeta.BaseMeta<? extends Lifecycle> meta = findMeta(componentType); return api .cast(meta .getInstantiator() .apply(configuration == null || meta.getParameterMetas().isEmpty() ? emptyMap() : configurationByExample(configuration, meta .getParameterMetas() .stream() .filter(p -> p.getName().equals(p.getPath())) .findFirst() .map(p -> p.getName() + '.') .orElseThrow(() -> new IllegalArgumentException( "Didn't find any option and therefore " + "can't convert the configuration instance to a configuration"))))); }
private String toAsciidoctor(final ParameterMeta p, final DefaultValueInspector.Instance instance, final ParameterBundle parent) { final ParameterBundle bundle = findBundle(p); return "|" + bundle.displayName(parent).orElse(p.getName()) + '|' + bundle.documentation(parent).orElseGet(() -> findDocumentation(p)) + '|' + ofNullable(findDefault(p, instance)).orElse("-") + '|' + renderConditions(p.getPath(), p.getMetadata()) + '|' + p.getPath(); }
private Object findField(final Object rootInstance, final ParameterMeta param) { if (param.getPath().startsWith("$") || param.getName().startsWith("$")) { // virtual param return null; } if (Collection.class.isInstance(rootInstance)) { return findCollectionField(rootInstance, param); } Class<?> current = rootInstance.getClass(); while (current != null) { try { final Field declaredField = current.getDeclaredField(findName(param)); if (!declaredField.isAccessible()) { declaredField.setAccessible(true); } return declaredField.get(rootInstance); } catch (final IllegalAccessException | NoSuchFieldException e) { // next } current = current.getSuperclass(); } throw new IllegalArgumentException("Didn't find field '" + param.getName() + "' in " + rootInstance); }
public ConditionGroup build(final ParameterMeta param) { final boolean and = "AND".equalsIgnoreCase(param.getMetadata().getOrDefault("tcomp::condition::ifs::operator", "AND")); return new ConditionGroup(param .getMetadata() .entrySet() .stream() .filter(meta -> meta.getKey().startsWith("tcomp::condition::if::target")) .map(meta -> { final String[] split = meta.getKey().split("::"); final String index = split.length == 5 ? "::" + split[split.length - 1] : ""; final String valueKey = "tcomp::condition::if::value" + index; final String negateKey = "tcomp::condition::if::negate" + index; final String evaluationStrategyKey = "tcomp::condition::if::evaluationStrategy" + index; final String absoluteTargetPath = pathResolver.resolveProperty(param.getPath(), meta.getValue()); return new Condition(toPointer(absoluteTargetPath), Boolean.parseBoolean(param.getMetadata().getOrDefault(negateKey, "false")), param.getMetadata().getOrDefault(evaluationStrategyKey, "DEFAULT").toUpperCase(ROOT), param.getMetadata().getOrDefault(valueKey, "true").split(",")); }) .collect(toList()), and ? stream -> stream.allMatch(i -> i) : stream -> stream.anyMatch(i -> i)); }
final String name) { final JsonArray array; if (definitions.size() == 1 && definitions.iterator().next().getPath().endsWith("[${index}]")) { // primitive final ParameterMeta primitiveMeta = definitions.stream().iterator().next(); array = config
private ParameterMeta translate(final ParameterMeta config, final int replacedPrefixLen, final String newPrefix) { return new ParameterMeta(config.getSource(), config.getJavaType(), config.getType(), newPrefix + config.getPath().substring(replacedPrefixLen), config.getPath().length() == replacedPrefixLen ? newPrefix : config.getName(), config.getI18nPackages(), config .getNestedParameters() .stream() .map(it -> translate(it, replacedPrefixLen, newPrefix)) .collect(toList()), config.getProposals(), config.getMetadata(), config.isLogMissingResourceBundle()); } }
private Class<?> toJavaType(final ParameterMeta p) { if (p.getType().equals(OBJECT) || p.getType().equals(ENUM)) { if (Class.class.isInstance(p.getJavaType())) { return Class.class.cast(p.getJavaType()); } throw new IllegalArgumentException("Unsupported type for parameter " + p.getPath() + " (from " + p.getSource().declaringClass() + "), ensure it is a Class<?>"); } if (p.getType().equals(ARRAY) && ParameterizedType.class.isInstance(p.getJavaType())) { final ParameterizedType parameterizedType = ParameterizedType.class.cast(p.getJavaType()); final Type[] arguments = parameterizedType.getActualTypeArguments(); if (arguments.length == 1 && Class.class.isInstance(arguments[0])) { return Class.class.cast(arguments[0]); } throw new IllegalArgumentException("Unsupported type for parameter " + p.getPath() + " (from " + p.getSource().declaringClass() + "), " + "ensure it is a ParameterizedType with one argument"); } throw new IllegalStateException("Parameter '" + p.getName() + "' is not an object."); }
private void addProcessorsBuiltInParameters(final Class<?> type, final List<ParameterMeta> parameterMetas) { final ParameterMeta root = parameterMetas.stream().filter(p -> p.getName().equals(p.getPath())).findFirst().orElseGet(() -> { final ParameterMeta umbrella = new ParameterMeta(new ParameterMeta.Source() {
errors.add(MESSAGES.required(meta.getPath())); if (value.getValueType() == JsonValue.ValueType.NUMBER && JsonNumber.class.cast(value).doubleValue() < bound) { errors.add(MESSAGES.min(meta.getPath(), bound, JsonNumber.class.cast(value).doubleValue())); if (value.getValueType() == JsonValue.ValueType.NUMBER && JsonNumber.class.cast(value).doubleValue() > bound) { errors.add(MESSAGES.max(meta.getPath(), bound, JsonNumber.class.cast(value).doubleValue())); final String val = JsonString.class.cast(value).getString(); if (val.length() < bound) { errors.add(MESSAGES.minLength(meta.getPath(), bound, val.length())); final String val = JsonString.class.cast(value).getString(); if (val.length() > bound) { errors.add(MESSAGES.maxLength(meta.getPath(), bound, val.length())); final double bound = Double.parseDouble(min); if (value.getValueType() == JsonValue.ValueType.ARRAY && value.asJsonArray().size() < bound) { errors.add(MESSAGES.minItems(meta.getPath(), bound, value.asJsonArray().size())); final double bound = Double.parseDouble(max); if (value.getValueType() == JsonValue.ValueType.ARRAY && value.asJsonArray().size() > bound) { errors.add(MESSAGES.maxItems(meta.getPath(), bound, value.asJsonArray().size())); final JsonArray array = value.asJsonArray(); if (new HashSet<>(array).size() != array.size()) { errors.add(MESSAGES.uniqueItems(meta.getPath()));
private Stream<SimplePropertyDefinition> buildProperties(final List<ParameterMeta> meta, final ClassLoader loader, final Locale locale, final DefaultValueInspector.Instance rootInstance, final ParameterMeta parent) { return meta.stream().flatMap(p -> { final String path = sanitizePropertyName(p.getPath()); final String name = sanitizePropertyName(p.getName()); final String type = p.getType().name();
.build())); final Map<String, String> configToMigrate = new HashMap<>(config); final String versionKey = configuration.getMeta().getPath() + ".__version"; final boolean addedVersion = configToMigrate.putIfAbsent(versionKey, Integer.toString(version)) == null; final Map<String, String> migrated = configuration.getMigrationHandler().migrate(version, configToMigrate);
final int prefixLen = c.getMeta().getPath().length(); final String forcedPrefix = c.getMeta().getName(); node
private Config createConfig(final String plugin, final ComponentManager.AllServices services, final ParameterMeta config, final String familyName, final String familyIcon, final MigrationHandlerFactory migrationHandlerFactory) { final Config c = new Config(); c.setIcon(familyIcon); c.setKey(getKey(familyName, config.getMetadata())); c.setMeta(translate(config, config.getPath().length(), "configuration")); c .setId(IdGenerator .get(plugin, c.getKey().getFamily(), c.getKey().getConfigType(), c.getKey().getConfigName())); if (Class.class.isInstance(config.getJavaType())) { final Class<?> clazz = Class.class.cast(config.getJavaType()); final Version version = clazz.getAnnotation(Version.class); if (version != null) { c.setVersion(version.value()); if (version.migrationHandler() != MigrationHandler.class) { c .setMigrationHandler(migrationHandlerFactory .findMigrationHandler(singletonList(c.getMeta()), clazz, services)); } } else { c.setVersion(-1); } } if (c.getMigrationHandler() == null) { c.setMigrationHandler((v, d) -> d); } return c; }