use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class MetadataInputDelegate method getInputMetadataDescriptors.
/**
* For each of the Component's {@link ParameterModel} creates the corresponding {@link TypeMetadataDescriptor} using only its
* static {@link MetadataType} and ignoring if any parameter has a dynamic type.
*
* @return A {@link List} containing a {@link MetadataResult} of {@link TypeMetadataDescriptor} for each input parameter using
* only its static {@link MetadataType} and ignoring if any parameter has a dynamic type.
*/
MetadataResult<InputMetadataDescriptor> getInputMetadataDescriptors(MetadataContext context, Object key) {
InputMetadataDescriptor.InputMetadataDescriptorBuilder input = InputMetadataDescriptor.builder();
List<MetadataResult<ParameterMetadataDescriptor>> results = new LinkedList<>();
for (ParameterModel parameter : component.getAllParameterModels()) {
MetadataResult<ParameterMetadataDescriptor> result = getParameterMetadataDescriptor(parameter, context, key);
input.withParameter(parameter.getName(), result.get());
results.add(result);
}
List<MetadataFailure> failures = results.stream().flatMap(e -> e.getFailures().stream()).collect(toList());
return failures.isEmpty() ? success(input.build()) : failure(input.build(), failures);
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class MetadataInputDelegate method getParameterMetadata.
/**
* Given a {@link MetadataKey} of a type and a {@link MetadataContext}, resolves the {@link MetadataType} of the
* {@code parameter} using the {@link InputTypeResolver} associated to the current component.
*
* @param context {@link MetadataContext} of the MetaData resolution
* @param key {@link MetadataKey} of the type which's structure has to be resolved
* @return a {@link MetadataResult} with the {@link MetadataType} of the {@code parameter}.
*/
private MetadataResult<MetadataType> getParameterMetadata(ParameterModel parameter, MetadataContext context, Object key) {
try {
boolean allowsNullType = !parameter.isRequired() && (parameter.getDefaultValue() == null);
MetadataType metadata = resolverFactory.getInputResolver(parameter.getName()).getInputMetadata(context, key);
if (isMetadataResolvedCorrectly(metadata, allowsNullType)) {
return success(adaptToListIfNecessary(metadata, parameter, context));
}
MetadataFailure failure = newFailure().withMessage(format("Error resolving metadata for the [%s] input parameter", parameter.getName())).withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE).withReason(NULL_TYPE_ERROR).onParameter(parameter.getName());
return failure(parameter.getType(), failure);
} catch (Exception e) {
return failure(parameter.getType(), newFailure(e).onParameter(parameter.getName()));
}
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class MetadataKeyIdObjectResolver method resolve.
/**
* Returns the populated key in the Type that the component parameter requires by looking for default values, if no
* {@link MetadataKeyId} is present an empty value is returned since is a key less component.
* <p>
* If a key should be built and there is at least one default value missing an {@link IllegalArgumentException} is thrown.
*
* @return a new instance of the {@link MetadataKeyId} parameter {@code type}.
* @throws MetadataResolvingException if the Parameter type is not instantiable.
* @throws IllegalArgumentException if cannot found the required default values for an specified key.
*/
public Object resolve() throws MetadataResolvingException {
if (isKeyLess()) {
return NullMetadataKey.ID;
}
if (!keyParts.stream().allMatch(p -> p.getDefaultValue() != null)) {
throw new IllegalArgumentException("Could not build metadata key from an object that does" + " not have a default value for all it's components.");
}
String id = keyParts.get(0).getDefaultValue().toString();
final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
MetadataType type = keyIdModelProperty.getType();
KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(id, getType(type)) {
@Override
protected Map<Field, String> getFieldValuesMap() {
return keyParts.stream().filter(p -> p.getModelProperty(DeclaringMemberModelProperty.class).isPresent()).collect(toMap(p -> p.getModelProperty(DeclaringMemberModelProperty.class).get().getDeclaringField(), p -> p.getDefaultValue().toString()));
}
};
type.accept(visitor);
return visitor.getResultId();
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class MetadataKeyIdObjectResolver method resolve.
/**
* Given {@link MetadataKey}, return the populated key in the Type that the component parameter requires.
*
* @param key the {@link MetadataKey} associated to the {@link MetadataKeyId}
* @return a new instance of the {@link MetadataKeyId} parameter {@code type} with the values of the passed {@link MetadataKey}
* @throws MetadataResolvingException if:
* <ul>
* <li>Parameter types is not instantiable</li>
* <li>{@param key} does not provide the required levels</li>
* </ul>
*/
public Object resolve(MetadataKey key) throws MetadataResolvingException {
if (isKeyLess()) {
return NullMetadataKey.ID;
}
final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
MetadataType type = keyIdModelProperty.getType();
KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(key.getId(), getType(type)) {
@Override
protected Map<Field, String> getFieldValuesMap() throws MetadataResolvingException {
return keyToFieldValueMap(key);
}
};
type.accept(visitor);
return visitor.getResultId();
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class MetadataOutputDelegate method resolveOutputAttributesMetadata.
private MetadataResult<MetadataType> resolveOutputAttributesMetadata(MetadataContext context, Object key, Function<MetadataType, Boolean> metadataValidator) {
try {
MetadataType metadata = resolverFactory.getOutputAttributesResolver().getAttributesType(context, key);
if (metadataValidator.apply(metadata)) {
return success(metadata);
}
MetadataFailure failure = newFailure().withMessage("Error resolving Output Attributes metadata").withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE).withReason(NULL_TYPE_ERROR).onOutputAttributes();
return failure(failure);
} catch (Exception e) {
return failure(newFailure(e).onOutputAttributes());
}
}
Aggregations