argo-python-dsl
argo-python-dsl copied to clipboard
to_yaml should produce valid Argo spec
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 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)
for beginners like me, the workaround is :
if __name__ == "__main__":
print(yaml.dump(sanitize_for_serialization(MyWorkflow().to_dict())))