Search in sources :

Example 1 with BNewArray

use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.

the class BTestRunner method extractArguments.

/**
 * Extract function arguments from the values sets.
 * @param valueSets user provided value sets
 * @return a list of function arguments
 */
private List<BValue[]> extractArguments(BValue[] valueSets) {
    List<BValue[]> argsList = new ArrayList<>();
    for (BValue value : valueSets) {
        if (value instanceof BRefValueArray) {
            BRefValueArray array = (BRefValueArray) value;
            for (BIterator it = array.newIterator(); it.hasNext(); ) {
                BValue[] vals = it.getNext(0);
                if (vals[1] instanceof BNewArray) {
                    BNewArray bNewArray = (BNewArray) vals[1];
                    BValue[] args = new BValue[(int) bNewArray.size()];
                    for (int j = 0; j < bNewArray.size(); j++) {
                        args[j] = bNewArray.getBValue(j);
                    }
                    argsList.add(args);
                } else {
                // cannot happen due to validations done at annotation processor
                }
            }
        } else if (value instanceof BJSON) {
            BJSON jsonArrayOfArrays = (BJSON) value;
            for (BIterator it = jsonArrayOfArrays.newIterator(); it.hasNext(); ) {
                BValue[] vals = it.getNext(0);
                if (vals[1] instanceof BJSON) {
                    List<BValue> args = new ArrayList<>();
                    BJSON jsonArray = (BJSON) vals[1];
                    for (BIterator it2 = jsonArray.newIterator(); it2.hasNext(); ) {
                        BValue[] vals2 = it2.getNext(0);
                        args.add(vals2[1]);
                    }
                    argsList.add(args.toArray(new BValue[0]));
                } else {
                // cannot happen due to validations done at annotation processor
                }
            }
        } else {
            argsList.add(new BValue[] { value });
        }
    }
    return argsList;
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) BValue(org.ballerinalang.model.values.BValue) ArrayList(java.util.ArrayList) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) ArrayList(java.util.ArrayList) List(java.util.List) BIterator(org.ballerinalang.model.values.BIterator) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with BNewArray

use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.

the class CPU method calculateLength.

@SuppressWarnings("rawtypes")
private static void calculateLength(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    int typeTag = typeRefCPEntry.getType().getTag();
    if (typeTag == TypeTags.STRING_TAG) {
        String value = sf.stringRegs[i];
        if (value == null) {
            handleNullRefError(ctx);
        } else {
            sf.longRegs[j] = value.length();
        }
        return;
    } else if (typeTag == TypeTags.BLOB_TAG) {
        // Here it is assumed null is not supported for blob type
        sf.longRegs[j] = sf.byteRegs[i].length;
        return;
    }
    BValue entity = sf.refRegs[i];
    if (entity == null) {
        handleNullRefError(ctx);
        return;
    }
    if (typeTag == TypeTags.XML_TAG) {
        sf.longRegs[j] = ((BXML) entity).length();
        return;
    } else if (entity instanceof BJSON) {
        if (JSONUtils.isJSONArray((BJSON) entity)) {
            sf.longRegs[j] = JSONUtils.getJSONArrayLength((BJSON) sf.refRegs[i]);
        } else {
            sf.longRegs[j] = -1;
        }
        return;
    } else if (typeTag == TypeTags.MAP_TAG) {
        sf.longRegs[j] = ((BMap) entity).size();
        return;
    }
    BNewArray newArray = (BNewArray) entity;
    sf.longRegs[j] = newArray.size();
    return;
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON)

Example 3 with BNewArray

use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.

the class StructImpl method getArrayField.

@Override
public Value[] getArrayField(String fieldName) {
    final BNewArray refField = (BNewArray) value.getRefField(getFieldIndex(fieldName));
    if (refField == null) {
        return null;
    }
    List<Value> list = new ArrayList<>();
    final BIterator bIterator = refField.newIterator();
    while (bIterator.hasNext()) {
        list.add(ValueImpl.createValue(bIterator.getNext(1)[0]));
    }
    return list.toArray(new Value[0]);
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) Value(org.ballerinalang.connector.api.Value) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) BValue(org.ballerinalang.model.values.BValue) ArrayList(java.util.ArrayList) BIterator(org.ballerinalang.model.values.BIterator)

Example 4 with BNewArray

use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.

the class JSONUtils method convertMapToJSON.

/**
 * Convert {@link BMap} to {@link BJSON}.
 *
 * @param map {@link BMap} to be converted to {@link BJSON}
 * @return JSON representation of the provided map
 */
@SuppressWarnings("unchecked")
public static BJSON convertMapToJSON(BMap<String, BValue> map) {
    Set<String> keys = map.keySet();
    BJSON bjson = new BJSON(new JsonNode(Type.OBJECT));
    JsonNode jsonNode = bjson.value();
    for (String key : keys) {
        try {
            BValue bvalue = map.get(key);
            if (bvalue == null) {
                jsonNode.set(key, new BJSON(NULL).value());
            } else if (bvalue.getType() == BTypes.typeString) {
                jsonNode.set(key, bvalue.stringValue());
            } else if (bvalue.getType() == BTypes.typeInt) {
                jsonNode.set(key, ((BInteger) bvalue).intValue());
            } else if (bvalue.getType() == BTypes.typeFloat) {
                jsonNode.set(key, ((BFloat) bvalue).floatValue());
            } else if (bvalue.getType() == BTypes.typeBoolean) {
                jsonNode.set(key, ((BBoolean) bvalue).booleanValue());
            } else if (bvalue.getType().getTag() == TypeTags.MAP_TAG) {
                jsonNode.set(key, convertMapToJSON((BMap<String, BValue>) bvalue).value());
            } else if (bvalue.getType() == BTypes.typeJSON) {
                jsonNode.set(key, ((BJSON) bvalue).value());
            } else if (bvalue instanceof BNewArray) {
                jsonNode.set(key, convertArrayToJSON((BNewArray) bvalue).value());
            } else if (bvalue instanceof BStruct) {
                jsonNode.set(key, convertStructToJSON((BStruct) bvalue).value());
            } else {
                throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, bvalue.getType());
            }
        } catch (Exception e) {
            handleError(e, key);
        }
    }
    return bjson;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BNewArray(org.ballerinalang.model.values.BNewArray) BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BFloat(org.ballerinalang.model.values.BFloat) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 5 with BNewArray

use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.

the class JSONUtils method convertArrayToJSON.

/**
 * Convert {@link BRefValueArray} to {@link BJSON}.
 *
 * @param refValueArray {@link BRefValueArray} to be converted to {@link BJSON}
 * @return JSON representation of the provided refValueArray
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static BJSON convertArrayToJSON(BRefValueArray refValueArray) {
    BJSON bjson = new BJSON(new JsonNode(Type.ARRAY));
    JsonNode arrayNode = bjson.value();
    for (int i = 0; i < refValueArray.size(); i++) {
        BRefType value = refValueArray.get(i);
        if (value == null) {
            arrayNode.add(new BJSON(NULL).value());
        } else if (value.getType().getTag() == TypeTags.MAP_TAG) {
            arrayNode.add(convertMapToJSON((BMap<String, BValue>) value).value());
        } else if (value instanceof BJSON) {
            arrayNode.add(((BJSON) value).value());
        } else if (value instanceof BStruct) {
            arrayNode.add(convertStructToJSON((BStruct) value).value());
        } else if (value instanceof BNewArray) {
            arrayNode.add(convertArrayToJSON((BNewArray) value).value());
        } else {
            throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, value.getType());
        }
    }
    return bjson;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStruct(org.ballerinalang.model.values.BStruct) BNewArray(org.ballerinalang.model.values.BNewArray) BMap(org.ballerinalang.model.values.BMap) BJSON(org.ballerinalang.model.values.BJSON)

Aggregations

BNewArray (org.ballerinalang.model.values.BNewArray)9 BJSON (org.ballerinalang.model.values.BJSON)7 BValue (org.ballerinalang.model.values.BValue)6 BStruct (org.ballerinalang.model.values.BStruct)5 BMap (org.ballerinalang.model.values.BMap)4 BString (org.ballerinalang.model.values.BString)4 ArrayList (java.util.ArrayList)2 BStructType (org.ballerinalang.model.types.BStructType)2 BIterator (org.ballerinalang.model.values.BIterator)2 BRefType (org.ballerinalang.model.values.BRefType)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 List (java.util.List)1 Value (org.ballerinalang.connector.api.Value)1 BType (org.ballerinalang.model.types.BType)1 JsonNode (org.ballerinalang.model.util.JsonNode)1 BFloat (org.ballerinalang.model.values.BFloat)1 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)1 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)1 BValueType (org.ballerinalang.model.values.BValueType)1 BXML (org.ballerinalang.model.values.BXML)1