Search in sources :

Example 1 with MapValue

use of org.ballerinalang.jvm.values.MapValue in project product-microgateway by wso2.

the class MGWJWTGeneratorInvoker method convertMapValueToMap.

/**
 * Convert MapValue to Map.
 */
public static Map<String, Object> convertMapValueToMap(MapValue mapValue) {
    Map<String, Object> map = new HashMap<>();
    for (Object key : mapValue.getKeys()) {
        Object valueObject = mapValue.get(key.toString());
        if (valueObject != null && valueObject instanceof MapValue) {
            MapValue subMapValue = mapValue.getMapValue(key.toString());
            Map<String, Object> subMap = convertMapValueToMap(subMapValue);
            map.put(key.toString(), subMap);
        } else if (valueObject != null && valueObject instanceof ArrayValue) {
            ArrayValue arrayValue = mapValue.getArrayValue(key.toString());
            map.put(key.toString(), convertArrayValueToArray(arrayValue));
        } else {
            map.put(key.toString(), valueObject);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) MapValue(org.ballerinalang.jvm.values.MapValue) ArrayValue(org.ballerinalang.jvm.values.ArrayValue)

Example 2 with MapValue

use of org.ballerinalang.jvm.values.MapValue in project product-microgateway by wso2.

the class Request method getQueryParamValue.

/**
 * Gets the query param value associated with the given key.
 *
 * @param key Represents the query param key.
 * @return Returns the query param value associated with the given key as a string. If multiple param values are
 * present, then the first value is returned. Null is returned if no key is found.
 */
public String getQueryParamValue(String key) {
    BMap mapValue = getNativeQueryParams();
    BArray arrayValue = ((MapValue) mapValue).getArrayValue(key);
    if (arrayValue != null) {
        return arrayValue.get(0).toString();
    }
    return null;
}
Also used : BMap(org.ballerinalang.jvm.values.api.BMap) MapValue(org.ballerinalang.jvm.values.MapValue) BArray(org.ballerinalang.jvm.values.api.BArray)

Example 3 with MapValue

use of org.ballerinalang.jvm.values.MapValue in project product-microgateway by wso2.

the class Request method getQueryParamValues.

/**
 * Gets all the query param values associated with the given key.
 *
 * @param key Represents the query param key.
 * @return Returns all the query param values associated with the given key as a `String[]`.
 * Null is returned if no key is found.
 */
public String[] getQueryParamValues(String key) {
    BMap mapValue = getNativeQueryParams();
    BArray arrayValue = ((MapValue) mapValue).getArrayValue(key);
    if (arrayValue != null) {
        return arrayValue.getStringArray();
    }
    return null;
}
Also used : BMap(org.ballerinalang.jvm.values.api.BMap) MapValue(org.ballerinalang.jvm.values.MapValue) BArray(org.ballerinalang.jvm.values.api.BArray)

Example 4 with MapValue

use of org.ballerinalang.jvm.values.MapValue in project product-microgateway by wso2.

the class MappingInvoker method convertMapValueToMap.

/**
 * Convert MapValue to Map.
 */
public static Map<String, Object> convertMapValueToMap(MapValue mapValue) throws Exception {
    Map<String, Object> map = new HashMap<>();
    for (Object key : mapValue.getKeys()) {
        Object valueObject = mapValue.get(key.toString());
        if (valueObject != null && valueObject instanceof MapValue) {
            MapValue subMapValue = mapValue.getMapValue(key.toString());
            Map<String, Object> subMap = convertMapValueToMap(subMapValue);
            map.put(key.toString(), subMap);
        } else if (valueObject != null && valueObject instanceof ArrayValue) {
            ArrayValue arrayValue = mapValue.getArrayValue(key.toString());
            Object[] array = new Object[arrayValue.size()];
            for (int i = 0; i < arrayValue.size(); i++) {
                if (arrayValue.get(i) instanceof MapValue) {
                    array[i] = convertMapValueToMap((MapValue) arrayValue.get(i));
                } else {
                    array[i] = arrayValue.get(i);
                }
            }
            map.put(key.toString(), array);
        } else {
            map.put(key.toString(), valueObject);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) MapValue(org.ballerinalang.jvm.values.MapValue) ArrayValue(org.ballerinalang.jvm.values.ArrayValue)

Aggregations

MapValue (org.ballerinalang.jvm.values.MapValue)4 HashMap (java.util.HashMap)2 ArrayValue (org.ballerinalang.jvm.values.ArrayValue)2 BArray (org.ballerinalang.jvm.values.api.BArray)2 BMap (org.ballerinalang.jvm.values.api.BMap)2