Skip to content

Commit

Permalink
Impl Borrow so that lookups don't need cloning.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnavion committed Feb 25, 2023
1 parent a369e19 commit 9c2838e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion k8s-openapi-codegen-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ fn evaluate_trait_bound(

swagger20::SchemaKind::Ref(ref_path @ swagger20::RefPath { .. }) if !ref_path.references_scope(map_namespace) => {
let trait_bound =
if let Some(target) = definitions.get(&swagger20::DefinitionPath(ref_path.path.clone())) {
if let Some(target) = definitions.get(&*ref_path.path) {
let mut visited = visited.clone();
evaluate_trait_bound_inner(
&std::borrow::Cow::Borrowed(&target.kind),
Expand Down
12 changes: 12 additions & 0 deletions k8s-openapi-codegen-common/src/swagger20/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct DefinitionPath(pub String);

impl std::borrow::Borrow<str> for DefinitionPath {
fn borrow(&self) -> &str {
&self.0
}
}

impl std::ops::Deref for DefinitionPath {
type Target = str;

Expand Down Expand Up @@ -35,6 +41,12 @@ pub enum NumberFormat {
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct PropertyName(pub String);

impl std::borrow::Borrow<str> for PropertyName {
fn borrow(&self) -> &str {
&self.0
}
}

impl std::ops::Deref for PropertyName {
type Target = str;

Expand Down
16 changes: 8 additions & 8 deletions k8s-openapi-codegen/src/fixups/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub(crate) mod json_ty {
// The original `DeleteOptions` type is still kept since it's used as a field of `io.k8s.api.policy.v1beta1.Eviction`
pub(crate) fn create_delete_optional(spec: &mut crate::swagger20::Spec) -> Result<(), crate::Error> {
let delete_options_schema =
spec.definitions.get(&crate::swagger20::DefinitionPath("io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions".to_owned()))
spec.definitions.get("io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions")
.ok_or("could not find io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions")?;
let crate::swagger20::SchemaKind::Properties(delete_options_properties) = &delete_options_schema.kind else {
return Err("io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions is not a SchemaKind::Properties".into());
Expand Down Expand Up @@ -188,7 +188,7 @@ pub(crate) fn create_optionals(spec: &mut crate::swagger20::Spec) -> Result<(),
}

// Not useful for programmatic clients.
let _ = optional_definition.remove(&crate::swagger20::PropertyName("pretty".to_owned()));
let _ = optional_definition.remove("pretty");

spec.definitions.insert(crate::swagger20::DefinitionPath(type_name.to_string()), crate::swagger20::Schema {
description: Some(format!("Common parameters for all {description} operations.")),
Expand Down Expand Up @@ -408,8 +408,8 @@ pub(crate) fn separate_watch_from_list_operations(spec: &mut crate::swagger20::S
}

// Not useful for programmatic clients.
let _ = list_optional_definition.remove(&crate::swagger20::PropertyName("pretty".to_owned()));
let _ = watch_optional_definition.remove(&crate::swagger20::PropertyName("pretty".to_owned()));
let _ = list_optional_definition.remove("pretty");
let _ = watch_optional_definition.remove("pretty");

spec.definitions.insert(crate::swagger20::DefinitionPath("io.k8s.ListOptional".to_string()), crate::swagger20::Schema {
description: Some("Common parameters for all list operations.".to_string()),
Expand Down Expand Up @@ -612,7 +612,7 @@ pub(crate) fn list(spec: &mut crate::swagger20::Spec) -> Result<(), crate::Error
};

let item_schema =
spec.definitions.get(&crate::swagger20::DefinitionPath(item_ref_path.path.clone()))
spec.definitions.get(&*item_ref_path.path)
.ok_or_else(|| format!("definition {definition_path} looks like a list but its item's definition does not exist in the spec"))?;

let [item_kubernetes_group_kind_version] = &item_schema.kubernetes_group_kind_versions[..] else {
Expand Down Expand Up @@ -694,7 +694,7 @@ pub(crate) fn list(spec: &mut crate::swagger20::Spec) -> Result<(), crate::Error
// Remove all list types

for (definition_path, item_ref_path, list_kind) in &list_definition_paths {
let item_definition = spec.definitions.get_mut(&crate::swagger20::DefinitionPath(item_ref_path.path.clone())).unwrap();
let item_definition = spec.definitions.get_mut(&*item_ref_path.path).unwrap();
item_definition.list_kind = Some(list_kind.clone());

let _ = spec.definitions.remove(definition_path);
Expand Down Expand Up @@ -827,7 +827,7 @@ pub(crate) fn response_types(spec: &mut crate::swagger20::Spec) -> Result<(), cr

if let Some(kubernetes_group_kind_version) = &operation.kubernetes_group_kind_version {
let response_schema =
definitions.get(&crate::swagger20::DefinitionPath(ref_path.path.clone()))
definitions.get(&*ref_path.path)
.unwrap_or_else(|| panic!("operation {} returns undefined type {kubernetes_group_kind_version:?}", operation.id));
if response_schema.kubernetes_group_kind_versions.contains(kubernetes_group_kind_version) {
return true;
Expand Down Expand Up @@ -1125,7 +1125,7 @@ pub(crate) fn resource_metadata_not_optional(spec: &mut crate::swagger20::Spec)
}

if has_api_version && has_kind && has_optional_metadata {
let metadata = properties.get_mut(&crate::swagger20::PropertyName("metadata".to_owned())).unwrap();
let metadata = properties.get_mut("metadata").unwrap();
metadata.1 = true;
found = true;
}
Expand Down
10 changes: 5 additions & 5 deletions k8s-openapi-codegen/src/fixups/upstream_bugs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub(crate) mod optional_properties {
let definition_path = crate::swagger20::DefinitionPath("io.k8s.api.core.v1.ContainerImage".to_owned());
if let Some(definition) = spec.definitions.get_mut(&definition_path) {
if let crate::swagger20::SchemaKind::Properties(properties) = &mut definition.kind {
if let Some(property) = properties.get_mut(&crate::swagger20::PropertyName("names".to_string())) {
if let Some(property) = properties.get_mut("names") {
if property.1 {
property.1 = false;
return Ok(());
Expand All @@ -116,7 +116,7 @@ pub(crate) mod optional_properties {
let definition_path = crate::swagger20::DefinitionPath("io.k8s.api.events.v1beta1.Event".to_owned());
if let Some(definition) = spec.definitions.get_mut(&definition_path) {
if let crate::swagger20::SchemaKind::Properties(properties) = &mut definition.kind {
if let Some(property) = properties.get_mut(&crate::swagger20::PropertyName("eventTime".to_string())) {
if let Some(property) = properties.get_mut("eventTime") {
if property.1 {
property.1 = false;
return Ok(());
Expand All @@ -133,7 +133,7 @@ pub(crate) mod optional_properties {
let definition_path = crate::swagger20::DefinitionPath("io.k8s.api.events.v1.Event".to_owned());
if let Some(definition) = spec.definitions.get_mut(&definition_path) {
if let crate::swagger20::SchemaKind::Properties(properties) = &mut definition.kind {
if let Some(property) = properties.get_mut(&crate::swagger20::PropertyName("eventTime".to_string())) {
if let Some(property) = properties.get_mut("eventTime") {
if property.1 {
property.1 = false;
return Ok(());
Expand All @@ -155,7 +155,7 @@ pub(crate) mod required_properties {
let definition_path = crate::swagger20::DefinitionPath("io.k8s.api.admissionregistration.v1alpha1.ValidatingAdmissionPolicyBindingList".to_owned());
if let Some(definition) = spec.definitions.get_mut(&definition_path) {
if let crate::swagger20::SchemaKind::Properties(properties) = &mut definition.kind {
if let Some(property) = properties.get_mut(&crate::swagger20::PropertyName("items".to_string())) {
if let Some(property) = properties.get_mut("items") {
if !property.1 {
property.1 = true;
return Ok(());
Expand All @@ -172,7 +172,7 @@ pub(crate) mod required_properties {
let definition_path = crate::swagger20::DefinitionPath("io.k8s.api.admissionregistration.v1alpha1.ValidatingAdmissionPolicyList".to_owned());
if let Some(definition) = spec.definitions.get_mut(&definition_path) {
if let crate::swagger20::SchemaKind::Properties(properties) = &mut definition.kind {
if let Some(property) = properties.get_mut(&crate::swagger20::PropertyName("items".to_string())) {
if let Some(property) = properties.get_mut("items") {
if !property.1 {
property.1 = true;
return Ok(());
Expand Down

0 comments on commit 9c2838e

Please sign in to comment.