use of org.mule.runtime.extension.api.dsql.Field 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