Skip to content

Commit

Permalink
Merge pull request #726 from samply/convertstodecimal
Browse files Browse the repository at this point in the history
Implement CQL ConvertsTo: Decimal, Integer, Long, Quantity and String
  • Loading branch information
alexanderkiel committed Jun 9, 2022
2 parents f7edb97 + 48c1afc commit 7c4cd44
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 29 deletions.
14 changes: 14 additions & 0 deletions modules/cql/src/blaze/elm/boolean.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
(to-boolean [x] x))


;; 22.24. ToDecimal
(extend-protocol p/ToDecimal
Boolean
(to-decimal [x]
(if (true? x) 1.0 0.0)))


;; 22.25. ToInteger
(extend-protocol p/ToInteger
Boolean
Expand All @@ -28,3 +35,10 @@
Boolean
(to-long [x]
(if (true? x) 1 0)))


;; 22.30. ToString
(extend-protocol p/ToString
Boolean
(to-string [x]
(str x)))
3 changes: 1 addition & 2 deletions modules/cql/src/blaze/elm/compiler/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,4 @@

(extend-protocol p/ToString
Object
(to-string [x]
(str x)))
(to-string [_]))
30 changes: 25 additions & 5 deletions modules/cql/src/blaze/elm/compiler/type_operators.clj
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,37 @@

;; TODO 22.9. ConvertsToDateTime

;; TODO 22.10. ConvertsToDecimal
;; 22.10. ConvertsToDecimal
(defunop converts-to-decimal [operand]
(when (some? operand)
(some? (p/to-decimal operand))))


;; 22.11. ConvertsToLong
(defunop converts-to-long [operand]
(when (some? operand)
(some? (p/to-long operand))))


;; 22.12. ConvertsToInteger
(defunop converts-to-integer [operand]
(when (some? operand)
(some? (p/to-integer operand))))

;; TODO 22.11. ConvertsToLong

;; TODO 22.12. ConvertsToInteger
;; 22.13. ConvertsToQuantity
(defunop converts-to-quantity [operand]
(when (some? operand)
(some? (p/to-quantity operand))))

;; TODO 22.13. ConvertsToQuantity

;; TODO 22.14. ConvertsToRatio

;; TODO 22.15. ConvertsToString
;; 22.15. ConvertsToString
(defunop converts-to-string [operand]
(when (some? operand)
(some? (p/to-string operand))))


;; TODO 22.16. ConvertsToTime

Expand Down
39 changes: 31 additions & 8 deletions modules/cql/src/blaze/elm/date_time.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
[blaze.fhir.spec.type OffsetInstant]
[blaze.fhir.spec.type.system DateTimeYear DateTimeYearMonth DateTimeYearMonthDay]
[java.time LocalDate LocalDateTime LocalTime OffsetDateTime Year YearMonth Instant]
[java.time.temporal ChronoField ChronoUnit Temporal TemporalAccessor]
[java.util Map]))
[java.time.temporal ChronoField ChronoUnit Temporal TemporalAccessor]))


(set! *warn-on-reflection* true)
Expand Down Expand Up @@ -1325,15 +1324,39 @@

String
(to-date-time [s now]
(p/to-date-time (system/parse-date-time s) now))

;; for the anomaly
Map
(to-date-time [_ _]))
(p/to-date-time (system/parse-date-time s) now)))


;; 22.30. ToString
(extend-protocol p/ToString
PrecisionLocalTime
(to-string [{:keys [local-time]}]
(str local-time)))
(str local-time))

Year
(to-string [x]
(str x))

DateTimeYear
(to-string [x]
(str x))

YearMonth
(to-string [x]
(str x))

DateTimeYearMonth
(to-string [x]
(str x))

LocalDate
(to-string [x]
(str x))

DateTimeYearMonthDay
(to-string [x]
(str x))

LocalDateTime
(to-string [x]
(str x)))
5 changes: 3 additions & 2 deletions modules/cql/src/blaze/elm/quantity.clj
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@
String
(to-quantity [s]
;; (+|-)?#0(.0#)?('<unit>')?
(let [[_ value unit] (re-matches #"(\d+(?:\.\d+)?)\s*('[^']+')?" s)]
(let [[_ value unit] (re-matches #"([+-]?\d+(?:\.\d+)?)\s*('[^']+')?" s)]
(when value
(quantity (p/to-decimal value) (or (str/trim unit "'") "1"))))))
(when-let [value (p/to-decimal value)]
(quantity value (or (str/trim unit "'") "1")))))))


;; 22.30. ToString
Expand Down
6 changes: 6 additions & 0 deletions modules/cql/src/blaze/elm/string.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@
(try
(p/to-decimal (BigDecimal. s))
(catch Exception _))))

;; 22.30. ToString
(extend-protocol p/ToString
String
(to-string [s]
(str s)))
8 changes: 7 additions & 1 deletion modules/cql/src/blaze/elm/tuple.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(:import
[java.util Map]))


;; 12.1. Equal
(extend-protocol p/Equal
Map
(equal [x y]
Expand All @@ -17,3 +17,9 @@
false)
false)
true))))

;; 22.23. ToDateTime
(extend-protocol p/ToDateTime
;; for the anomaly
Map
(to-date-time [_ _]))
Loading

0 comments on commit 7c4cd44

Please sign in to comment.