Skip to content

Commit

Permalink
Fix request default fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tinrab committed Dec 22, 2023
1 parent 6144e15 commit ee8eaab
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 65 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni"
version = "0.1.47"
version = "0.1.48"
authors = ["Tin Rabzelj <[email protected]>"]
description = "Utility Library for Rust"
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -38,9 +38,9 @@ tokio = ["bomboni_common/tokio"]
tonic = ["bomboni_proto/tonic", "bomboni_request/tonic"]

[dependencies]
bomboni_common = { path = "bomboni_common", version = "0.1.47" }
bomboni_common = { path = "bomboni_common", version = "0.1.48" }

bomboni_prost = { path = "bomboni_prost", version = "0.1.47", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.47", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.47", optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.47", optional = true }
bomboni_prost = { path = "bomboni_prost", version = "0.1.48", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.48", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.48", optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.48", optional = true }
2 changes: 1 addition & 1 deletion bomboni_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_common"
version = "0.1.47"
version = "0.1.48"
authors = ["Tin Rabzelj <[email protected]>"]
description = "Common things for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
2 changes: 1 addition & 1 deletion bomboni_prost/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_prost"
version = "0.1.47"
version = "0.1.48"
authors = ["Tin Rabzelj <[email protected]>"]
description = "Utilities for working with prost. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
4 changes: 2 additions & 2 deletions bomboni_proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_proto"
version = "0.1.47"
version = "0.1.48"
authors = ["Tin Rabzelj <[email protected]>"]
description = "Utilities for working with Protobuf/gRPC. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -36,5 +36,5 @@ serde_json = { version = "1.0.108", optional = true }
serde_json = "1.0.108"

[build-dependencies]
bomboni_prost = { path = "../bomboni_prost", version = "0.1.47" }
bomboni_prost = { path = "../bomboni_prost", version = "0.1.48" }
prost-build = "0.12.3"
8 changes: 4 additions & 4 deletions bomboni_request/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_request"
version = "0.1.47"
version = "0.1.48"
authors = ["Tin Rabzelj <[email protected]>"]
description = "Utilities for working with API requests. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -19,8 +19,8 @@ testing = []
tonic = ["bomboni_proto/tonic", "dep:tonic"]

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.47" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.47" }
bomboni_common = { path = "../bomboni_common", version = "0.1.48" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.48" }
thiserror = "1.0.51"
itertools = "0.12.0"
time = { version = "0.3.31", features = ["formatting", "parsing"] }
Expand All @@ -35,7 +35,7 @@ rand = "0.8.5"
regex = "1.10.2"

tonic = { version = "0.10.2", optional = true }
bomboni_request_derive = { path = "../bomboni_request_derive", version = "0.1.47", optional = true }
bomboni_request_derive = { path = "../bomboni_request_derive", version = "0.1.48", optional = true }

[dev-dependencies]
serde = { version = "1.0.193", features = ["derive"] }
Expand Down
56 changes: 55 additions & 1 deletion bomboni_request/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mod tests {
required_string_optional: String,
nested: ParsedNestedItem,
optional_nested: Option<ParsedNestedItem>,
#[parse(default = NestedItem::default())]
#[parse(default = ParsedNestedItem::default())]
default_nested: ParsedNestedItem,
#[parse(default)]
default_default_nested: ParsedNestedItem,
Expand Down Expand Up @@ -1878,4 +1878,58 @@ mod tests {
)
));
}

#[test]
fn custom_default_field() {
#[derive(Debug, Clone, PartialEq, Default)]
struct Item {
part: Option<Part>,
}

#[derive(Debug, Clone, PartialEq, Default)]
struct Part {
value: i32,
}

#[derive(Debug, Clone, PartialEq, Parse)]
#[parse(source = Item, write)]
struct ParsedItem {
#[parse(default = get_default_part())]
part: ParsedPart,
}

#[derive(Debug, Clone, PartialEq, Parse)]
#[parse(source = Part, write)]
struct ParsedPart {
value: i32,
}

fn get_default_part() -> ParsedPart {
ParsedPart { value: 42 }
}

assert_eq!(
ParsedItem::parse(Item {
part: Some(Part { value: 1337 }),
})
.unwrap(),
ParsedItem {
part: ParsedPart { value: 1337 },
}
);
assert_eq!(
ParsedItem::parse(Item { part: None }).unwrap(),
ParsedItem {
part: ParsedPart { value: 42 },
}
);
assert_eq!(
Item::from(ParsedItem {
part: ParsedPart { value: 1337 },
}),
Item {
part: Some(Part { value: 1337 }),
}
);
}
}
2 changes: 1 addition & 1 deletion bomboni_request_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_request_derive"
version = "0.1.47"
version = "0.1.48"
authors = ["Tin Rabzelj <[email protected]>"]
description = "Provides derive implementations for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
31 changes: 16 additions & 15 deletions bomboni_request_derive/src/parse/message/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,6 @@ fn expand_parse_field(options: &ParseOptions, field: &ParseField) -> syn::Result
}
}

let default_expr = if let Some(default) = field.default.as_ref() {
quote! { #default }
} else {
quote! { Default::default() }
};

// Source field for nested messages is always wrapped in `Option`
let source_option = field.source_option
|| is_option
Expand Down Expand Up @@ -537,23 +531,30 @@ fn expand_parse_field(options: &ParseOptions, field: &ParseField) -> syn::Result
});
}
} else {
if source_option {
if field.default.is_some() {
parse.extend(quote! {
let target = target.unwrap_or_else(|| #default_expr);
});
parse.extend(if source_option {
if let Some(default) = field.default.as_ref() {
quote! {
let target = if let Some(target) = target {
#parse_source
target
} else {
#default
};
}
} else {
parse.extend(quote! {
quote! {
let target = target.ok_or_else(|| {
RequestError::field(
#field_name,
CommonError::RequiredFieldMissing,
)
})?;
});
#parse_source
}
}
}
parse.extend(parse_source);
} else {
parse_source
});
}

if is_box {
Expand Down
14 changes: 6 additions & 8 deletions bomboni_request_derive/src/parse/message/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,6 @@ fn expand_write_field(options: &ParseOptions, field: &ParseField) -> TokenStream
let source = value.#target_ident;
};

let default_expr = if let Some(default) = field.default.as_ref() {
quote! { #default }
} else {
quote! { Default::default() }
};

let source_option = field.source_option
|| is_option
|| (is_nested
Expand All @@ -202,8 +196,12 @@ fn expand_write_field(options: &ParseOptions, field: &ParseField) -> TokenStream
}
} else {
quote! {
let source = source.unwrap_or_else(|| #default_expr);
#write_target
let source = if let Some(source) = source {
#write_target
source
} else {
Default::default()
};
}
});
} else {
Expand Down
31 changes: 16 additions & 15 deletions bomboni_request_derive/src/parse/oneof/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,6 @@ fn expand_parse_variant(
}
}

let default_expr = if let Some(default) = variant.default.as_ref() {
quote! { #default }
} else {
quote! { Default::default() }
};

let source_option = variant.source_option || is_option;

if is_option {
Expand Down Expand Up @@ -386,23 +380,30 @@ fn expand_parse_variant(
});
}
} else {
if source_option {
if variant.default.is_some() {
parse.extend(quote! {
let target = target.unwrap_or_else(|| #default_expr);
});
parse.extend(if source_option {
if let Some(default) = variant.default.as_ref() {
quote! {
let target = if let Some(target) = target {
#parse_source
target
} else {
#default
};
}
} else {
parse.extend(quote! {
quote! {
let target = target.ok_or_else(|| {
RequestError::field(
variant_name,
CommonError::RequiredFieldMissing,
)
})?;
});
#parse_source
}
}
}
parse.extend(parse_source);
} else {
parse_source
});
}

if is_box {
Expand Down
14 changes: 6 additions & 8 deletions bomboni_request_derive/src/parse/oneof/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@ fn expand_write_variant(options: &ParseOptions, variant: &ParseVariant) -> Token
let source = value;
};

let default_expr = if let Some(default) = variant.default.as_ref() {
quote! { #default }
} else {
quote! { Default::default() }
};

let source_option = variant.source_option || is_option;

if is_option {
Expand All @@ -243,8 +237,12 @@ fn expand_write_variant(options: &ParseOptions, variant: &ParseVariant) -> Token
}
} else {
quote! {
let source = source.unwrap_or_else(|| #default_expr);
#write_target
let source = if let Some(source) = source {
#write_target
source
} else {
Default::default()
};
}
});
} else {
Expand Down
6 changes: 3 additions & 3 deletions bomboni_template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_template"
version = "0.1.47"
version = "0.1.48"
authors = ["Tin Rabzelj <[email protected]>"]
description = "Utilities for working Handlebars templates. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -17,8 +17,8 @@ path = "src/lib.rs"
testing = []

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.47" }
bomboni_proto = { version = "0.1.47", path = "../bomboni_proto", features = [
bomboni_common = { path = "../bomboni_common", version = "0.1.48" }
bomboni_proto = { version = "0.1.48", path = "../bomboni_proto", features = [
"json",
] }
thiserror = "1.0.51"
Expand Down

0 comments on commit ee8eaab

Please sign in to comment.