From d4724e846cf3285f1a00c70bf5acce1681ff8f4c Mon Sep 17 00:00:00 2001 From: clux Date: Sat, 12 Nov 2022 14:55:53 +0000 Subject: [PATCH 1/2] Add discriminant to integer enums Signed-off-by: clux --- src/analyzer.rs | 11 +++++++---- src/main.rs | 6 +++++- src/output.rs | 2 ++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/analyzer.rs b/src/analyzer.rs index f823c4c..d06e1f7 100644 --- a/src/analyzer.rs +++ b/src/analyzer.rs @@ -36,7 +36,7 @@ fn analyze_( let mut array_recurse_level: HashMap = Default::default(); // create a Container if we have a container type: - //trace!("analyze_ with {} + {}", current, stack); + trace!("analyze_ with {} + {}", current, stack); if schema.type_.clone().unwrap_or_default() == "object" { // we can have additionalProperties XOR properties // https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation @@ -190,13 +190,13 @@ fn analyze_enum_properties( for en in items { debug!("got enum {:?}", en); // TODO: do we need to verify enum elements? only in oneOf only right? - let name = match &en.0 { - serde_json::Value::String(name) => name.to_string(), + let (name, disc) = match &en.0 { + serde_json::Value::String(name) => (name.to_string(), None), serde_json::Value::Number(val) => { if !val.is_u64() { bail!("enum member cannot have signed/floating discriminants"); } - val.to_string() + (val.to_string(), Some(val.as_u64().unwrap())) } _ => bail!("not handling non-string/int enum outside oneOf block"), }; @@ -210,6 +210,7 @@ fn analyze_enum_properties( serde_annot: vec![], extra_annot: vec![], docs: member_doc, + discriminant: disc, }) } Ok(Container { @@ -350,6 +351,7 @@ fn extract_container( serde_annot: vec![], extra_annot: vec![], docs: member_doc, + discriminant: None, }) } else { // option wrapping needed if not required @@ -363,6 +365,7 @@ fn extract_container( ], extra_annot: vec![], docs: member_doc, + discriminant: None, }) // TODO: must capture `default` key here instead of blindly using serde default // this will require us storing default properties for the member in above loop diff --git a/src/main.rs b/src/main.rs index 96643be..6b7ea05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -237,7 +237,11 @@ impl Kopium { let spec_trimmed_type = m.type_.as_str().replace(&format!("{}Spec", kind), kind); if s.is_enum { // NB: only supporting plain enumerations atm, not oneOf - println!(" {},", name); + if let Some(d) = m.discriminant { + println!(" {} = {},", name, d); + } else { + println!(" {},", name); + } } else { println!(" pub {}: {},", name, spec_trimmed_type); } diff --git a/src/output.rs b/src/output.rs index f50f310..c838b65 100644 --- a/src/output.rs +++ b/src/output.rs @@ -42,6 +42,8 @@ pub struct Member { pub extra_annot: Vec, /// Documentation properties extracted from the property pub docs: Option, + /// Discriminant of an integer enum + pub discriminant: Option, } impl Container { From 470549df5343f28943427dad1fe3c40de6083a96 Mon Sep 17 00:00:00 2001 From: clux Date: Sat, 12 Nov 2022 15:06:19 +0000 Subject: [PATCH 2/2] improve tests Signed-off-by: clux --- tests/httproute.yaml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/httproute.yaml b/tests/httproute.yaml index 549869d..5a8c720 100644 --- a/tests/httproute.yaml +++ b/tests/httproute.yaml @@ -7,11 +7,10 @@ spec: parentRefs: - name: gateway namespace: istio-system - sectionName: foobar - - name: gateway - namespace: istio-system - sectionName: same-namespace rules: - - backendRefs: - - name: httpbin - port: 81 + - filters: + - type: RequestRedirect + requestRedirect: + port: 8080 + statusCode: 302 + scheme: https