Search in sources :

Example 1 with MappingTarget

use of alien4cloud.tosca.parser.MappingTarget in project alien4cloud by alien4cloud.

the class AbstractMapper method quickMap.

/**
 * Map a field to be parsed as a scalar and mapped to a tosca field that use the same name based on LOWER_CASE_WITH_UNDERSCORES rather than CAMEL_CASE.
 *
 * @param fieldName The name of the field.
 */
public void quickMap(String fieldName) {
    int pathSeparatorLastIndex = fieldName.lastIndexOf(".");
    pathSeparatorLastIndex++;
    pathSeparatorLastIndex = pathSeparatorLastIndex > 0 ? pathSeparatorLastIndex : 0;
    String yamlFieldCamelCase = fieldName.substring(pathSeparatorLastIndex);
    String yamlField = ((PropertyNamingStrategyBase) PropertyNamingStrategy.SNAKE_CASE).translate(yamlFieldCamelCase);
    log.trace("Mapping yaml field {} to {} using basic ScalarParser", yamlField, fieldName);
    instance.getYamlToObjectMapping().put(yamlField, new MappingTarget(fieldName, scalarParser));
}
Also used : PropertyNamingStrategyBase(com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase) MappingTarget(alien4cloud.tosca.parser.MappingTarget)

Example 2 with MappingTarget

use of alien4cloud.tosca.parser.MappingTarget in project alien4cloud by alien4cloud.

the class AbstractMapper method quickMap.

/**
 * Map a field to be parsed with the given parser and mapped to a tosca field that use the same name based on LOWER_CASE_WITH_UNDERSCORES rather than
 * CAMEL_CASE.
 *
 * @param parser The parser to use to parse the field.
 * @param fieldName The name of the field to parse.
 */
public void quickMap(INodeParser<?> parser, String fieldName) {
    int pathSeparatorLastIndex = fieldName.lastIndexOf(".");
    pathSeparatorLastIndex++;
    pathSeparatorLastIndex = pathSeparatorLastIndex > 0 ? pathSeparatorLastIndex : 0;
    String yamlFieldCamelCase = fieldName.substring(pathSeparatorLastIndex);
    String yamlField = ((PropertyNamingStrategyBase) PropertyNamingStrategy.SNAKE_CASE).translate(yamlFieldCamelCase);
    log.info("Mapping yaml field {} to {} using basic ScalarParser", yamlField, fieldName);
    instance.getYamlToObjectMapping().put(yamlField, new MappingTarget(fieldName, parser));
}
Also used : PropertyNamingStrategyBase(com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase) MappingTarget(alien4cloud.tosca.parser.MappingTarget)

Example 3 with MappingTarget

use of alien4cloud.tosca.parser.MappingTarget in project alien4cloud by alien4cloud.

the class DiscriminatorMappingBuilder method buildMapping.

@Override
public MappingTarget buildMapping(MappingNode mappingNode, ParsingContextExecution context) {
    String mappingTarget = "";
    Map<String, INodeParser<?>> parsersByKey = Maps.newHashMap();
    INodeParser<?> fallbackParser = null;
    for (NodeTuple tuple : mappingNode.getValue()) {
        String tupleKey = ParserUtils.getScalar(tuple.getKeyNode(), context);
        if (DISCRIMINATOR.equals(tupleKey)) {
            mappingTarget = ParserUtils.getScalar(tuple.getValueNode(), context);
        } else if (KEYS.equals(tupleKey)) {
            MappingNode keys = (MappingNode) tuple.getValueNode();
            for (NodeTuple keyMappingTuple : keys.getValue()) {
                parsersByKey.put(ParserUtils.getScalar(keyMappingTuple.getKeyNode(), context), baseParserFactory.getReferencedParser(ParserUtils.getScalar(keyMappingTuple.getValueNode(), context)));
            }
        } else if (DEFAULT.equals(tupleKey)) {
            fallbackParser = baseParserFactory.getReferencedParser(ParserUtils.getScalar(tuple.getValueNode(), context));
        }
    }
    return new MappingTarget(mappingTarget, baseParserFactory.getKeyDiscriminatorParser(parsersByKey, fallbackParser));
}
Also used : INodeParser(alien4cloud.tosca.parser.INodeParser) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) MappingTarget(alien4cloud.tosca.parser.MappingTarget)

Example 4 with MappingTarget

use of alien4cloud.tosca.parser.MappingTarget in project alien4cloud by alien4cloud.

the class MapMappingBuilder method buildMapping.

@Override
public MappingTarget buildMapping(MappingNode mappingNode, ParsingContextExecution context) {
    Map<String, String> map = ParserUtils.parseStringMap(mappingNode, context);
    if (map.containsKey(TYPE) && map.containsKey(LIST_TYPE)) {
        log.warn("Both field [ {} ] and [ {} ] exist in your mapping. If it's defined [ {} ] will override the [ {} ].", TYPE, LIST_TYPE, LIST_TYPE, TYPE);
    }
    // default mapping for simple type or for a list_type if defined
    MappingTarget mappingTarget = new MappingTarget(map.get(MAP), baseParserFactory.getMapParser(baseParserFactory.getReferencedParser(map.get(TYPE)), "map of " + map.get(TYPE), map.get(KEY)));
    if (map.containsKey(LIST_TYPE) && !map.get(LIST_TYPE).isEmpty()) {
        mappingTarget = new MappingTarget(map.get(MAP), baseParserFactory.getMapParser(baseParserFactory.getListParser(baseParserFactory.getReferencedParser(map.get(TYPE)), "list of " + map.get(LIST_TYPE), map.get(KEY)), "map of " + map.get(LIST_TYPE), map.get(KEY)));
    }
    return mappingTarget;
}
Also used : MappingTarget(alien4cloud.tosca.parser.MappingTarget)

Example 5 with MappingTarget

use of alien4cloud.tosca.parser.MappingTarget in project alien4cloud by alien4cloud.

the class ReferenceMappingBuilder method buildMapping.

@Override
public MappingTarget buildMapping(MappingNode mappingNode, ParsingContextExecution context) {
    Map<String, String> map = Maps.newHashMap();
    for (NodeTuple tuple : mappingNode.getValue()) {
        String key = ParserUtils.getScalar(tuple.getKeyNode(), context);
        String value = ParserUtils.getScalar(tuple.getValueNode(), context);
        map.put(key, value);
    }
    return new MappingTarget(map.get(REFERENCE_KEY), baseParserFactory.getReferencedParser(map.get(TYPE)));
}
Also used : NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) MappingTarget(alien4cloud.tosca.parser.MappingTarget)

Aggregations

MappingTarget (alien4cloud.tosca.parser.MappingTarget)8 PropertyNamingStrategyBase (com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase)2 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)2 INodeParser (alien4cloud.tosca.parser.INodeParser)1 ListParser (alien4cloud.tosca.parser.impl.base.ListParser)1 SetParser (alien4cloud.tosca.parser.impl.base.SetParser)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1