Skip to content

Commit

Permalink
Refactor and rename add-defaults fn
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinqian00 committed Jun 29, 2023
1 parent c33f996 commit c869631
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
5 changes: 1 addition & 4 deletions src/cli/com/yetanalytics/datasim/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@
[(partial input/validate-throw :parameters)
"Failed to validate Parameters."]
[])
;; TODO: it looks like, when the validation is skipped, a simple empty
;; default doesn't work here, as the full input spec fails.
;; For now we just hack it by calling the defaults fn directly.
:default (params/add-defaults {})]
:default (params/apply-defaults)]

["-i" "--input URI" "Combined Simulation input"
:id :input
Expand Down
6 changes: 3 additions & 3 deletions src/main/com/yetanalytics/datasim/input.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
;; alignments input vector, and the inner vector associated with each actor.
;; Find separate names for each.
(s/def ::alignments
::alignments/alignments-vector)
::alignments/alignment-vector)

(s/def ::parameters
::params/parameters)
Expand Down Expand Up @@ -76,7 +76,7 @@

(defmethod from-location [:input :json] [_ _ location]
(-> (dio/read-json-location location)
(update :parameters params/add-defaults)))
(update :parameters params/apply-defaults)))

(defmethod from-location [:profile :json] [_ _ location]
(dio/read-json-location location))
Expand All @@ -98,7 +98,7 @@

(defmethod from-location [:parameters :json] [_ _ location]
(->> (dio/read-json-location location)
params/add-defaults))
params/apply-defaults))

;; Write to file

Expand Down
25 changes: 15 additions & 10 deletions src/main/com/yetanalytics/datasim/input/parameters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[xapi-schema.spec :as xs]
[com.yetanalytics.pan.objects.profile :as prof]
[com.yetanalytics.pan.objects.pattern :as pat]
[com.yetanalytics.datasim.random :as random]
[com.yetanalytics.datasim.util.errors :as errs])
(:import [clojure.lang ExceptionInfo]
[java.time.zone ZoneRulesException]
Expand Down Expand Up @@ -99,13 +100,17 @@
;; Defaults
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn add-defaults
"Generate defualts"
[{:keys [start from timezone seed] :as params}]
(merge
params
(let [start (or start (.toString (Instant/now)))]
{:start start
:from (or from start)
:timezone (or timezone "UTC")
:seed (or seed (.nextLong (Random.)))})))
(defn apply-defaults
"Generate defaults and apply them to the existing `params` map. If no
`params` is provided, simply generate the default map with the current
time and a random seed."
([]
(apply-defaults {}))
([{:keys [start from timezone seed] :as params}]
(merge
params
(let [start (or start (.toString (Instant/now)))]
{:start start
:from (or from start)
:timezone (or timezone "UTC")
:seed (or seed (random/rand-long (Random.)))}))))

0 comments on commit c869631

Please sign in to comment.