Commits

taka2ru committed 6646068

using multi-method for type conversion

Comments (0)

Files changed (1)

src/clj_soap/core.clj

        .getSchemaType .getName
        keyword))
 
-(defn value-str [valobj argtype]
-  (case argtype
-    :integer (str valobj)
-    :double (str valobj)
-    :string (str valobj)
-    :anyType (str valobj)
-    :boolean (str valobj))) 
+(defmulti obj->soap-str (fn [obj argtype] argtype))
 
-(defn get-result [op retelem]
-  (let [ret-str (.getText (first (iterator-seq (.getChildElements retelem))))]
-    (case (axis-op-rettype op)
-      :integer (Integer/parseInt ret-str)
-      :double (Double/parseDouble ret-str)
-      :string ret-str
-      :anyType ret-str
-      :boolean (Boolean/parseBoolean ret-str))))
+(defmethod obj->soap-str :integer [obj argtype] (str obj))
+(defmethod obj->soap-str :double [obj argtype] (str obj))
+(defmethod obj->soap-str :string [obj argtype] (str obj))
+(defmethod obj->soap-str :boolean [obj argtype] (str obj))
+(defmethod obj->soap-str :anyType [obj argtype] (str obj))
+
+(defmulti soap-str->obj (fn [obj argtype] argtype))
+
+(defmethod soap-str->obj :integer [soap-str argtype] (Integer/parseInt soap-str))
+(defmethod soap-str->obj :double [soap-str argtype] (Double/parseDouble soap-str))
+(defmethod soap-str->obj :string [soap-str argtype] soap-str)
+(defmethod soap-str->obj :boolean [soap-str argtype] (Boolean/parseBoolean soap-str))
+(defmethod soap-str->obj :anyType [soap-str argtype] soap-str)
 
 (defn make-client [url]
   (doto (org.apache.axis2.client.ServiceClient. nil (java.net.URL. url) nil nil)
       (.addChild request
                  (doto (.createOMElement
                          factory (javax.xml.namespace.QName. (:name argtype)))
-                   (.setText (value-str argval (:type argtype))))))
+                   (.setText (obj->soap-str argval (:type argtype))))))
     request))
 
+(defn get-result [op retelem]
+  (let [ret-str (.getText (first (iterator-seq (.getChildElements retelem))))]
+    (soap-str->obj ret-str (axis-op-rettype op))))
+
 (defn client-call [client op & args]
   (if (isa? (class op) org.apache.axis2.description.OutOnlyAxisOperation)
     (.sendRobust client (.getName op) (apply make-request op args))