Fork me on GitHub
#java
<
2023-12-20
>
Drew Verlee23:12:49

Any advice on how to turn this java into clojure? https://github.com/awsdocs/aws-lambda-developer-guide/blob/main/sample-apps/java17-examples/src/main/java/example/HandlerIntegerJava17.java

public class HandlerIntegerJava17 implements RequestHandler{

  @Override
  /*
   * Takes in an InputRecord, which contains two integers and a String.
   * Logs the String, then returns the sum of the two Integers.
   */
  public Integer handleRequest(IntegerRecord event, Context context)
  {
    LambdaLogger logger = context.getLogger();
    logger.log("String found: " + event.message());
    return event.x() + event.y();
  }
}

record IntegerRecord(int x, int y, String message) {
}
My attempt at the clojure version:
(defrecord IntegerRecord [^Integer x ^Integer y ^String message])

(reify RequestHandler
  (^Integer handleRequest [_ ^IntegerRecord {:keys [x y message]} ^Context context]
    (let [^LambdaLogger logger (.getLogger context)]
      (.log logger message)
      (+ x y))))
exception i get when i eval the clojure code:
;; => Syntax error (IllegalArgumentException) compiling reify* at (src/core.clj:26:1).
;;    Mismatched return type: handleRequest, expected: java.lang.Object, had: java.lang.Integer
Why is it a mismatched type a syntax error? Why does it expect an object? How does clojure translate the @override annotation? Beyond that, i'm guessing i have some other deeper misunderstandings that need to be corrected 🙂 .

hiredman23:12:12

expects an object because that is what RequestHandler says handleRequest returns, start by removing all the type hints and see if it works

hiredman23:12:17

it looks like something does some reflection to see the event type and construct it to use for java

hiredman23:12:58

I would recommend not bothering to try and make that work with clojure, I haven't use aws lambdas, but I believe there is some kind of generic request structure they can get, just use that

hiredman23:12:43

(defrecord IntegerRecord [^Integer x ^Integer y ^String message]) is basically not at all like

record IntegerRecord(int x, int y, String message) {
}

hiredman23:12:43

the generated class for the defrecord will have type Object fields (the type hint would apply to any inline protocol / interface usage in the body of the defrecord) and it does not create a record class

👀 1
Drew Verlee23:12:25

thanks hired! I'll take a look at both links.