This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-12-20
Channels
- # adventofcode (23)
- # announcements (4)
- # babashka (1)
- # beginners (37)
- # biff (2)
- # calva (1)
- # cider (19)
- # clj-kondo (11)
- # clojure (45)
- # clojure-bay-area (2)
- # clojure-europe (12)
- # clojure-nl (1)
- # clojure-norway (15)
- # clojure-uk (2)
- # clojurescript (8)
- # conjure (1)
- # cursive (17)
- # datomic (11)
- # garden (1)
- # graalvm (4)
- # hyperfiddle (21)
- # java (10)
- # jobs (3)
- # lsp (23)
- # off-topic (18)
- # polylith (2)
- # re-frame (4)
- # releases (1)
- # remote-jobs (3)
- # rewrite-clj (4)
- # squint (44)
- # uncomplicate (1)
- # xtdb (84)
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 🙂 .expects an object because that is what RequestHandler says handleRequest returns, start by removing all the type hints and see if it works
it looks like something does some reflection to see the event type and construct it to use for java
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
(defrecord IntegerRecord [^Integer x ^Integer y ^String message])
is basically not at all like
record IntegerRecord(int x, int y, String message) {
}
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
you may have better luck using https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-core/src/main/java/com/amazonaws/services/lambda/runtime/RequestStreamHandler.java
https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html#java-handler-interfaces
thanks hired! I'll take a look at both links.