1

I'm new to Clojure, and I'm working with an application that has leiningen + clojure + java the problem is with the plugin lein-environ "1.0.0".

within the app, its using a lot the "env" map. either by reading existing values defined within the .lein_env or project.clj. But within the same application is doing a some:

(attach! :my-val (str (env :api-host) "/api/" (env :resource)))

if I print this value with a (println (env :my-val)) I do see the content. But if I try to send it to Java as a Map, I dont see this value. I get the previously defined values. I mean everything from .lein_env, but none application calculated value.

I havent found any api or documentation regarding the attach! function, so to be honest I dont know what it does. But since the values are read from env, I thought it was attaching those keys to the map.

I tried to convert the env to a HashMap within clojure with:

(defn- creat-env-hash-map []
    (java.util.HashMap. (env)))

But still getting the same result, those values are not mapped.

I wonder what am I doing wrong and if this is even possible.

Thanks a lot for your help.

Edit:

I call the java code in the way:

(println (com.my.package.HelperClass/staticMethod env))

The static method in java:

public static String staticMethod(HashMap<String, String> map) {
    String result = "";
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        result += pair.getKey() + " = " + pair.getValue() + "\n";
        it.remove(); // avoids a ConcurrentModificationException
    }
    return result;
}
cesaregb
  • 765
  • 1
  • 11
  • 27
  • When you say "I try to send it to Java as a Map", what are you doing? Can you please post that line of code? – BenC Dec 21 '15 at 03:05
  • 1
    You probably don't need to convert, a clojure `{}` is already a java HashMap. – noisesmith Dec 21 '15 at 03:11
  • @noisesmith yea, you are right, I tried the convert just to confirm that I wasen't missing something, thanks for confirm that it is a HashMap already – cesaregb Dec 21 '15 at 03:39
  • @BenC I've added the simple interaction example I've been working with after Edit: As I've mentioned I'm new to clojure I maybe missing basic stuff yet. – cesaregb Dec 21 '15 at 03:39

2 Answers2

1

Ok, it looks like Clojure's persistent maps implement java.util.Map but not java.util.HashMap. This means that your Java method needs to take a Map<Object, Object>.

If you need a HashMap in Java, your Java method can pass the map from Clojure into the HashMap constructor.

Is this method the actual one you're using, or a simplification?

BenC
  • 451
  • 3
  • 7
0

I was not understanding correctly the "env" in clojure. Environ has it as a function as well, its a kind of a smart object that when you call it like (env :value) calls the function but not all the values are kind of added to the same map.

Environ has this kind of a context where it host some values you set in executions time.

What I end up doing was creating another map hosting both the "pre-defined" values and the ones

(defn- creat-env-hash-map []
(java.util.HashMap. (env)))

(defn- complete-hash-map []
    (doto (creat-env-hash-map)
        (.put "value-a" (str (env :value-a)))
        (.put "value-b" (str (env :value-b)))
))

I maybe not using the best approach but as I mentioned, I'm new to clojure. If someone else has the correct answer is more than welcome.

Regards

cesaregb
  • 765
  • 1
  • 11
  • 27