ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.68k stars 753 forks source link

[Proposal]: Improve support to map resource functions to the java native function #39737

Open daneshk opened 1 year ago

daneshk commented 1 year ago

Description

The current Java interoperability feature does not have specific binding to the Ballerina resource functions. There are a few differences in the resource function and instance function and Java binding doesn't fully support binding Ballerina resource functions.

The difference between the resource function and instance function is,

We need to have a way of passing resource function action, resource name, and path parameters to the attached native function.

Describe your problem(s)

In the persistence layer implementation, we generate a client object with a set of resource functions for each schema file defined in the Ballerina project. We need to make that resource functions dependently typed functions. The only way of doing it at the moment is changing them to external functions.

We need to pass the resource function details(action and resource name) and path params to the native function which is not supported in java bindings

Describe your solution(s)

Java native function signature

class:  x.y.z.ClassName;
method: JRT methodName(Environment env, BObject client, String action, String resourceName, Map<Object> pathParams, JT1 t1, JT2 t2, …, JTn tn);

JT - Java type
JRT - Java return type
BT - Ballerina type
BRT - Ballerina return type

Ballerina function signature

resource function get resourceName/[BT1 p1]/[BT2 p2].../[BTn pn](BT1 t1, …, BTn tn) returns BRT = @java:Method {
    [name: “methodName”,]
class: “x.y.z.ClassName”,
} external;

Suggested changes are,

Add three new input params to the java native function, all are mandatory parameters with the order,

Related area

-> Bindgen Tool

Related issue(s) (optional)

https://github.com/ballerina-platform/ballerina-standard-library/issues/4087

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

daneshk commented 1 year ago

In the current binding support,

This ballerina function is mapped to

resource function get resourceName/[BT1 p1]/[BT2 p2].../[BTn pn](BT1 t1, …, BTn tn) returns BRT = @java:Method {
    [name: “methodName”,]
class: “x.y.z.ClassName”,
} external;

java function,

class:  x.y.z.ClassName;
method: JRT methodName(Environment env, BObject client, JT1 p1, JT2 p2 ...., JTn pn, JT1 t1, JT2 t2, …, JTn tn);

JT - Java type JRT - Java return type BT - Ballerina type BRT - Ballerina return type

We need to have mappings for each path and input parameters

daneshk commented 1 year ago

Can we improve the java binding to at least support as below,

The Ballerina function

resource function get resourceName/[BT1 p1]/[BT2 p2]/.../[BTn pn](BT1 t1, …, BTn tn) returns BRT = @java:Method {
    [name: “methodName”,]
class: “x.y.z.ClassName”,
} external;

The Java function

class:  x.y.z.ClassName;
method: JRT methodName(Environment env, BObject client, BArray pathParams, JT1 t1, JT2 t2, …, JTn tn);

Here all the path param values in the Ballerina resource function are bundled to the BArray pathParams variable. BArray is an Object Array.

daneshk commented 1 year ago

As discussed with @warunalakshitha compile time details like resource name, and path parameter names can pass via env. We can have a new annotation field to enable function metadata so that metadata is processed only if needed.

daneshk commented 1 year ago

As per the offline discussion with Sameera and Waruna, we decided to do following improvements to support the use case.

  1. If the binding java function has BArray param for the path params, bundle all the path param values in the Ballerina resource function to a BArray value and pass to the native function as discussed in the comment https://github.com/ballerina-platform/ballerina-lang/issues/39737#issuecomment-1451281543

  2. Give a new API from the Environment class to get the function name and path params.

rdulmina commented 1 year ago

@niveathika shall we close this issue if this is fixed or remove the milestone?