From bc09fbe1b48d2ec902e0fd7a3883ae51a59e6222 Mon Sep 17 00:00:00 2001 From: Fabrizio Ferrai Date: Wed, 11 Jul 2018 18:35:11 +0200 Subject: [PATCH] Fix $ in record labels: start emitting quoted ones (#22) --- convert.py | 18 +++++++++++++++--- ...apiextensions.v1beta1.JSONSchemaProps.dhall | 4 ++-- ...apiextensions.v1beta1.JSONSchemaProps.dhall | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/convert.py b/convert.py index e6d9b907..7770cdd6 100755 --- a/convert.py +++ b/convert.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import requests +import re def get_typ(props, required, importing_from_default=False): if '$ref' in props: @@ -79,6 +80,17 @@ def get_static_data(modelSpec): return {} +def labelize(propName): + """ + If a propName doesn't match the 'simple-label' grammar, we return a quoted label + See: https://github.com/dhall-lang/dhall-lang/blob/1d2912067658fdbbc17696fc86f057d6f91712b9/standard/dhall.abnf#L125 + """ + if not re.match("^[a-zA-Z_][a-zA-Z0-9_\-/]*$", propName): + return "`" + propName + "`" + else: + return propName + + url = 'https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json' # See https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/#required-fields @@ -105,7 +117,7 @@ def main(): properties = modelSpec.get('properties', {}) - fields = [" {} : ({})\n".format(propName, get_typ(propVal, propName in required)) + fields = [" {} : ({})\n".format(labelize(propName), get_typ(propVal, propName in required)) for propName, propVal in properties.items()] f.write('{' + ','.join(fields) + '}\n') @@ -124,7 +136,7 @@ def main(): # If there's any required props, we make it a lambda if len([k for k in properties if k in required]) > 0: - params = ['{} : ({})'.format(propName, get_typ(propVal, True, True)) + params = ['{} : ({})'.format(labelize(propName), get_typ(propVal, True, True)) for propName, propVal in properties.items() if propName in param_names] f.write('\(_params : {' + ', '.join(params) + '}) ->\n') @@ -137,7 +149,7 @@ def main(): # If there's no fields, should be an empty record if len(KVs) > 0: - formatted = [" {} = {}\n".format(k, v) for k, v in KVs] + formatted = [" {} = {}\n".format(labelize(k), v) for k, v in KVs] else: formatted = '=' f.write('{' + ','.join(formatted) + '} : ../types/' + modelName + '.dhall\n') diff --git a/default/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall b/default/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall index 7eca7699..9944a98e 100644 --- a/default/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall +++ b/default/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall @@ -1,5 +1,5 @@ -{ $ref = ([] : Optional (Text)) -, $schema = ([] : Optional (Text)) +{ `$ref` = ([] : Optional (Text)) +, `$schema` = ([] : Optional (Text)) , additionalItems = ([] : Optional (../types/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaPropsOrBool.dhall)) , additionalProperties = ([] : Optional (../types/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaPropsOrBool.dhall)) , allOf = ([] : Optional (List ../types/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall)) diff --git a/types/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall b/types/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall index 6ef0ccc5..caa500e7 100644 --- a/types/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall +++ b/types/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall @@ -1,5 +1,5 @@ -{ $ref : (Optional (Text)) -, $schema : (Optional (Text)) +{ `$ref` : (Optional (Text)) +, `$schema` : (Optional (Text)) , additionalItems : (Optional (./io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaPropsOrBool.dhall)) , additionalProperties : (Optional (./io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaPropsOrBool.dhall)) , allOf : (Optional (List ./io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps.dhall))