brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From richardcloudsoft <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Allows for lists in templateOptio...
Date Wed, 13 May 2015 12:20:05 GMT
Github user richardcloudsoft commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/639#discussion_r30224760
  
    --- 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 --
    
    Is there any unexpected behaviour here if the template option you want to set here takes
a single parameter that is a `List`?


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