Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow integer enums to have correct integer serialization #104

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn analyze_(
let mut array_recurse_level: HashMap<String, u8> = 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
Expand Down Expand Up @@ -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"),
};
Expand All @@ -210,6 +210,7 @@ fn analyze_enum_properties(
serde_annot: vec![],
extra_annot: vec![],
docs: member_doc,
discriminant: disc,
})
}
Ok(Container {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct Member {
pub extra_annot: Vec<String>,
/// Documentation properties extracted from the property
pub docs: Option<String>,
/// Discriminant of an integer enum
pub discriminant: Option<u64>,
}

impl Container {
Expand Down
13 changes: 6 additions & 7 deletions tests/httproute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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