Skip to content

Commit

Permalink
new custom cast for quorum
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jan 13, 2016
1 parent 2ea56e4 commit e5c02b5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/quorum/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import locale
import random
import datetime
import itertools
import threading
import subprocess

Expand All @@ -65,6 +66,15 @@
representing sorting with the current infra-structure
number way of representing the same information """

CASTERS = {
list : lambda v: [y for y in itertools.chain(*[x.split(",") for x in v])],
bool : lambda v: v if type(v) == bool else\
not v in ("", "0", "false", "False")
}
""" The map associating the various data types with a proper custom
caster to be used for special data types (more complex) under some
of the simple casting operations """

defines = defines

def to_limit(limit_s):
Expand Down Expand Up @@ -161,6 +171,10 @@ def get_field(name, default = None, cast = None):
value = flask.request.form.get(name, value)
value = flask.request.args.get(name, value)

# in case a cast operation is defined, tries to retrieve a possible
# indirect/custom caster for the current cast operation
if cast: cast = CASTERS.get(cast, cast)

# in case the cast type value is set and the value is not invalid tries
# to cast the value into the requested cast type
if cast and not value in (None, ""): value = cast(value)
Expand Down

0 comments on commit e5c02b5

Please sign in to comment.