Search in sources :

Example 1 with HashedResolverSet

use of org.mule.runtime.module.extension.internal.runtime.resolver.HashedResolverSet in project mule by mulesoft.

the class ImplicitObjectUtils method buildImplicitResolverSet.

/**
 * Creates a {@link ResolverSet} based on the default values for the {@link ParameterModel}s in the {@code parametrizedModel}.
 * <p>
 * If a {@link ParameterModel} returns {@code null} for {@link ParameterModel#getDefaultValue()} then it's ignored
 *
 * @param model a model holding the {@link ParameterModel}s to consider
 * @param muleContext the Mule node.
 * @return a {@link ResolverSet}
 */
public static ResolverSet buildImplicitResolverSet(ParameterizedModel model, ReflectionCache reflectionCache, MuleContext muleContext) {
    ResolverSet resolverSet = new HashedResolverSet(muleContext);
    ParametersResolver parametersResolver = ParametersResolver.fromDefaultValues(model, muleContext, reflectionCache);
    for (ParameterGroupModel groupModel : model.getParameterGroupModels()) {
        Optional<ParameterGroupDescriptor> descriptor = groupModel.getModelProperty(ParameterGroupModelProperty.class).map(ParameterGroupModelProperty::getDescriptor);
        if (descriptor.isPresent() && groupModel.getParameterModels().stream().noneMatch(ParameterModel::isRequired)) {
            String groupKey = getContainerName(descriptor.get().getContainer());
            resolverSet.add(groupKey, NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), descriptor.get().getMetadataType(), reflectionCache, muleContext, parametersResolver));
        } else {
            groupModel.getParameterModels().forEach(parameterModel -> {
                Object defaultValue = parameterModel.getDefaultValue();
                ValueResolver<?> resolver;
                if (defaultValue instanceof String) {
                    resolver = getExpressionBasedValueResolver((String) defaultValue, parameterModel.getType(), muleContext);
                } else {
                    resolver = new StaticValueResolver<>(null);
                }
                if (parameterModel.getModelProperty(NullSafeModelProperty.class).isPresent()) {
                    MetadataType metadataType = parameterModel.getModelProperty(NullSafeModelProperty.class).get().defaultType();
                    resolver = NullSafeValueResolverWrapper.of(resolver, metadataType, reflectionCache, muleContext, parametersResolver);
                }
                resolverSet.add(parameterModel.getName(), resolver);
            });
        }
    }
    return resolverSet;
}
Also used : HashedResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.HashedResolverSet) ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) NullSafeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.NullSafeModelProperty) HashedResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.HashedResolverSet) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType) ParametersResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ParametersResolver) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)

Aggregations

MetadataType (org.mule.metadata.api.model.MetadataType)1 ParameterGroupModel (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)1 ParameterGroupDescriptor (org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor)1 NullSafeModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.NullSafeModelProperty)1 ParameterGroupModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty)1 HashedResolverSet (org.mule.runtime.module.extension.internal.runtime.resolver.HashedResolverSet)1 ParametersResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ParametersResolver)1 ResolverSet (org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet)1 StaticValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)1