@Override public String toString() { return "LambdaRef:" + (function != null ? String.valueOf(function.getName()) : null); } }
private BLangFunction extractMainFunction(BLangPackage myPkg) { for (BLangFunction func : myPkg.getFunctions()) { if (MAIN_FUNC_NAME.equals(func.getName().value)) { return func; } } return null; }
private static void validateParamDetailsSize(List<BLangSimpleVariable> paramDetails, int min, int max, BLangFunction resource, DiagnosticLog dlog) { if (paramDetails == null || paramDetails.size() < min || paramDetails.size() > max) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": Unexpected parameter count"); } }
private static void validateParamDetailsSize(List<BLangSimpleVariable> paramDetails, int expectedSize, BLangFunction resource, DiagnosticLog dlog) { if (paramDetails == null || paramDetails.size() != expectedSize) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": Expected parameter count = " + expectedSize); } }
private static void validateEndpointParameter(BLangFunction resource, DiagnosticLog dlog, List<BLangSimpleVariable> paramDetails, boolean isClient) { if (paramDetails == null || paramDetails.isEmpty() || (!isClient && !WebSocketConstants.WEBSOCKET_CALLER_NAME.equals( paramDetails.get(0).type.toString())) || (isClient && !WebSocketConstants.WEBSOCKET_CLIENT_ENDPOINT_NAME.equals( paramDetails.get(0).type.toString()))) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The first parameter should be an endpoint"); } }
private void visitBuiltinFunctions(BLangPackage pkgNode, BLangFunction function) { if (Symbols.isFlagOn(pkgNode.symbol.flags, Flags.TESTABLE)) { String funcName = function.getName().value; String builtinFuncName = funcName.substring(funcName.indexOf("<") + 1, funcName.indexOf(">")); String modifiedFuncName = funcName.replace(builtinFuncName, "test" + builtinFuncName); function.name.setValue(modifiedFuncName); function.originalFuncSymbol.name.value = modifiedFuncName; function.symbol.name.value = modifiedFuncName; } createFunctionInfoEntry(function); genNode(function, this.env); }
private static void validateOnErrorResource(BLangFunction resource, DiagnosticLog dlog, boolean isClient) { List<BLangSimpleVariable> paramDetails = resource.getParameters(); validateParamDetailsSize(paramDetails, 2, resource, dlog); validateEndpointParameter(resource, dlog, paramDetails, isClient); if (paramDetails.size() < 2 || !"error".equals(paramDetails.get(1).type.toString())) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, String.format( "Invalid resource signature for %s resource in service : The second parameter should be an error", resource.getName().getValue())); } }
private static void validateOnCloseResource(BLangFunction resource, DiagnosticLog dlog, boolean isClient) { List<BLangSimpleVariable> paramDetails = resource.getParameters(); validateParamDetailsSize(paramDetails, 3, resource, dlog); validateEndpointParameter(resource, dlog, paramDetails, isClient); if (paramDetails.size() < 2 || TypeTags.INT != paramDetails.get(1).type.tag) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The second parameter should be an int"); } if (paramDetails.size() < 3 || TypeTags.STRING != paramDetails.get(2).type.tag) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The third parameter should be a string"); } }
private static void validateOnBinaryResource(BLangFunction resource, DiagnosticLog dlog, boolean isClient) { List<BLangSimpleVariable> paramDetails = resource.getParameters(); validateParamDetailsSize(paramDetails, 2, 3, resource, dlog); validateEndpointParameter(resource, dlog, paramDetails, isClient); if (paramDetails.size() < 2 || !"byte[]".equals(paramDetails.get(1).type.toString())) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The second parameter should be a byte[]"); } if (paramDetails.size() == 3 && !"boolean".equals(paramDetails.get(2).type.toString())) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The third parameter should be a boolean"); } }
private static void validateOnPingPongResource(BLangFunction resource, DiagnosticLog dlog, boolean isClient) { List<BLangSimpleVariable> paramDetails = resource.getParameters(); validateParamDetailsSize(paramDetails, 2, resource, dlog); validateEndpointParameter(resource, dlog, paramDetails, isClient); if (paramDetails.size() < 2 || !"byte[]".equals(paramDetails.get(1).type.toString())) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The second parameter should be a byte[]"); } }
void addLambdaFunctionDef(DiagnosticPos pos, Set<Whitespace> ws, boolean paramsAvail, boolean retParamsAvail, boolean restParamAvail) { BLangFunction lambdaFunction = (BLangFunction) this.invokableNodeStack.peek(); lambdaFunction.pos = pos; endCallableUnitSignature(pos, ws, lambdaFunction.getName().value, pos, paramsAvail, retParamsAvail, restParamAvail); BLangLambdaFunction lambdaExpr = (BLangLambdaFunction) TreeBuilder.createLambdaFunctionNode(); lambdaExpr.function = lambdaFunction; lambdaExpr.pos = pos; addExpressionNode(lambdaExpr); // TODO: is null correct here endFunctionDef(pos, null, false, false, false, true, false, true); }
private static void validateOnOpenResource(BLangFunction resource, DiagnosticLog dlog, boolean isClient) { if (!isClient || !resource.getName().getValue().equals(WebSocketConstants.RESOURCE_NAME_ON_OPEN)) { List<BLangSimpleVariable> paramDetails = resource.getParameters(); validateParamDetailsSize(paramDetails, 1, resource, dlog); validateEndpointParameter(resource, dlog, paramDetails, isClient); } else { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, "onOpen resource is not supported for " + WebSocketConstants.WEBSOCKET_CLIENT_SERVICE); } }
if (paramDetails.size() < 2) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": A second parameter needs to be specified"); org.ballerinalang.model.types.TypeTags.BYTE_TAG))) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The second parameter should be a string, json, xml, byte[] or a record type"); } else if (paramDetails.size() == 3) { if (!"string".equals(secondParamType.toString())) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": Final fragment is not valid if the second parameter is not a string"); } else if (!"boolean".equals(paramDetails.get(2).type.toString())) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, INVALID_RESOURCE_SIGNATURE_FOR + resource.getName().getValue() + RESOURCE_IN_SERVICE + ": The third parameter should be a boolean");
/** * Gets the path value of the @http:resourceConfig. * * @param resource The ballerina resource. * @return The path value. */ private String getPath(BLangFunction resource) { String path = "/" + resource.getName(); AnnotationAttachmentNode annotation = ConverterUtils .getAnnotationFromList(HttpConstants.ANN_NAME_RESOURCE_CONFIG, httpAlias, resource.getAnnotationAttachments()); if (annotation != null) { BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) annotation) .getExpression()); List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs(); Map<String, BLangExpression> attributes = ConverterUtils.listToMap(list); if (attributes.containsKey(HttpConstants.ANN_RESOURCE_ATTR_PATH)) { path = ConverterUtils.getStringLiteralValue(attributes.get(HttpConstants.ANN_RESOURCE_ATTR_PATH)); } } return path; }
public void validate(String serviceName, BLangFunction resource, DiagnosticLog dlog) { switch (resource.getName().getValue()) { case RESOURCE_NAME_ON_CREATE: case RESOURCE_NAME_ON_DELETE: String msg = "Invalid resource signature for %s in service %s. " + "The parameter should be a file:FileEvent with no returns."; msg = String.format(msg, resource.getName().getValue(), serviceName); if (parameters.size() != 1) { dlog.logDiagnostic(ERROR, resource.getPosition(), msg); default: dlog.logDiagnostic(ERROR, resource.getPosition(), "Invalid resource name " + resource.getName().getValue() + " in service " + serviceName);
@Override public void visit(BLangFunction funcNode) { String functionName = funcNode.getName().getValue(); SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcNode.symbol.scope, this.symbolEnv); CursorPositionResolver cpr = CursorPositionResolvers.getResolverByClass(this.cursorPositionResolver); funcNode.annAttachments.forEach(annotationAttachment -> this.acceptNode(annotationAttachment, funcEnv)); boolean withinParamContext = CompletionVisitorUtil .isWithinParameterContext(functionName, UtilSymbolKeys.FUNCTION_KEYWORD_KEY, funcEnv, lsContext, this); boolean cursorBeforeNode = cpr.isCursorBeforeNode(funcNode.getPosition(), this, this.lsContext, funcNode, funcNode.symbol); if (terminateVisitor || cursorBeforeNode || withinParamContext) { return; } if (funcNode.getBody() != null) { this.blockOwnerStack.push(funcNode); this.cursorPositionResolver = BlockStatementScopeResolver.class; this.acceptNode(funcNode.body, funcEnv); this.blockOwnerStack.pop(); } }
Optional<BLangFunction> descriptorMapFunc = ((ArrayList) ((BLangPackage) serviceNode .parent).functions).stream().filter(var -> DESCRIPTOR_MAP.equals(((BLangFunction) var) .getName().getValue())).findFirst();
dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, "Invalid return type: expected error?"); switch (resource.getName().getValue()) { case WebSocketConstants.RESOURCE_NAME_ON_OPEN: case WebSocketConstants.RESOURCE_NAME_ON_IDLE_TIMEOUT: default: dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, "Invalid resource name " + resource.getName().getValue() + " in service ");
/** * This method will convert ballerina @Resource to ballerina @OperationAdaptor. * * @param resource Resource array to be convert. * @return Operation Adaptor object of given resource */ private OperationAdaptor convertResourceToOperation(BLangFunction resource, String httpMethod, int idIncrement) { OperationAdaptor op = new OperationAdaptor(); if (resource != null) { op.setHttpOperation(httpMethod); op.setPath('/' + resource.getName().getValue()); Response response = new Response() .description("Successful") .example(MediaType.APPLICATION_JSON, "Ok"); op.getOperation().response(200, response); // Replacing all '_' with ' ' to keep the consistency with what we are doing in swagger -> bal // @see BallerinaOperation#buildContext String resName = resource.getName().getValue().replaceAll("_", " "); op.getOperation().setOperationId(getOperationId(idIncrement, resName)); op.getOperation().setParameters(null); // Parsing annotations. this.parseResourceConfigAnnotationAttachment(resource, op); this.parseResourceInfo(resource, op.getOperation(), httpMethod); this.addResourceParameters(resource, op); this.parseResponsesAnnotationAttachment(resource, op.getOperation()); } return op; }
boolean onCompleteExists = false; for (BLangFunction resourceNode : resources) { switch (resourceNode.getName().getValue()) { case ON_OPEN_RESOURCE: onOpenExists = true;