use of org.mule.metadata.api.builder.BaseTypeBuilder 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.builder.BaseTypeBuilder in project mule by mulesoft.
the class ExtensionsTestUtils method objectTypeBuilder.
public static TypeBuilder<?> objectTypeBuilder(Class<?> clazz) {
BaseTypeBuilder typeBuilder = BaseTypeBuilder.create(JAVA);
final TypeHandlerManager typeHandlerManager = new ExtensionsTypeHandlerManagerFactory().createTypeHandlerManager();
typeHandlerManager.handle(clazz, new ParsingContext(), typeBuilder);
return typeBuilder;
}
use of org.mule.metadata.api.builder.BaseTypeBuilder in project mule by mulesoft.
the class DsqlQueryMetadataResolver method getOutputType.
/**
* Automatically resolves the output metadata for the {@link DsqlQuery}.
* <p>
* The base entity is resolved using the component {@link QueryEntityResolver} and assuming the key of the entity is the DSQL
* {@link DsqlQuery#getType() type}.
*
* @param context {@link MetadataContext} of the MetaData resolution
* @param query the {@link DsqlQuery} to resolve the output metadata from.
*/
@Override
public MetadataType getOutputType(MetadataContext context, Object query) throws MetadataResolvingException, ConnectionException {
if (query instanceof DsqlQuery) {
DsqlQuery dsqlQuery = (DsqlQuery) query;
MetadataType entityMetadata = entityResolver.getEntityMetadata(context, dsqlQuery.getType().getName());
BaseTypeBuilder builder = context.getTypeBuilder();
final List<Field> fields = dsqlQuery.getFields();
if (fields.size() == 1 && fields.get(0).getName().equals("*")) {
return entityMetadata;
}
entityMetadata.accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
ObjectTypeBuilder objectTypeBuilder = builder.objectType();
objectType.getFields().stream().filter(p -> fields.stream().anyMatch(f -> f.getName().equalsIgnoreCase(p.getKey().getName().getLocalPart()))).forEach(p -> {
ObjectFieldTypeBuilder field = objectTypeBuilder.addField();
field.key(p.getKey().getName());
field.value(p.getValue());
});
}
});
return builder.build();
} else {
return nativeOutputResolver.getOutputType(context, query);
}
}
Aggregations