common-workflow-lab / python-cwlgen

Generation of CWL programmatically. Available types: CommandLineTool and DockerRequirement
https://github.com/common-workflow-language/cwl-utils
MIT License
29 stars 13 forks source link

Fix nested arrays #18

Closed illusional closed 5 years ago

illusional commented 5 years ago

Add parsing and test of nested arrays (objs with get_dict)

Idea: generic to_dict where all objects inherit from some base "serialisable" that has some base get_dict method with:

@staticmethod
def serialize(obj):
    if isinstance(v, str) or isinstance(v, int) or isinstance(v, float) or isinstance(v, bool):
        return v
    if isinstance(v, list):
        return [serialize(x) for x in v]
    if isinstance(v, dict):
        return {k: serialize(v) for k,v in v.items() if v is not None}
    if callable(getattr(v, "get_dict", None)):
        return v.get_dict()
    raise Exception(f"Can't serialize '{type(obj)}'")

def get_dict(self):
    return {k:serialize(v) for k,v in vars(self).items if v is not None)        
illusional commented 5 years ago

Ignore this merge request, I chose the wrong target repo.