brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grkvlt <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Add type coercion for parameteriz...
Date Sat, 27 Sep 2014 00:42:57 GMT
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/182#discussion_r18120509
  
    --- Diff: usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
---
    @@ -93,53 +114,141 @@ public static Object sensor(String clazzName, String sensorName)
{
                     Map<String,Sensor<?>> sensors = EntityDynamicType.findSensors((Class)clazz,
null);
                     sensor = sensors.get(sensorName);
                 }
    -            if (sensor == null)
    +            if (sensor == null) {
                     throw new IllegalArgumentException("Sensor " + sensorName + " not found
on class " + clazzName);
    +            }
                 return sensor;
             } catch (ClassNotFoundException e) {
                 throw Exceptions.propagate(e);
             }
         }
     
    -    // --- build complex things
    -    
    +    // Build complex things
    +
         public static EntitySpecConfiguration entitySpec(Map<String, Object> arguments)
{
             return new EntitySpecConfiguration(arguments);
         }
    -    
    -    // --- string manipulation
     
    -    /** return a literal string -- ie skip parsing */
    +    /**
    +     * Return an instance of the specified class with its fields set according
    +     * to the {@link Map} or a {@link BrooklynDslDeferredSupplier} if the arguments are
not
    +     * yet fully resolved.
    +     */
    +    public static Object object(Map<String, Object> arguments) {
    +        try {
    +            ConfigBag config = ConfigBag.newInstance(arguments);
    +            String typeName = BrooklynYamlTypeInstantiator.InstantiatorFromKey.extractTypeName("object",
config).orNull();
    +            Map<String,Object> fields = (Map<String, Object>) config.getStringKey("object.fields");
    +            Class<?> type = Class.forName(typeName);
    +            if (DslUtils.resolved(fields.values())) {
    +                String json = gson.get().toJson(fields);
    +                return gson.get().fromJson(json, type);
    +            } else {
    +                return new DslObject(type, fields);
    +            }
    +        } catch (Exception e) {
    +            throw Exceptions.propagate(e);
    +        }
    +    }
    +
    +    // String manipulation
    +
    +    /** Return the expression as a literal string without any further parsing. */
         public static Object literal(Object expression) {
             return expression;
         }
     
    -    /** returns a DslParsedObject<String> OR a String if it is fully resolved */
    +    /**
    +     * Returns a formatted string or a {@link BrooklynDslDeferredSupplier} if the arguments
    +     * are not yet fully resolved.
    +     */
         public static Object formatString(final String pattern, final Object ...args) {
             if (DslUtils.resolved(args)) {
                 // if all args are resolved, apply the format string now
                 return String.format(pattern, args);
             }
    -        return new FormatString(pattern, args);
    +        return new DslFormatString(pattern, args);
         }
     
    -    protected static final class FormatString extends BrooklynDslDeferredSupplier<String>
{
    --- End diff --
    
    @ahgittin not sure I understand where these classes could actually be persisted?


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