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

Added btreemap flag to toggle BTreeMap/HashMap for map types #218

Merged
merged 2 commits into from
Apr 1, 2024
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
23 changes: 13 additions & 10 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const IGNORED_KEYS: [&str; 3] = ["metadata", "apiVersion", "kind"];
#[derive(Default)]
pub struct Config {
pub no_condition: bool,
pub btreemap: bool,
pub relaxed: bool,
}

Expand Down Expand Up @@ -252,7 +253,8 @@ fn extract_container(
dict_key = Some("serde_json::Value".into());
}
if let Some(dict) = dict_key {
format!("BTreeMap<String, {}>", dict)
let map_type = if cfg.btreemap { "BTreeMap" } else { "HashMap" };
format!("{}<String, {}>", map_type, dict)
} else {
format!("{}{}", stack, key.to_upper_camel_case())
}
Expand Down Expand Up @@ -427,7 +429,8 @@ fn array_recurse_for_type(
}

let vec_value = if let Some(dict_value) = dict_value {
format!("BTreeMap<String, {dict_value}>")
let map_type = if cfg.btreemap { "BTreeMap" } else { "HashMap" };
format!("{map_type}<String, {dict_value}>")
} else {
let structsuffix = key.to_upper_camel_case();
format!("{stack}{structsuffix}")
Expand Down Expand Up @@ -594,7 +597,7 @@ mod test {
// should have a member with a key to the map:
let map = &root.members[0];
assert_eq!(map.name, "validationsInfo");
assert_eq!(map.type_, "Option<BTreeMap<String, AgentValidationsInfo>>");
assert_eq!(map.type_, "Option<HashMap<String, AgentValidationsInfo>>");
// should have a separate struct
let other = &structs[1];
assert_eq!(other.name, "AgentValidationsInfo");
Expand Down Expand Up @@ -644,7 +647,7 @@ type: object
assert_eq!(server_selector.level, 1);
let match_labels = &server_selector.members[0];
assert_eq!(match_labels.name, "matchLabels");
assert_eq!(match_labels.type_, "BTreeMap<String, serde_json::Value>");
assert_eq!(match_labels.type_, "HashMap<String, serde_json::Value>");
}

#[test]
Expand Down Expand Up @@ -693,7 +696,7 @@ type: object
assert_eq!(root.name, "Options");
assert_eq!(root.level, 0);
assert_eq!(&root.members[0].name, "options");
assert_eq!(&root.members[0].type_, "Option<BTreeMap<String, bool>>");
assert_eq!(&root.members[0].type_, "Option<HashMap<String, bool>>");
}

#[test]
Expand Down Expand Up @@ -871,7 +874,7 @@ type: object
assert_eq!(&ps.members[0].name, "MatchExpressions");
assert_eq!(&ps.members[0].type_, "Vec<ServerPodSelectorMatchExpressions");
assert_eq!(&ps.members[1].name, "MatchLabels");
assert_eq!(&ps.members[1].type_, "BTreeMap<String, String>");
assert_eq!(&ps.members[1].type_, "HashMap<String, String>");

// should have the inner struct match expressions
let me = &structs[2];
Expand Down Expand Up @@ -940,7 +943,7 @@ type: object
// should have an params member:
let member = &eps.members[0];
assert_eq!(member.name, "params");
assert_eq!(member.type_, "Option<BTreeMap<String, String>>");
assert_eq!(member.type_, "Option<HashMap<String, String>>");
}

#[test]
Expand Down Expand Up @@ -1004,7 +1007,7 @@ type: object
assert_eq!(from.name, "from");
assert_eq!(to.name, "to");
assert_eq!(from.type_, "Option<String>");
assert_eq!(to.type_, "Option<BTreeMap<String, i32>>");
assert_eq!(to.type_, "Option<HashMap<String, i32>>");
}

#[test]
Expand Down Expand Up @@ -1097,7 +1100,7 @@ type: object
assert_eq!(&root.members[0].name, "jwtTokensByRole");
assert_eq!(
&root.members[0].type_,
"Option<BTreeMap<String, AppProjectStatusJwtTokensByRole>>"
"Option<HashMap<String, AppProjectStatusJwtTokensByRole>>"
);
let role = &structs[1];
assert_eq!(role.level, 1);
Expand Down Expand Up @@ -1159,7 +1162,7 @@ type: object
assert_eq!(structs[0].members[0].name, "records");
assert_eq!(
structs[0].members[0].type_,
"Option<Vec<BTreeMap<String, String>>>"
"Option<Vec<HashMap<String, String>>>"
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ struct Kopium {
/// Condition API instead of generating a custom definition.
#[arg(long)]
no_condition: bool,

/// Use BTreeMap to represent the map (additionalProperties) types.
///
/// If false, HashMap is defaulted in representing the map types.
#[arg(long)]
btreemap: bool,
}

#[derive(Clone, Copy, Debug, Subcommand)]
Expand Down Expand Up @@ -199,6 +205,7 @@ impl Kopium {
log::debug!("schema: {}", serde_json::to_string_pretty(&schema)?);
let cfg = Config {
no_condition: self.no_condition,
btreemap: self.btreemap,
relaxed: self.relaxed,
};
let structs = analyze(schema, kind, cfg)?
Expand Down
Loading