All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cloudslang.lang.cli.converters.MapConverter Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * (c) Copyright 2016 Hewlett-Packard Development Company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *******************************************************************************/
package io.cloudslang.lang.cli.converters;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.shell.core.Completion;
import org.springframework.shell.core.Converter;
import org.springframework.shell.core.MethodTarget;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Date: 11/11/2014
 *
 * @author lesant
 */

@Component
public class MapConverter implements Converter> {

    public static final String ESCAPE_EXPRESSION = "\\&^\\&";

    @Override
    public boolean supports(Class type, String optionContext) {
        return Map.class.isAssignableFrom(type);
    }

    @Override
    public Map convertFromText(String value, Class targetType, String optionContext) {
        value = value.replace("\\,", ESCAPE_EXPRESSION);
        String[] values = StringUtils.commaDelimitedListToStringArray(value);
        Map map = new HashMap<>();

        for (String v : values) {
            String[] keyValue = StringUtils.delimitedListToStringArray(v, "=");
            if (keyValue.length == 2) {
                keyValue[1] = keyValue[1].replace(ESCAPE_EXPRESSION, ",");
                map.put(keyValue[0], keyValue[1]);
            } else {
                throw new RuntimeException("Input should be in a key=value " +
                        "comma separated format, e.g. key1=val1,key2=val2 etc.");
            }
        }

        return map;
    }

    @Override
    public boolean getAllPossibleValues(List completions, Class targetType,
                                        String existingData, String optionContext, MethodTarget target) {
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy