public static boolean isRequestStruct(BMap<String, BValue> struct) { return struct.getType().getName().equals(REQUEST); }
public static String generateDeleteDataStatment(String tableName, BMap<?, ?> constrainedType) { StringBuilder sbSql = new StringBuilder(); sbSql.append(TableConstants.SQL_DELETE_FROM).append(tableName).append(TableConstants.SQL_WHERE); Collection<BField> structFields = ((BStructureType) constrainedType.getType()).getFields().values(); String sep = ""; for (BField sf : structFields) { String name = sf.getFieldName(); sbSql.append(sep).append(name).append(" = ? "); sep = TableConstants.SQL_AND; } return sbSql.toString(); }
private static boolean isResponseStruct(BMap<String, BValue> struct) { return struct.getType().getName().equals(HttpConstants.RESPONSE); }
public static String generateInsertDataStatment(String tableName, BMap<?, ?> constrainedType) { StringBuilder sbSql = new StringBuilder(); StringBuilder sbValues = new StringBuilder(); sbSql.append(TableConstants.SQL_INSERT_INTO).append(tableName).append(" ("); Collection<BField> structFields = ((BStructureType) constrainedType.getType()).getFields().values(); String sep = ""; for (BField sf : structFields) { String name = sf.getFieldName(); sbSql.append(sep).append(name).append(" "); sbValues.append(sep).append("?"); sep = ","; } sbSql.append(") values (").append(sbValues).append(")"); return sbSql.toString(); }
StructImpl(BMap<String, BValue> value) { this.value = value; type = (BStructureType) value.getType(); type.getFields().values().forEach(sf -> { final StructFieldImpl structField = new StructFieldImpl(sf.fieldName, sf.fieldType.getTag()); setIndex(structField, indexes); structFields.put(sf.fieldName, structField); }); }
private static Strand invokeVirtualFunction(Strand ctx, StackFrame sf, int receiver, FunctionInfo virtualFuncInfo, int[] argRegs, int retReg, int flags) { BMap<String, BValue> structVal = (BMap<String, BValue>) sf.refRegs[receiver]; // TODO use ObjectTypeInfo once record init function is removed StructureTypeInfo structInfo = (StructureTypeInfo) ((BStructureType) structVal.getType()).getTypeInfo(); FunctionInfo attachedFuncInfo = structInfo.funcInfoEntries.get(virtualFuncInfo.getName()); return invokeCallable(ctx, attachedFuncInfo, argRegs, retReg, flags); }
private static void convertMapToJSON(Strand ctx, int[] operands, StackFrame sf) { int i = operands[0]; int cpIndex = operands[1]; int j = operands[2]; BJSONType targetType = (BJSONType) ((TypeRefCPEntry) sf.constPool[cpIndex]).getType(); BMap<String, ?> bMap = (BMap<String, ?>) sf.refRegs[i]; try { sf.refRegs[j] = JSONUtils.convertMapToJSON((BMap<String, BValue>) bMap, targetType); } catch (Exception e) { String errorMsg = "cannot convert '" + bMap.getType() + "' to type '" + targetType + "': " + e.getMessage(); handleTypeConversionError(ctx, sf, j, errorMsg); } }
private static void convertStructToJSON(Strand ctx, int[] operands, StackFrame sf) { int i = operands[0]; int cpIndex = operands[1]; int j = operands[2]; BJSONType targetType = (BJSONType) ((TypeRefCPEntry) sf.constPool[cpIndex]).getType(); BMap<String, BValue> bStruct = (BMap<String, BValue>) sf.refRegs[i]; try { sf.refRegs[j] = JSONUtils.convertMapToJSON(bStruct, targetType); } catch (Exception e) { String errorMsg = "cannot convert '" + bStruct.getType() + "' to type '" + targetType + "': " + e.getMessage(); handleTypeConversionError(ctx, sf, j, errorMsg); } }
public void addData(BMap<String, BValue> data, Context context) { if (data.getType() != this.constraintType) { throw new BallerinaException("incompatible types: record of type:" + data.getType().getName() + " cannot be added to a table with type:" + this.constraintType.getName()); } tableProvider.insertData(tableName, data); reset(); }
public static <T> T getNativeObject(BMap<String, BValue> struct, String objectId, Class<T> objectClass, Context context) { Object messageNativeData = struct.getNativeData(objectId); return verifyNativeObject(context, struct.getType().getName(), objectClass, messageNativeData); }
/** * {@inheritDoc} */ @Override public synchronized void attemptFreeze(BVM.FreezeStatus freezeStatus) { if (this.type.getTag() == TypeTags.OBJECT_TYPE_TAG) { throw new BLangFreezeException("'freeze()' not allowed on '" + getType() + "'"); } if (isOpenForFreeze(this.freezeStatus, freezeStatus)) { this.freezeStatus = freezeStatus; map.values().forEach(val -> { if (val != null) { val.attemptFreeze(freezeStatus); } }); } }
public static Service getService(ProgramFile programFile, BMap serviceValue) { final BObjectType serviceType = (BObjectType) serviceValue.getType(); final ServiceInfo serviceInfo = programFile.getPackageInfo(serviceType.getPackagePath()) .getServiceInfo(serviceType); serviceInfo.serviceValue = serviceValue; return ConnectorSPIModelHelper.createService(programFile, serviceInfo, serviceValue); }
@Override public BPacket toBValue(BMap bMap, BValueSerializer serializer) { LinkedHashMap implMap = bMap.getMap(); BValue serialized = serializer.toBValue(implMap, implMap.getClass()); BValue value = serializer.toBValue(bMap.getType(), null); return BPacket.from(typeName(), serialized).put(MAP_STORAGE_TYPE, value); }
private static void convertMapToStruct(Strand ctx, int[] operands, StackFrame sf) { int i = operands[0]; int cpIndex = operands[1]; int j = operands[2]; TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) sf.constPool[cpIndex]; BStructureType structType = (BStructureType) typeRefCPEntry.getType(); BMap<String, BValue> bMap = (BMap<String, BValue>) sf.refRegs[i]; try { sf.refRegs[j] = convertMapToStruct(ctx, bMap, structType); } catch (BallerinaException e) { sf.refRegs[j] = null; String errorMsg = "cannot convert '" + bMap.getType() + "' to type '" + structType + ": " + e.getMessage(); handleTypeConversionError(ctx, sf, j, errorMsg); } }
/** * Where extern function logic is implemented. * * @param context Current Context instance */ @Override public void execute(Context context) { String query = context.getStringArgument(0); BTable fromTable = (BTable) context.getRefArgument(0); BValueArray array = (BValueArray) context.getRefArgument(1); BMap<String, BValue> tableTypeStruct = (BMap<String, BValue>) context.getRefArgument(2); context.setReturnValues(new BTable(query, fromTable, null, (BStructureType) tableTypeStruct.getType(), array)); } }
private static BValue convertMap(Strand ctx, BMap<String, BValue> mapValue, BType targetType, String key) { switch (targetType.getTag()) { case TypeTags.RECORD_TYPE_TAG: return convertMapToStruct(ctx, mapValue, (BStructureType) targetType); case TypeTags.JSON_TAG: return JSONUtils.convertMapToJSON(mapValue, (BJSONType) targetType); case TypeTags.UNION_TAG: for (BType memType : ((BUnionType) targetType).getMemberTypes()) { try { return convertMap(ctx, mapValue, memType, key); } catch (BallerinaException e) { // ignore conversion exception if thrown when the expected type is a union // type, to allow attempting conversion for other types } } break; default: if (checkCast(mapValue, targetType, new ArrayList<TypePair>())) { return mapValue; } } throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, targetType, mapValue.getType()); }
/** * Where extern function logic is implemented. * * @param context Current Context instance */ @Override public void execute(Context context) { String query = context.getStringArgument(0); BTable fromTable = (BTable) context.getRefArgument(0); BTable joinTable = (BTable) context.getRefArgument(1); BValueArray array = (BValueArray) context.getRefArgument(2); BMap<String, BValue> tableTypeStruct = (BMap<String, BValue>) context.getRefArgument(3); context.setReturnValues( new BTable(query, fromTable, joinTable, (BStructureType) tableTypeStruct.getType(), array)); } }
private static void insertToMap(Strand ctx, BMap bMap, String fieldName, BValue value) { try { bMap.put(fieldName, value); } catch (BLangFreezeException e) { // we would only reach here for record or map, not for object String errMessage = ""; switch (bMap.getType().getTag()) { case TypeTags.RECORD_TYPE_TAG: errMessage = "Invalid update of record field: "; break; case TypeTags.MAP_TAG: errMessage = "Invalid map insertion: "; break; } ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), errMessage + e.getDetail())); handleError(ctx); } }
private Object[] createEvent(BMap<String, BValue> data) { BStructureType streamType = (BStructureType) data.getType(); Object[] event = new Object[streamType.getFields().size()]; int index = 0; for (Map.Entry<String, BField> fieldEntry : streamType.getFields().entrySet()) { BField field = fieldEntry.getValue(); switch (field.getFieldType().getTag()) { case TypeTags.INT_TAG: event[index++] = ((BInteger) data.get(field.fieldName)).intValue(); break; case TypeTags.FLOAT_TAG: event[index++] = ((BFloat) data.get(field.fieldName)).floatValue(); break; case TypeTags.BOOLEAN_TAG: event[index++] = ((BBoolean) data.get(field.fieldName)).booleanValue(); break; case TypeTags.STRING_TAG: event[index++] = data.get(field.fieldName).stringValue(); break; default: throw new BallerinaException("Fields in streams do not support data types other than int, " + "float, boolean and string"); } } return event; }