/** * Check whether provided JSON object is a JSON Array. * * @param json JSON to execute array condition. * @return returns true if provided JSON is a JSON Array. */ public static boolean isJSONArray(BValue json) { if (json == null) { return false; } return json.getType().getTag() == TypeTags.ARRAY_TAG; }
/** * Method to attempt freezing a {@link BValue}, to disallow further modification. * * @param freezeStatus the {@link BVM.FreezeStatus} instance to keep track of the * freeze result of this attempt */ default void attemptFreeze(BVM.FreezeStatus freezeStatus) { throw new BLangFreezeException("freeze not allowed on '" + getType() + "'"); }
/** * Check whether provided JSON object is a JSON Object. * * @param json JSON to execute array condition. * @return returns true if provided JSON is a JSON Object. */ public static boolean isJSONObject(BValue json) { if (json == null) { return false; } return json.getType().getTag() == TypeTags.JSON_TAG || json.getType().getTag() == TypeTags.MAP_TAG; }
public static Type getType(BValue value) { if (value == null) { return NULL; } for (Type type : Type.values()) { if (type.tag == value.getType().getTag()) { return type; } } return OTHER; } }
public void setBValue(BValue bValue) { if (bValue == null) { type = "null"; value = "null"; return; } type = bValue.getType().toString(); value = getStringValue(bValue); }
private static void checkTransactionCoordinatorError(BValue value, String errMsg) { if (value.getType().getTag() == TypeTags.ERROR_TAG) { throw new BallerinaException(errMsg + ((BError) value).reason); } }
/** * Remove a field from JSON. Has no effect if the JSON if not object types or if the given field doesn't exists. * * @param json JSON object * @param fieldName Name of the field to remove */ public static void remove(BValue json, String fieldName) { if (json == null || json.getType().getTag() != TypeTags.JSON_TAG) { return; } ((BMap<String, ?>) json).remove(fieldName); }
public static String getTypeName(BValue jsonValue) { if (jsonValue == null) { return BTypes.typeNull.toString(); } return jsonValue.getType().toString(); }
public static boolean checkIsType(BValue sourceVal, BType targetType) { if (isMutable(sourceVal)) { BType sourceType = sourceVal == null ? BTypes.typeNull : sourceVal.getType(); return checkIsType(sourceType, targetType, new ArrayList<>()); } return checkIsLikeType(sourceVal, targetType); }
private static boolean isValidMapInsertion(BType mapType, BValue value) { if (value == null) { return true; } BType constraintType = ((BMapType) mapType).getConstrainedType(); if (constraintType == BTypes.typeAny || constraintType.equals(value.getType())) { return true; } return checkCast(value, constraintType, new ArrayList<>()); }
/** * This will validate given BValue is an BError and then convert it into * Ballerina exception. This method will be used to validate module life cycle methods. * * @value BValue to be validated. */ private static void validateInvocationError(BValue value) { if (value != null && value.getType().getTag() == TypeTags.ERROR_TAG) { throw new BLangRuntimeException("error: " + BLangVMErrors.getPrintableStackTrace((BError) value)); } } }
/** * Convert {@link JsonNode} to int. * * @param jsonNode {@link JsonNode} to be converted * @return BInteger value of the JSON, if its a integer or a long JSON node. Error, otherwise. */ private static BInteger jsonNodeToInt(BValue json) { if (json == null || json.getType().getTag() != TypeTags.INT_TAG) { throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING_JSON, BTypes.typeInt, getTypeName(json)); } return (BInteger) json; }
/** * Convert {@link JsonNode} to boolean. * * @param jsonNode {@link JsonNode} to be converted * @return BBoolean value of the JSON, if its a boolean node. Error, otherwise. */ private static BBoolean jsonNodeToBool(BValue json) { if (json == null || json.getType().getTag() != TypeTags.BOOLEAN_TAG) { throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING_JSON, BTypes.typeBoolean, getTypeName(json)); } return (BBoolean) json; }
public static Service getServiceFromType(ProgramFile programFile, BValue value) { if (value == null || value.getType().getTag() != TypeTags.SERVICE_TAG) { throw new BallerinaConnectorException("Can't get service reference"); } return getService(programFile, (BMap) value); }
/** * Method to publish to a topic representing the stream in the broker. * * @param data the data to publish to the stream */ public void publish(BValue data) { BType dataType = data.getType(); if (!BVM.checkCast(data, constraintType)) { throw new BallerinaException("incompatible types: value of type:" + dataType + " cannot be added to a stream of type:" + this.constraintType); } brokerInstance.publish(topicName, new BallerinaBrokerByteBuf(data)); }
/** * Get ConnectorEndPoint struct. * * @param context current invocation context. * @return ConnectorEndPoint struct. */ public static Struct getConnectorEndpointStruct(Context context) { BValue result = context.getRefArgument(0); if (result == null || result.getType().getTag() != TypeTags.OBJECT_TYPE_TAG) { throw new BallerinaException("Can't get connector endpoint struct"); } return ConnectorSPIModelHelper.createStruct((BMap<String, BValue>) result); }
/** * Returns Service registered. * * Note: Call this util method when service is required, in register server connector SPI function. * * @param context invocation Context * @return register service. */ public static Service getServiceRegistered(Context context) { BValue result = context.getRefArgument(1); if (result == null || result.getType().getTag() != TypeTags.SERVICE_TAG) { throw new BallerinaConnectorException("Can't get service reference"); } final ProgramFile programFile = context.getProgramFile(); final Service service = getService(programFile, (BMap) result); return service; }
@Override public void execute(Context context) { BMap<String, BValue> serviceEndpoint = (BMap<String, BValue>) context.getRefArgument(SERVICE_ENDPOINT_INDEX); // Service client responder is populated in method listener. There we create new service endpoint instance // and bind client responder instance. BValue clientType = serviceEndpoint.get(LISTENER_CONNECTION_FIELD); if (clientType != null && clientType.getType().getTag() == TypeTags.OBJECT_TYPE_TAG) { BMap<String, BValue> endpointClient = (BMap<String, BValue>) clientType; context.setReturnValues(endpointClient); } else { context.setError(MessageUtils.getConnectorError(new GrpcServerException("Error while " + "retrieving endpoint client."))); } } }