brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neykov <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #462: Inherit config default values
Date Fri, 06 Jan 2017 17:42:48 GMT
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/462#discussion_r94968550
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/objs/BasicSpecParameter.java ---
    @@ -404,7 +431,126 @@ public static void addParameters(AbstractBrooklynObjectSpec<?,
?> spec, List<? e
                 spec.parametersAdd(BasicSpecParameter.fromSpec(loader.getManagementContext(),
spec));
             }
             if (explicitParams.size() > 0) {
    -            spec.parametersAdd(explicitParams);
    +            spec.parametersReplace(resolveParameters(explicitParams, spec.getParameters(),
spec));
    +        }
    +    }
    +
    +    /** merge parameters against other parameters and known and type-inherited config
keys */
    +    static Collection<SpecParameter<?>> resolveParameters(Collection<?
extends SpecParameter<?>> newParams, Collection<? extends SpecParameter<?>>
existingReferenceParamsToKeep, AbstractBrooklynObjectSpec<?,?> spec) {
    +        Map<String,SpecParameter<?>> existingToKeep = MutableMap.of();
    +        if (existingReferenceParamsToKeep!=null) {
    +            for (SpecParameter<?> p: existingReferenceParamsToKeep) { 
    +                existingToKeep.put(p.getConfigKey().getName(), p);
    +            }
    +        }
    +
    +        List<SpecParameter<?>> result = MutableList.<SpecParameter<?>>of();
    +        for (SpecParameter<?> p: newParams) {
    +            if (p instanceof SpecParameterForInheritance) {
    +                SpecParameter<?> existingP = existingToKeep.remove(p.getConfigKey().getName());
    +                if (existingP!=null) {
    +                    p = ((SpecParameterForInheritance<?>)p).resolveWithAncestor(existingP);
    +                } else {
    +                    // TODO find config keys on the type (not available as parameters)
    +                    /* we don't currently do this due to low priority; all it means if
there is a config key in java,
    +                     * and a user wishes to expose it as a parameter, they have to redeclare
everything;
    +                     * nothing from the config key in java will be inherited */
    +                    p = ((SpecParameterForInheritance<?>)p).resolveWithAncestor((ConfigKey<?>)null);
    +                }
    +                result.add(p);
    +            } else {
    +                existingToKeep.remove(p.getConfigKey().getName());
    +                result.add(p);
    +            }
    +        }
    +        result.addAll(existingToKeep.values());
    +        return result;
    +    }
    +
    +    /** instance of {@link SpecParameter} which includes information about what fields
are explicitly set,
    +     * for use with a subsequent merge */
    +    @SuppressWarnings("serial")
    +    @Beta
    +    static class SpecParameterForInheritance<T> extends BasicSpecParameter<T>
{
    +
    +        private final boolean hasType, hasLabelSet, hasPinnedSet, hasDefaultValue, hasConstraints,
hasRuntimeInheritance, hasTypeInheritance;
    +
    +        private <SensorType> SpecParameterForInheritance(String label, Boolean
pinned, ConfigKey<T> config, AttributeSensor<SensorType> sensor,
    +                boolean hasType, boolean hasDefaultValue, boolean hasConstraints, boolean
hasRuntimeInheritance, boolean hasTypeInheritance) {
    +            super(Preconditions.checkNotNull(label!=null ? label : config.getName(),
"label or config name must be set"), 
    +                    pinned==null ? true : pinned, config, sensor);
    +            this.hasType = hasType;
    +            this.hasLabelSet = label!=null;
    +            this.hasPinnedSet = pinned!=null;
    +            this.hasDefaultValue = hasDefaultValue;
    +            this.hasConstraints = hasConstraints;
    +            this.hasRuntimeInheritance = hasRuntimeInheritance;
    +            this.hasTypeInheritance = hasTypeInheritance;
    +        }
    +
    +        /** used if yaml specifies a parameter, but possibly incomplete, and a spec supertype
has a parameter */
    +        SpecParameter<?> resolveWithAncestor(SpecParameter<?> ancestor) {
    +            if (ancestor==null) return new BasicSpecParameter<>(getLabel(), isPinned(),
getConfigKey(), getSensor());
    +
    +            return new BasicSpecParameter<>(
    +                    hasLabelSet ? getLabel() : ancestor.getLabel(), 
    +                            hasPinnedSet ? isPinned() : ancestor.isPinned(), 
    +                                    resolveWithAncestorConfigKey(ancestor.getConfigKey()),

    +                                    hasType ? getSensor() : ancestor.getSensor());
    +        }
    +
    +        /** as {@link #resolveWithAncestor(SpecParameter)} but where the param redefines/extends
a config key coming from a java supertype, rather than a parameter */
    +        // TODO not used yet; see calls to the other resolveWithAncestor method,
    +        // and see BrooklynComponentTemplateResolver.findAllConfigKeys;
    +        // also note whilst it is easiest to do this here, logically it is messy,
    +        // and arguably it should be done when converting the spec to an instance
    +        SpecParameter<?> resolveWithAncestor(ConfigKey<?> ancestor) {
    +            if (ancestor==null) return new BasicSpecParameter<>(getLabel(), isPinned(),
getConfigKey(), getSensor());
    +
    +            // TODO probably want to do this (but it could get expensive!)
    +            //          Set<Class<?>> types = MutableSet.<Class<?>>builder()
    +            //                  .add(spec.getImplementation())
    +            //                  .add(spec.getType())
    +            //                  .addAll(spec.getAdditionalInterfaces())
    +            //                  .remove(null)
    +            //                  .build();
    +            //          // order above is important, respected below to take the first
one defined 
    +            //          MutableMap<String, ConfigKey<?>> result = MutableMap.copyOf(FlagUtils.findAllConfigKeys(null,
types));
    +
    +            return new BasicSpecParameter<>(
    +                    getLabel(), 
    +                    isPinned(), 
    +                    resolveWithAncestorConfigKey(ancestor),
    +                    // TODO port sensor will be lost (see messy code above which sets
the port sensor)
    +                    getSensor());
    +        }
    +
    +        @SuppressWarnings({ "unchecked", "rawtypes" })
    +        private ConfigKey<?> resolveWithAncestorConfigKey(ConfigKey<?> ancestor)
{
    +            ConfigKey<?> dominant = getConfigKey();
    +            return ConfigKeys.<Object>builder((TypeToken)(hasType ? dominant :
ancestor).getTypeToken())
    +                    .name(dominant.getName())
    +                    .description((dominant.getDescription()!=null ? dominant : ancestor).getDescription())
    +                    .defaultValue((hasDefaultValue ? dominant : ancestor).getDefaultValue())
    +                    .constraint((Predicate<Object>) (hasConstraints ? dominant
: ancestor).getConstraint())
    +                    .runtimeInheritance( (hasRuntimeInheritance ? dominant : ancestor).getInheritanceByContext(InheritanceContext.RUNTIME_MANAGEMENT))
    +                    .typeInheritance( (hasTypeInheritance ? dominant : ancestor).getInheritanceByContext(InheritanceContext.TYPE_DEFINITION))
    +                    // not yet configurable from yaml
    +                    //.reconfigurable()
    +                    .build();
    +        }
    +
    +        public boolean isHasDefaultValue() {
    --- End diff --
    
    This just sounds wrong :). But then need to stick to convention.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message