Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/585#discussion_r28337658
  
    --- Diff: 
core/src/main/java/brooklyn/catalog/internal/BasicBrooklynCatalog.java ---
    @@ -491,43 +508,127 @@ public void load() {
             if (item instanceof CatalogItemDtoAbstract) return 
(CatalogItemDtoAbstract<T,SpecT>) item;
             throw new IllegalStateException("Cannot unwrap catalog item 
'"+item+"' (type "+item.getClass()+") to restore DTO");
         }
    +    
    +    @SuppressWarnings("unchecked")
    +    private <T> Maybe<T> getFirstAs(Map<?,?> map, Class<T> type, String 
firstKey, String ...otherKeys) {
    +        if (map==null) return Maybe.absent("No map available");
    +        String foundKey = null;
    +        Object value = null;
    +        if (map.containsKey(firstKey)) foundKey = firstKey;
    +        else for (String key: otherKeys) {
    +            if (map.containsKey(key)) {
    +                foundKey = key;
    +                break;
    +            }
    +        }
    +        if (foundKey==null) return Maybe.absent("Missing entry 
'"+firstKey+"'");
    +        value = map.get(foundKey);
    +        if (type.equals(String.class) && Number.class.isInstance(value)) 
value = value.toString();
    +        if (!type.isInstance(value)) 
    +            throw new IllegalArgumentException("Entry for '"+firstKey+"' 
should be of type "+type+", not "+value.getClass());
    +        return Maybe.of((T)value);
    +    }
    +    
    +    @SuppressWarnings({ "unchecked", "rawtypes" })
    +    private Maybe<Map<?,?>> getFirstAsMap(Map<?,?> map, String firstKey, 
String ...otherKeys) {
    +        return (Maybe<Map<?,?>>)(Maybe) getFirstAs(map, Map.class, 
firstKey, otherKeys);
    +    }
     
    -    private CatalogItemDtoAbstract<?,?> getAbstractCatalogItem(String 
yaml) {
    -        DeploymentPlan plan = makePlanFromYaml(yaml);
    +    private List<CatalogItemDtoAbstract<?,?>> 
addAbstractCatalogItems(String yaml, Boolean whenAddingAsDtoShouldWeForce) {
    +        Map<?,?> itemDef = Yamls.getAs(Yamls.parseAll(yaml), Map.class);
    +        Map<?,?> catalogMetadata = getFirstAsMap(itemDef, 
"brooklyn.catalog", "catalog").orNull();
    +        if (catalogMetadata==null)
    +            log.warn("No `brooklyn.catalog` supplied in catalog request; 
using legacy mode for "+itemDef);
    +        catalogMetadata = MutableMap.copyOf(catalogMetadata);
     
    -        @SuppressWarnings("rawtypes")
    -        Maybe<Map> possibleCatalog = 
plan.getCustomAttribute("brooklyn.catalog", Map.class, true);
    -        MutableMap<String, Object> catalog = MutableMap.of();
    -        if (possibleCatalog.isPresent()) {
    -            @SuppressWarnings("unchecked")
    -            Map<String, Object> catalog2 = (Map<String, Object>) 
possibleCatalog.get();
    -            catalog.putAll(catalog2);
    +        List<CatalogItemDtoAbstract<?, ?>> result = MutableList.of();
    +        
    +        addAbstractCatalogItems(Yamls.getTextOfYamlAtPath(yaml, 
"brooklyn.catalog").getMatchedYamlTextOrWarn(), 
    --- End diff --
    
    agree


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to