Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 8C953200C18 for ; Fri, 6 Jan 2017 18:42:50 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 8B5F4160B48; Fri, 6 Jan 2017 17:42:50 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 9E908160B51 for ; Fri, 6 Jan 2017 18:42:49 +0100 (CET) Received: (qmail 17948 invoked by uid 500); 6 Jan 2017 17:42:48 -0000 Mailing-List: contact dev-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list dev@brooklyn.apache.org Received: (qmail 17027 invoked by uid 99); 6 Jan 2017 17:42:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Jan 2017 17:42:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3D723DFC2F; Fri, 6 Jan 2017 17:42:48 +0000 (UTC) From: neykov To: dev@brooklyn.apache.org Reply-To: dev@brooklyn.apache.org References: In-Reply-To: Subject: [GitHub] brooklyn-server pull request #462: Inherit config default values Content-Type: text/plain Message-Id: <20170106174248.3D723DFC2F@git1-us-west.apache.org> Date: Fri, 6 Jan 2017 17:42:48 +0000 (UTC) archived-at: Fri, 06 Jan 2017 17:42:50 -0000 Github user neykov commented on a diff in the pull request: https://github.com/apache/brooklyn-server/pull/462#discussion_r94961965 --- Diff: core/src/main/java/org/apache/brooklyn/core/objs/BasicSpecParameter.java --- @@ -404,7 +431,126 @@ public static void addParameters(AbstractBrooklynObjectSpec spec, List 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> resolveParameters(Collection> newParams, Collection> existingReferenceParamsToKeep, AbstractBrooklynObjectSpec spec) { + Map> existingToKeep = MutableMap.of(); + if (existingReferenceParamsToKeep!=null) { + for (SpecParameter p: existingReferenceParamsToKeep) { + existingToKeep.put(p.getConfigKey().getName(), p); + } + } + + List> result = MutableList.>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 extends BasicSpecParameter { + + private final boolean hasType, hasLabelSet, hasPinnedSet, hasDefaultValue, hasConstraints, hasRuntimeInheritance, hasTypeInheritance; + + private SpecParameterForInheritance(String label, Boolean pinned, ConfigKey config, AttributeSensor 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()); --- End diff -- Wrong indentation - all should start at the same column. --- 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. ---