我面临以下问题。我试图在hapi-fhir api之上构造一个函数(宏)。
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)
)))当执行类似的操作时,此宏会工作。
(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]"]但当我做的时候
(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),因为读者在编译时评估(符号“观察”),我得到
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)也不是
(resolve (symbol ~res) nor
(resolve ~(symbol re) 工作。
原始java代码如下所示
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(); 我所做的就是尝试用
client
.search()
.forResource(another-resource.class)
.where(another-resource.PATIENT.hasId(another-id))
.returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class)
.execute();发布于 2017-04-14 15:26:22
好的。我面临的问题也是由于我在从不同的名称空间调用代码时忽略了导入适当的符号。当上面代码中的res = "Resource“时,则
~(symbol "Resource")如果我没有第一次添加
(import '(ca.uhn.fhir.model.dstu2.resource.Resource)) 在命名空间中调用宏。这使得代码在大多数情况下都能工作。为了使它充分发挥作用,我不得不改变
~(symbol "Resource")to (标识~(符号“资源”))
这是java的正确翻译
从Resource.class到clojure代码
在最后,宏采取了如下形式:
(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))))https://stackoverflow.com/questions/43017613
复制相似问题