appwrite / functions-starter

31 stars 14 forks source link

🐛 Bug Report: Incorrect Java Syntax #40

Closed FuadEfendi closed 1 year ago

FuadEfendi commented 2 years ago

👟 Reproduction steps

Missing public class Index {...}

👍 Expected behavior

public class Index {...}

👎 Actual Behavior

Missing public class Index {...}

🎲 Appwrite version

Version 1.0.x

💻 Operating system

Linux

🧱 Your Environment

No response

👀 Have you spent some time to check if this issue has been raised before?

🏢 Have you read the Code of Conduct?

FuadEfendi commented 2 years ago

I probably misunderstood: are those templates to create function?

And also, which package contains RuntimeRequest and RuntimeResponse classes? Because Kotlin doesn't have it!

stnguyen90 commented 2 years ago

@FuadEfendi Yes, these are templates and are merged in with the open runtimes code when deployed: https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/java-18.0/src/main/java/io/openruntimes/java

FuadEfendi commented 2 years ago

I got it, Open Runtimes will run build.sh script which will generate Wrapper.java class from Index.java template.

However, template (in this repository) contains bug, env is not defined.

Docker Error: /usr/local/src/src/main/java/io/openruntimes/java/Wrapper.java:53: error: cannot find symbol
    if (env == null
        ^
  symbol:   variable env
  location: class Wrapper

So that instead of this one, I use https://github.com/open-runtimes/open-runtimes/blob/main/runtimes/java-11.0/example/Index.java

Also, RuntimeRequest & RuntimeResponse are here: https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/java-11.0/src/main/java/io/openruntimes/java

FuadEfendi commented 2 years ago

It is very strange to have build.sh script whose only task is to use predefined hardcoded class and package name, and not allowing developers to ensure their code can be compiled and tested.

stnguyen90 commented 2 years ago

@FuadEfendi,

However, template (in this repository) contains bug, env is not defined.

Would you please give more info/context on this? Where exactly is the bug?

It is very strange to have build.sh script whose only task is to use predefined hardcoded class and package name,

I think this was mostly done to avoid requiring developers to add any additional dependencies. Do you have any other suggestion on how to improve this?

not allowing developers to ensure their code can be compiled and tested.

The best way to test is to run the code through open runtimes as instructed in the usage documentation for the respective runtime.

FuadEfendi commented 2 years ago

stnguyen90, env is undefined. It is HashMap? TreeMap? what is env? https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/java-11.0

I follow step by step tutorial to generate function and deploy it, I didn't change any code in provided autogenerated example function and it cannot be built.

Docker Error: /usr/local/src/src/main/java/io/openruntimes/java/Wrapper.java:53: error: cannot find symbol
    if (env == null
        ^
  symbol:   variable env
  location: class Wrapper
/usr/local/src/src/main/java/io/openruntimes/java/Wrapper.java:54: error: cannot find symbol
        || !env.containsKey("APPWRITE_FUNCTION_ENDPOINT")
            ^
  symbol:   variable env
  location: class Wrapper
/usr/local/src/src/main/java/io/openruntimes/java/Wrapper.java:55: error: cannot find symbol
        || !env.containsKey("APPWRITE_FUNCTION_API_KEY")
            ^
  symbol:   variable env
  location: class Wrapper
Note: /usr/local/src/src/main/java/io/openruntimes/java/RuntimeRequest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
 errors

FAILURE: Build failed with an exception.
FuadEfendi commented 2 years ago

The best way to test is to run the code through open runtimes as instructed in the usage documentation for the respective runtime.

My function needs to read data from Postgre and load it to Appwrite in multithreaded environment and a lot of other things such as run AI at Google in-between, and my Gradle has more that 100 lines of code, and I am writing Unit and Integration tests too; there is no way to import RuntimeRequest / RuntimeResponse from Maven public repository, and dependency on org.rapidoid:rapidoid-http-server:5.5.5 is non-documented.

stnguyen90 commented 2 years ago

@FuadEfendi,

stnguyen90, env is undefined. It is HashMap? TreeMap? what is env? https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/java-11.0

I see what you mean about the type of the Variables. How about we use this issue to improve some of that documentation?

I follow step by step tutorial to generate function and deploy it, I didn't change any code in provided autogenerated example function and it cannot be built.

I think there's a typo in the readme you're using. Can you try this?

FuadEfendi commented 2 years ago
var variables = req.getVariables();

    if (env == null
        || !env.containsKey("APPWRITE_FUNCTION_ENDPOINT")
        || !env.containsKey("APPWRITE_FUNCTION_API_KEY")
        || variables.get("APPWRITE_FUNCTION_ENDPOINT") == null
        || variables.get("APPWRITE_FUNCTION_API_KEY") == null) {
        System.out.println("Environment variables are not set. Function cannot use Appwrite SDK.");
    }

stnguyen90, variables are defined, but where env comes from?

My steps are very standard for newbies, and everything should work as prescribed, but it doesn't because of a bug in Java template:

  1. appwrite login
  2. appwrite init project
  3. appwrite init function
  4. appwrite deploy function

Deploy fails; I know why, but newbies don't know yet ;)

FuadEfendi commented 2 years ago

I think simplest solution for complex use cases is to design standard Java application with dependency of io.appwrite:sdk-for-kotlin:1.1.0 and to forget about RuntimeRequest/RuntimeResponse; to deploy it as JAR (or to use source codes); and finally to plug it into template method call:

public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {
    // convert RuntimeRequest to MyRequest
    MyResponse = (new MyApplication(MyReguest...).start();
    // convert MyResponse to RuntimeResponse

I think I can also use precompiled published JAR in custom local repository and configure Gradle for this dependency; and OpenRuntimes has examples too which I need to try: https://github.com/open-runtimes/open-runtimes/blob/main/runtimes/java-11.0/README.md

stnguyen90 commented 2 years ago

stnguyen90, variables are defined, but where env comes from?

@FuadEfendi, ah I see what you mean now! This is what you're referring to:

https://github.com/appwrite/functions-starter/blob/53948b924db3bab390e03c4d10970add1b0de8bb/java-11.0/src/Index.java#L52

This looks like a bug from when we changed req.env to req.variables! We can leave this issue to fix that bug then.