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;
}
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;
}
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]);
}
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;
}
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;
}
Aggregations