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

treat x-kubernetes-int-or-string as IntOrString #43

Merged
merged 1 commit into from
Apr 9, 2022
Merged
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
32 changes: 27 additions & 5 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ fn analyze_object_properties(
}
"" => {
if s.x_kubernetes_int_or_string.is_some() {
warn!("coercing presumed IntOrString {} to String", key);
Some("String".into())
Some("IntOrString".into())
} else {
bail!("unknown empty dict type for {}", key)
}
Expand Down Expand Up @@ -190,8 +189,7 @@ fn analyze_object_properties(
}
"" => {
if value.x_kubernetes_int_or_string.is_some() {
warn!("coercing presumed IntOrString {} to String", key);
"String".into()
"IntOrString".into()
} else {
bail!("unknown empty dict type for {}", key)
}
Expand Down Expand Up @@ -332,7 +330,6 @@ fn uppercase_first_letter(s: &str) -> String {
}
}


// unit tests particular schema patterns
#[cfg(test)]
mod test {
Expand Down Expand Up @@ -393,4 +390,29 @@ mod test {
assert_eq!(other.members[2].name, "status");
assert_eq!(other.members[2].type_, "String");
}

#[test]
fn int_or_string() {
let schema_str = r#"
properties:
port:
description: A port name or number. Must exist in a pod spec.
x-kubernetes-int-or-string: true
required:
- port
type: object
"#;
let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();

let mut structs = vec![];
analyze(schema, "ServerSpec", "Server", 0, &mut structs).unwrap();
let root = &structs[0];
assert_eq!(root.name, "Server");
assert_eq!(root.level, 0);
// should have an IntOrString member:
let member = &root.members[0];
assert_eq!(member.name, "port");
assert_eq!(member.type_, "IntOrString");
assert!(root.uses_int_or_string());
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl OutputStruct {
pub fn uses_date(&self) -> bool {
self.members.iter().any(|m| m.type_.contains("NaiveDate"))
}

pub fn uses_int_or_string(&self) -> bool {
self.members.iter().any(|m| m.type_.contains("IntOrString"))
}
}

mod analyzer;
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ fn print_prelude(results: &[OutputStruct]) {
if results.iter().any(|o| o.uses_date()) {
println!("use chrono::naive::NaiveDate;");
}
if results.iter().any(|o| o.uses_int_or_string()) {
println!("use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;");
}
println!();
}

Expand Down