use of org.mule.runtime.module.extension.internal.config.dsl.object.ParsingDelegate in project mule by mulesoft.
the class ExtensionDefinitionParser method getValueResolverFromMetadataType.
private ValueResolver getValueResolverFromMetadataType(final String parameterName, MetadataType expectedType, final Object value, final Object defaultValue, final boolean acceptsReferences, final Class<?> expectedClass) {
final Reference<ValueResolver> resolverValueHolder = new Reference<>();
expectedType.accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
if (conversionService.canConvert(value.getClass(), expectedClass)) {
resolverValueHolder.set(new StaticValueResolver<>(convertSimpleValue(value, expectedClass, parameterName)));
} else {
defaultVisit(metadataType);
}
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
resolverValueHolder.set(parseDate(value, dateTimeType, defaultValue));
}
@Override
public void visitDate(DateType dateType) {
resolverValueHolder.set(parseDate(value, dateType, defaultValue));
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
defaultVisit(objectType);
return;
}
ValueResolver valueResolver;
Optional<? extends ParsingDelegate> delegate = locateParsingDelegate(valueResolverParsingDelegates, objectType);
Optional<DslElementSyntax> typeDsl = dslResolver.resolve(objectType);
if (delegate.isPresent() && typeDsl.isPresent()) {
valueResolver = (ValueResolver) delegate.get().parse(value.toString(), objectType, typeDsl.get());
} else {
valueResolver = acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), objectType, null) : new StaticValueResolver<>(value);
}
resolverValueHolder.set(valueResolver);
}
@Override
protected void defaultVisit(MetadataType metadataType) {
ValueResolver delegateResolver = locateParsingDelegate(valueResolverParsingDelegates, metadataType).map(delegate -> delegate.parse(value.toString(), metadataType, null)).orElseGet(() -> acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), metadataType, null) : new TypeSafeValueResolverWrapper(new StaticValueResolver<>(value), expectedClass));
resolverValueHolder.set(delegateResolver);
}
});
return resolverValueHolder.get();
}
Aggregations