From 4fec2167a8df5ac519b22c64af07cc62a949202a Mon Sep 17 00:00:00 2001 From: Fabrizio Ferrai Date: Mon, 6 Aug 2018 17:13:33 +0200 Subject: [PATCH] Support 'format' key from the swagger schema (#28) Fix #12 by introducing a `< Int : Natural | String : Text >` Union for the "int-or-string" format key. --- README.md | 7 ++++--- convert.py | 16 +++++++++++++--- ...pimachinery.pkg.util.intstr.IntOrString.dhall | 2 +- examples/ingress.dhall | 3 ++- examples/out/ingress.yaml | 4 ++-- ...pimachinery.pkg.util.intstr.IntOrString.dhall | 2 +- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 28266134..df3052f0 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,7 @@ in let Ingress = ../types/io.k8s.api.extensions.v1beta1.Ingress.dhall in let defaultIngress = ../default/io.k8s.api.extensions.v1beta1.Ingress.dhall in let defaultMeta = ../default/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.dhall in let defaultSpec = ../default/io.k8s.api.extensions.v1beta1.IngressSpec.dhall +in let IntOrString = ../default/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall -- Our Service type in let Service = ./Service.dhall @@ -183,7 +184,7 @@ in let makeRule = \(service : Service) -> { host = Some Text service.host , http = Some RuleVal { paths = [ { backend = { serviceName = service.name - , servicePort = "80" + , servicePort = IntOrString.Int 80 } , path = None Text }]}} @@ -244,13 +245,13 @@ spec: - http: paths: - backend: - servicePort: '80' + servicePort: 80 serviceName: foo host: foo.example.com - http: paths: - backend: - servicePort: '80' + servicePort: 80 serviceName: default host: default.example.com tls: diff --git a/convert.py b/convert.py index 95bbbf05..ffa7b6ab 100755 --- a/convert.py +++ b/convert.py @@ -45,6 +45,9 @@ def build_type(schema, path_prefix, schema_name=None): return '(List {mapKey : Text, mapValue : Text})' elif typ == 'array': return 'List {}'.format(build_type(schema['items'], path_prefix)) + # Fix for the funny format parameters they use in Kube + elif typ == 'string' and 'format' in schema and schema['format'] == 'int-or-string': + return '< Int : Natural | String : Text >' else: return { 'string' : 'Text', @@ -131,7 +134,13 @@ def main(): f.write('{}\n'.format(build_type(modelSpec, '.', modelName))) with open('default/' + modelName + '.dhall', 'w') as f: if 'type' in modelSpec: - f.write('\(a : {}) -> a\n'.format(build_type(modelSpec, '../types'))) + typ = build_type(modelSpec, '../types') + # In case we have a union, we make the constructors for it + if typ[0] == '<': + f.write('constructors {}\n'.format(typ)) + # Otherwise we just output the identity + else: + f.write('\(a : {}) -> a\n'.format(typ)) elif '$ref' in modelSpec: path = schema_path_from_ref('.', modelSpec['$ref']) f.write('{}\n'.format(path)) @@ -145,8 +154,9 @@ def main(): resource_data = get_static_data(modelSpec) param_names = required - set(resource_data.keys()) - # If there's any required props, we make it a lambda - if len([k for k in properties if k in required]) > 0: + # If there's multiple required props, we make it a lambda + requiredProps = [k for k in properties if k in required] + if len(requiredProps) > 0: params = ['{} : ({})'.format(labelize(propName), build_type(propVal, '../types')) for propName, propVal in properties.items() if propName in param_names] diff --git a/default/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall b/default/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall index a3a707e5..59501bd4 100644 --- a/default/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall +++ b/default/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall @@ -1 +1 @@ -\(a : Text) -> a +constructors < Int : Natural | String : Text > diff --git a/examples/ingress.dhall b/examples/ingress.dhall index 6d32bcda..7a27bc2e 100644 --- a/examples/ingress.dhall +++ b/examples/ingress.dhall @@ -12,6 +12,7 @@ in let Ingress = ../types/io.k8s.api.extensions.v1beta1.Ingress.dhall in let defaultIngress = ../default/io.k8s.api.extensions.v1beta1.Ingress.dhall in let defaultMeta = ../default/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.dhall in let defaultSpec = ../default/io.k8s.api.extensions.v1beta1.IngressSpec.dhall +in let IntOrString = ../default/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall -- Our Service type in let Service = ./Service.dhall @@ -27,7 +28,7 @@ in let makeRule = \(service : Service) -> { host = Some Text service.host , http = Some RuleVal { paths = [ { backend = { serviceName = service.name - , servicePort = "80" + , servicePort = IntOrString.Int 80 } , path = None Text }]}} diff --git a/examples/out/ingress.yaml b/examples/out/ingress.yaml index 9f0565df..2d0a358d 100644 --- a/examples/out/ingress.yaml +++ b/examples/out/ingress.yaml @@ -5,13 +5,13 @@ spec: - http: paths: - backend: - servicePort: '80' + servicePort: 80 serviceName: foo host: foo.example.com - http: paths: - backend: - servicePort: '80' + servicePort: 80 serviceName: default host: default.example.com tls: diff --git a/types/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall b/types/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall index 3de705a4..044b8fb2 100644 --- a/types/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall +++ b/types/io.k8s.apimachinery.pkg.util.intstr.IntOrString.dhall @@ -1 +1 @@ -Text +< Int : Natural | String : Text >