brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahgittin <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Allows for lists in templateOptio...
Date Fri, 22 May 2015 09:37:41 GMT
Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/639#discussion_r30881635
  
    --- Diff: locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
---
    @@ -1050,42 +1050,59 @@ public void apply(TemplateOptions t, ConfigBag props, Object v)
{
                         public void apply(TemplateOptions t, ConfigBag props, Object v) {
                             t.networks((String)v);
                         }})
    -              .put(TEMPLATE_OPTIONS, new CustomizeTemplateOptions() {
    -                  @Override
    -                  public void apply(TemplateOptions options, ConfigBag config, Object
v) {
    -                      if (v == null) return;
    -                      @SuppressWarnings("unchecked") Map<String, String> optionsMap
= (Map<String, String>) v;
    -                      if (optionsMap.isEmpty()) return;
    -
    -                      Class<? extends TemplateOptions> clazz = options.getClass();
    -                      Iterable<Method> methods = Arrays.asList(clazz.getMethods());
    -                      for(final Map.Entry<String, String> option : optionsMap.entrySet())
{
    -                          Optional<Method> methodOptional = Iterables.tryFind(methods,
new Predicate<Method>() {
    -                              @Override
    -                              public boolean apply(@Nullable Method input) {
    -                                  // Matches a method with the expected name, and a single
parameter that TypeCoercions
    -                                  // can coerce to
    -                                  if (input == null) return false;
    -                                  if (!input.getName().equals(option.getKey())) return
false;
    -                                  Type[] parameterTypes = input.getGenericParameterTypes();
    -                                  return parameterTypes.length == 1
    -                                          && TypeCoercions.tryCoerce(option.getValue(),
TypeToken.of(parameterTypes[0])).isPresentAndNonNull();
    -                              }
    -                          });
    -                          if(methodOptional.isPresent()) {
    -                              try {
    -                                  Method method = methodOptional.get();
    -                                  method.invoke(options, TypeCoercions.coerce(option.getValue(),
TypeToken.of(method.getGenericParameterTypes()[0])));
    -                              } catch (IllegalAccessException e) {
    -                                  throw Exceptions.propagate(e);
    -                              } catch (InvocationTargetException e) {
    -                                  throw Exceptions.propagate(e);
    -                              }
    -                          } else {
    -                              LOG.warn("Ignoring request to set template option {} because
this is not supported by {}", new Object[] { option.getKey(), clazz.getCanonicalName() });
    -                          }
    -                      }
    -                  }
    +            .put(TEMPLATE_OPTIONS, new CustomizeTemplateOptions() {
    +                @Override
    +                public void apply(TemplateOptions options, ConfigBag config, Object v)
{
    +                    if (v == null) return;
    +                    @SuppressWarnings("unchecked") Map<String, Object> optionsMap
= (Map<String, Object>) v;
    +                    if (optionsMap.isEmpty()) return;
    +
    +                    Class<? extends TemplateOptions> clazz = options.getClass();
    +                    Iterable<Method> methods = Arrays.asList(clazz.getMethods());
    +                    for(final Map.Entry<String, Object> option : optionsMap.entrySet())
{
    +                        Optional<Method> methodOptional = Iterables.tryFind(methods,
new Predicate<Method>() {
    +                            @Override
    +                            public boolean apply(@Nullable Method input) {
    +                                // Matches a method with the expected name, and a single
parameter that TypeCoercions
    +                                // can coerce to
    +                                if (input == null) return false;
    +                                if (!input.getName().equals(option.getKey())) return
false;
    +                                int numOptionParams = option.getValue() instanceof List
? ((List)option.getValue()).size() : 1;
    +                                Type[] parameterTypes = input.getGenericParameterTypes();
    +                                if (parameterTypes.length != numOptionParams) return
false;
    +                                if (numOptionParams == 1 && !(option.getValue()
instanceof List) && parameterTypes.length == 1) {
    +                                    return true;
    +                                }
    +                                for (int paramCount = 0; paramCount < numOptionParams;
paramCount ++) {
    +                                    if (!TypeCoercions.tryCoerce(((List)option.getValue()).get(paramCount),
    +                                            TypeToken.of(parameterTypes[paramCount])).isPresentAndNonNull())
return false;
    +                                }
    +                                return true;
    +                            }
    +                        });
    +                        if(methodOptional.isPresent()) {
    +                            try {
    +                                Method method = methodOptional.get();
    +                                if (option.getValue() instanceof List) {
    +                                    List<Object> parameters = Lists.newArrayList();
    +                                    int numOptionParams = ((List)option.getValue()).size();
    +                                    for (int paramCount = 0; paramCount < numOptionParams;
paramCount++) {
    +                                        parameters.add(TypeCoercions.coerce(((List)option.getValue()).get(paramCount),
TypeToken.of(method.getGenericParameterTypes()[paramCount])));
    +                                    }
    +                                    method.invoke(options, parameters.toArray());
    +                                } else {
    +                                    method.invoke(options, TypeCoercions.coerce(option.getValue(),
TypeToken.of(method.getGenericParameterTypes()[0])));
    +                                }
    +                            } catch (IllegalAccessException e) {
    +                                throw Exceptions.propagate(e);
    +                            } catch (InvocationTargetException e) {
    +                                throw Exceptions.propagate(e);
    +                            }
    +                        } else {
    +                            LOG.warn("Ignoring request to set template option {} because
this is not supported by {}", new Object[] { option.getKey(), clazz.getCanonicalName() });
    +                        }
    +                    }
    +                }
    --- End diff --
    
    Agree this case is surprising.  It was ugly in your example that where a method takes
a one-arg list i have to pass `[[ "value" ]]` rather than `[ value ]`.
    
    I suggest either changing the logic, both in the `try` and once the method is retrieved,
so that either:
    * if the first pass fails, i.e. we are unable to match the items in a list against any
method's parameters, we try again looking for a `method(Iterable)` (this will do the right
thing in more cases, ie for `foo(List)`, we will accept `foo: [ arg ]` *or* `foo: [[ arg ]]`;
it is only in the case we want a `foo(List<List>)` that a user has to take care, and
in that case they must do `foo: [ [[ arg ]] ]`, which we should document.
    * we *always* prefer a one-parameter method, if `option.getValue()` is coercible to that
parameter type (however this means you must *not* doubly-wrap lists)
    
    Probably the former is better as it gives preference to multi-argument methods which are
less ambiguous.  Make sure you add a test for both the `[ list ]` and `[[ list ]]` cases!
    
    In any case this is a nice feature and nice job with tricky logic.  I've wanted to pass
more complex types here before so I'm glad it's now supported.
    
    I also agree with the importance of documenting this in the locations markdown.  You will
see instances there where I describe YAML notation (where `templateOptions` are set in a blueprint)
in addition to properties file.



---
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