Passed
Push — master ( 22acc6...f41365 )
by Amresh
02:01
created

schema_to_object_builder()   A

Complexity

Conditions 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 4
nop 2
1
from fastest.fuzzers.primitive_fuzzer.fuzz import random_integers, random_ascii_chars, random_float
2
3
4
def build_empty_of_type(item_type):
5
    empty_values_for_types = {
6
        "str": random_ascii_chars,
7
        "int": random_integers,
8
        "float": random_float,
9
        "list": lambda: [],
10
        "dict": lambda: {}
11
    }
12
    return empty_values_for_types[item_type]
13
14
15
def schema_to_object_builder(schema, name='__root__'):
16
    root_object = build_empty_of_type(schema['type'])()
17
18
    if schema['type'] == 'list':
19
        root_object.append(schema_to_object_builder(schema['inner']))
20
21
    elif schema['type'] == 'dict':
22
        for prop in schema['inner']:
23
            root_object[prop['name']] = schema_to_object_builder(prop)
24
25
    return root_object
26