Skip to content

Instantly share code, notes, and snippets.

@petersen-poul
Last active April 11, 2019 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petersen-poul/3dd389ae585075aabf470c3ee59c4b55 to your computer and use it in GitHub Desktop.
Save petersen-poul/3dd389ae585075aabf470c3ee59c4b55 to your computer and use it in GitHub Desktop.
{
"name": "Apply Date Format by Field Name",
"description": "Allows applying a custom date format to a source by matching field names",
"inputs": [
{
"name": "source",
"description": "Source to update",
"type": "source-id"
}
],
"outputs": [
{
"name": "updated-src",
"description": "A link to the updated source with the modified date-time fields",
"type": "source-id"
}
]
}
;
; Compare the field name to a list of patterns. Return
; true/false if it matches any
;
(define (field-matches? fieldname patterns)
(loop (els patterns)
(if (= els []) false
(let (el (head els) rest (tail els))
(if (contains-string? el fieldname) true (recur rest))))))
;
; Given a source id or record, update the field optypes by searching
; for matching field names in each of the input arrays.
;
(define (custom-date-format source field-match date-format)
(let
(source-rec (fetch source)
src-field-ids (keys (get source-rec "fields"))
optype-map
(loop (ids src-field-ids update-map {})
(if (= ids []) update-map
(let
(this (head ids)
rest (tail ids)
field-name (get-in source-rec ["fields" this "name"]))
(if
(field-matches? field-name field-match)
(recur rest (assoc update-map this {"optype" "datetime" "time_formats" [date-format] }))
(recur rest update-map))))))
(update-and-wait source {"fields" optype-map})))
(define updated-src (custom-date-format source ["Date"] "dd-MMM-YYYY HH:mm:ss"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment