argo-python-dsl icon indicating copy to clipboard operation
argo-python-dsl copied to clipboard

to_yaml should produce valid Argo spec

Open process0 opened this issue 4 years ago • 2 comments

Currently the to_yaml function will produce output that is not valid to be used with argo. Examples:

api_version: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generate_name: child-
  name: ''

Running yaml.dump(sanitize_for_serialization(wf)) will produce valid spec.

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: child-
  name: ''

process0 avatar Dec 21 '20 16:12 process0

@process0 I have actually implemented this function if you feel like opening a PR 😄

def sanitize_for_serialization(wf_dict: dict):
    def camelize(snake_str):
        components = snake_str.split('_')
        return components[0] + ''.join(x.title() for x in components[1:])

    def update_value(val, is_key=False):
        if isinstance(val, dict):
            return {update_value(key, is_key=True): update_value(value, is_key=False) for key, value in val.items()}
        elif isinstance(val, list):
            return [update_value(item, is_key=False) for item in val]
        elif is_key and isinstance(val, str):
            return camelize(val)
        return val

    return update_value(wf_dict)

samuelamar15 avatar Jan 16 '21 21:01 samuelamar15

for beginners like me, the workaround is :


if __name__ == "__main__":
    print(yaml.dump(sanitize_for_serialization(MyWorkflow().to_dict())))

christopheblin avatar May 07 '21 14:05 christopheblin