首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java互操作和clojure宏

java互操作和clojure宏
EN

Stack Overflow用户
提问于 2017-03-25 14:29:55
回答 1查看 182关注 0票数 1

我面临以下问题。我试图在hapi-fhir api之上构造一个函数(宏)。

代码语言:javascript
复制
function (macro) on topo of hapi-fhir api
  (defmacro search-patient-resource
  "This macro searches for a specified resource based on the
  Patient Id"
  [res id json?]
  (let [tmp (symbol res)]
    (if json?
         `(. (. (. (. (. (. @restful-client search) (forResource ~(symbol res))) encodedJson) (where (. (. ~(resolve tmp)  PATIENT)
                    (hasId (str ~id))))) (returnBundle Bundle)) execute)
    )))

当执行类似的操作时,此宏会工作。

代码语言:javascript
复制
(let [id 10465]
 (search-patient-resource "Observation" id true))
=>#object[ca.uhn.fhir.model.dstu2.resource.Bundle 0x520a3cc9 "Bundle[id=Bundle/9ca62ae1-82af-488f-a166-5b014f45886e]"]

但当我做的时候

代码语言:javascript
复制
 (let [id 10465 res "Observation"]
 (search-patient-resource "Observation" id true))
=> CompilerException java.lang.NullPointerException, compiling:(apycare_emrspp/hapi_fhir_helper.clj:122:1)

当然,我不能写(符号~res),因为读者在编译时评估(符号“观察”),我得到

代码语言:javascript
复制
 CompilerException java.lang.IllegalArgumentException: No matching method found: forResource for class ca.uhn.fhir.rest.client.GenericCl
 ient$SearchInternal, compiling:(apycare_emrspp/hapi_fhir_helper.clj:122:1)

也不是

代码语言:javascript
复制
   (resolve (symbol ~res) 

nor

代码语言:javascript
复制
   (resolve ~(symbol re) 

工作。

原始java代码如下所示

代码语言:javascript
复制
 FhirContext ctx = FhirContext.forDstu2();
 String serverBase = "fhirtest.uhn.ca/baseDstu2";
 IGenericClient client = ctx.newRestfulGenericClient(serverBase); 
 Bundle results = client .search() .forResource(Observation.class) 
.where(Observation.PATIENT.hasId("1234")) 
.returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) 
.execute(); 

我所做的就是尝试用

代码语言:javascript
复制
 client
 .search() 
 .forResource(another-resource.class) 
 .where(another-resource.PATIENT.hasId(another-id)) 
 .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) 
 .execute();
EN

回答 1

Stack Overflow用户

发布于 2017-04-14 15:26:22

好的。我面临的问题也是由于我在从不同的名称空间调用代码时忽略了导入适当的符号。当上面代码中的res = "Resource“时,则

代码语言:javascript
复制
 ~(symbol "Resource")

如果我没有第一次添加

代码语言:javascript
复制
(import '(ca.uhn.fhir.model.dstu2.resource.Resource))  

在命名空间中调用宏。这使得代码在大多数情况下都能工作。为了使它充分发挥作用,我不得不改变

代码语言:javascript
复制
 ~(symbol "Resource")

to (标识~(符号“资源”))

这是java的正确翻译

Resource.class到clojure代码

在最后,宏采取了如下形式:

代码语言:javascript
复制
 (defmacro search-patient-resource
 "This macro searches for a specified resource based on the
  Patient Id"
 [res id json?]
 (let [tmp (symbol res)]
   (if json?
     `(. (. (. (. (. (. @restful-client search)
                     (forResource (identity ~(symbol res))))
                  encodedJson)
               (where
                 (.
                  (. ~(resolve tmp)  PATIENT)
                  (hasId (~str ~id)))))
            (returnBundle Bundle))
         execute)
     `(. (. (. (. (. @restful-client search)
                  (forResource (identity ~(symbol res))))
               (where (. (. ~(symbol res)
                            PATIENT)
                         (hasId (str ~id)))))
            (returnBundle Bundle))
         execute))))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43017613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档