[
https://issues.apache.org/jira/browse/BROOKLYN-433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15863583#comment-15863583
]
ASF GitHub Bot commented on BROOKLYN-433:
-----------------------------------------
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/558#discussion_r100783030
--- Diff:
core/src/main/java/org/apache/brooklyn/core/objs/BasicSpecParameter.java ---
@@ -310,6 +317,41 @@ private static Predicate parseConstraints(Object obj,
BrooklynClassLoadingContex
}
}
+ private static Predicate<?> parseConstraint(Object
untypedConstraint, BrooklynClassLoadingContext loader) {
+ // TODO Could try to handle deferred supplier as well?
+ if (untypedConstraint instanceof Predicate) {
+ // An explicit predicate (e.g. via "$brooklyn:object: ...")
+ return (Predicate<?>) untypedConstraint;
+ } else if (untypedConstraint instanceof String) {
+ // build-in simple declaration, such as "required"
+ String constraint = (String)untypedConstraint;
+ if (BUILT_IN_CONSTRAINTS.containsKey(constraint)) {
+ return BUILT_IN_CONSTRAINTS.get(constraint);
+ } else {
+ throw new IllegalArgumentException("The constraint '"
+ constraint + "' for a catalog input is not "
+ + "recognized as a built-in (" +
BUILT_IN_CONSTRAINTS.keySet() + " or "
+ + BUILT_IN_CONSTRAINT_FACTORIES.keySet() +
")");
+ }
+ } else if (untypedConstraint instanceof Map) {
+ // For example "regex: foo.*"
+ Map<?,?> constraint = (Map<?,?>)untypedConstraint;
+ if (constraint.size() == 1) {
+ Object key =
Iterables.getOnlyElement(constraint.keySet());
+ Object val = constraint.get(key);
+ if (BUILT_IN_CONSTRAINT_FACTORIES.containsKey(key)) {
+ Function<Object, Predicate<?>> factory =
BUILT_IN_CONSTRAINT_FACTORIES.get(key);
+ return factory.apply(val);
+ } else {
+ throw new IllegalArgumentException("The constraint
'" + constraint + "' for a catalog input is not recognized as a built-in (" +
BUILT_IN_CONSTRAINTS.keySet() + ")");
+ }
+ } else {
+ throw new IllegalArgumentException("The constraint '"
+ constraint + "' for a catalog input is not recognized - cannot handle
multiple key:vales");
--- End diff --
Change to `The config key constraint '" + constraint + "' is not supported
- it can handle only single key:value constraint.`
> YAML-based config constraint to support regex
> ---------------------------------------------
>
> Key: BROOKLYN-433
> URL: https://issues.apache.org/jira/browse/BROOKLYN-433
> Project: Brooklyn
> Issue Type: Improvement
> Affects Versions: 0.10.0
> Reporter: Aled Sage
> Assignee: Aled Sage
> Priority: Minor
>
> One can specify constraints on a config key's value, to give a validation
> error if an invalid value is supplied.
> In the Java code, this can be any predicate.
> However, in YAML we don't support many options. It would be good to support a
> regex constraint. For example:
> {noformat}
> brooklyn.parameters:
> - name: address
> type: string
> constraints:
> - required
> - regex: ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
> {noformat}
> See docs at
> https://raw.githubusercontent.com/apache/brooklyn-docs/master/guide/yaml/yaml-reference.md,
> and code at
> https://github.com/apache/brooklyn-server/blob/master/core/src/main/java/org/apache/brooklyn/core/objs/BasicSpecParameter.java#L204-L205
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)