Skip to content

Instantly share code, notes, and snippets.

@mmerce
Last active May 11, 2017 21:39
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 mmerce/bdb3ab06c53b1aaaec844c5715729aaf to your computer and use it in GitHub Desktop.
Save mmerce/bdb3ab06c53b1aaaec844c5715729aaf to your computer and use it in GitHub Desktop.
Updating fields type with a default type
{
"description": "Script that updates the field types for an existing Source. The `base-type` is assigned to all the fields unless otherwise stated in `explicit-types`.\nThe `explicit-types` argument expects a list of [field, type] pairs like:\n\n[[\"field1\", \"categorical\"], [\"field2\", \"numeric\"]]",
"inputs": [
{
"name": "source-id",
"type": "source-id",
"description": "Select the source to be updated"
},
{
"name": "base-type",
"type": "string",
"description": "Type that will be assigned to fields if not specified in explicit-types",
"default": "numeric"
},
{
"name": "explicit-types",
"type": "list",
"description": "List of [field, type] pairs when different from base type (e.g.:[[\"field\", \"categorical\"], [\"field2\", \"numeric\"]])",
"default": []
}
]
}
;; Auxiliar function to get the field ID
;; corresponding to each explicitly typed field
(define (get-field-id fields field-name)
(let (field-ids (keys fields))
(loop (field-ids field-ids)
(if (= (fields [(head field-ids) "name"]) field-name)
(head field-ids)
(recur (tail field-ids))))))
;;Function to change fields types
;;It accepts a default type and a list of [field, type] pairs:
;; e.g.: [["field", "categorical"], ["field2", "numeric"]]
(define (change-field-types source-id base-type explicit-types)
(if (not (list? explicit-types))
(raise "Explicit types must be specified as a list of pairs")
(let (source (fetch source-id)
fields (resource-fields source)
field-ids (keys fields)
new-types (iterate (new-types {} field-id field-ids)
(assoc new-types field-id {"optype" base-type}))
new-types (iterate (new-types new-types pair explicit-types)
(assoc new-types (get-field-id fields (pair 0)) {"optype" (pair 1)})))
(update source-id {"fields" new-types}))))
(change-field-types source-id base-type explicit-types)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment