use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class NullSafeValueResolverWrapper method of.
/**
* Creates a new instance
*
* @param delegate the {@link ValueResolver} to wrap
* @param type the type of the value this resolver returns
* @param reflectionCache the cache for expensive reflection lookups
* @param muleContext the current {@link MuleContext}
* @param <T> the generic type of the produced values
* @return a new null safe {@link ValueResolver}
* @throws IllegalParameterModelDefinitionException if used on parameters of not supported types
*/
public static <T> ValueResolver<T> of(ValueResolver<T> delegate, MetadataType type, ReflectionCache reflectionCache, MuleContext muleContext, ObjectTypeParametersResolver parametersResolver) {
checkArgument(delegate != null, "delegate cannot be null");
Reference<ValueResolver> wrappedResolver = new Reference<>();
type.accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
Class clazz = getType(objectType);
if (isMap(objectType)) {
ValueResolver<?> fallback = MapValueResolver.of(clazz, emptyList(), emptyList(), reflectionCache, muleContext);
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
return;
}
String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
if (!isBlank(requiredFields)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it contains non optional fields: [%s]", clazz.getName(), requiredFields));
}
wrappedResolver.set(delegate);
return;
}
ResolverSet resolverSet = new ResolverSet(muleContext);
for (Field field : getFields(clazz)) {
ValueResolver<?> fieldResolver = null;
ObjectFieldType objectField = objectType.getFieldByName(getAlias(field)).orElse(null);
if (objectField == null) {
continue;
}
Optional<String> defaultValue = getDefaultValue(objectField);
// TODO MULE-13066 Extract ParameterResolver logic into a centralized resolver
if (defaultValue.isPresent()) {
fieldResolver = getFieldDefaultValueValueResolver(objectField, muleContext);
} else if (isFlattenedParameterGroup(objectField)) {
DefaultObjectBuilder groupBuilder = new DefaultObjectBuilder<>(getType(objectField.getValue()));
resolverSet.add(field.getName(), new ObjectBuilderValueResolver<T>(groupBuilder, muleContext));
ObjectType childGroup = (ObjectType) objectField.getValue();
parametersResolver.resolveParameters(childGroup, groupBuilder);
parametersResolver.resolveParameterGroups(childGroup, groupBuilder);
} else {
NullSafe nullSafe = field.getAnnotation(NullSafe.class);
if (nullSafe != null) {
MetadataType nullSafeType;
if (Object.class.equals(nullSafe.defaultImplementingType())) {
nullSafeType = objectField.getValue();
} else {
nullSafeType = new BaseTypeBuilder(JAVA).objectType().with(new TypeIdAnnotation(nullSafe.defaultImplementingType().getName())).build();
}
fieldResolver = NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), nullSafeType, reflectionCache, muleContext, parametersResolver);
}
if (field.getAnnotation(ConfigOverride.class) != null) {
ValueResolver<?> fieldDelegate = fieldResolver != null ? fieldResolver : new StaticValueResolver<>(null);
fieldResolver = ConfigOverrideValueResolverWrapper.of(fieldDelegate, field.getName(), reflectionCache, muleContext);
}
}
if (fieldResolver != null) {
resolverSet.add(field.getName(), fieldResolver);
}
}
ObjectBuilder<T> objectBuilder = new DefaultResolverSetBasedObjectBuilder<T>(clazz, resolverSet);
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, new ObjectBuilderValueResolver(objectBuilder, muleContext), muleContext));
}
@Override
public void visitArrayType(ArrayType arrayType) {
Class collectionClass = getType(arrayType);
ValueResolver<?> fallback = CollectionValueResolver.of(collectionClass, emptyList());
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
}
@Override
protected void defaultVisit(MetadataType metadataType) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it is of a simple type", getType(metadataType).getName()));
}
wrappedResolver.set(delegate);
}
});
return wrappedResolver.get();
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class ParametersResolver method getParameterValueResolver.
private ValueResolver<Object> getParameterValueResolver(ParameterModel parameterModel, String parameterName, MuleContext muleContext) {
ValueResolver<?> resolver;
if (parameters.containsKey(parameterName)) {
resolver = toValueResolver(parameters.get(parameterName), parameterModel.getModelProperties());
} else {
// TODO MULE-13066 Extract ParameterResolver logic into a centralized resolver
resolver = getDefaultValueResolver(parameterModel, this.muleContext);
}
if (isNullSafe(parameterModel)) {
ValueResolver<?> delegate = resolver != null ? resolver : new StaticValueResolver<>(null);
MetadataType type = parameterModel.getModelProperty(NullSafeModelProperty.class).get().defaultType();
resolver = NullSafeValueResolverWrapper.of(delegate, type, reflectionCache, this.muleContext, this);
}
if (parameterModel.isOverrideFromConfig()) {
resolver = ConfigOverrideValueResolverWrapper.of(resolver != null ? resolver : new StaticValueResolver<>(null), parameterName, reflectionCache, this.muleContext);
}
return (ValueResolver<Object>) resolver;
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class DsqlQueryMetadataResolverTestCase method getTrimmedOutputMetadata.
@Test
public void getTrimmedOutputMetadata() throws MetadataResolvingException, ConnectionException {
DsqlQuery dsqlQuery = dsqlParser.parse("dsql:SELECT id FROM Circle WHERE (diameter < 18)");
MetadataType outputMetadata = getQueryMetadataResolver().getOutputType(context, dsqlQuery);
ObjectType type = getAndAssertTypeOf(outputMetadata);
assertThat(type.getFields(), hasSize(1));
ObjectFieldType onlyField = type.getFields().iterator().next();
assertThat(onlyField.getValue(), is(instanceOf(NumberType.class)));
assertThat(onlyField.getKey().getName().getLocalPart(), is("id"));
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class ExtensionWithCustomStaticTypesTestCase method assertJsonPerson.
private void assertJsonPerson(Typed typed) {
MetadataType type = typed.getType();
assertThat(typed.hasDynamicType(), is(false));
assertThat(type.getMetadataFormat(), is(JSON));
assertThat(type, instanceOf(ObjectType.class));
assertThat(((ObjectType) type).getFields(), hasSize(3));
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class JavaDeclarationDelegateTestCase method assertTestModuleMessageSource.
private void assertTestModuleMessageSource(ExtensionDeclaration extensionDeclaration) throws Exception {
assertThat(extensionDeclaration.getMessageSources(), hasSize(1));
SourceDeclaration source = extensionDeclaration.getMessageSources().get(0);
assertThat(source.getName(), is(DEA_SOURCE_NAME));
final MetadataType outputType = source.getOutput().getType();
assertThat(outputType, is(instanceOf(ArrayType.class)));
assertMessageType(((ArrayType) outputType).getType(), STRING_TYPE, TYPE_LOADER.load(DEAOfficerAttributes.class));
assertThat(source.getOutputAttributes().getType(), equalTo(TYPE_LOADER.load(Object.class)));
ConfigurationDeclaration config = extensionDeclaration.getConfigurations().get(0);
assertThat(config.getMessageSources(), hasSize(3));
assertHeisenbergSource(config.getMessageSources().get(0), ASYNC_SOURCE_NAME, AsyncHeisenbergSource.class);
assertHeisenbergSource(config.getMessageSources().get(1), SOURCE_NAME, HeisenbergSource.class);
}
Aggregations