Skip to content

Commit 6046d0a

Browse files
authored
chore: impl Display for meta-service txn struct with crate display-more (#18289)
1 parent a0f9327 commit 6046d0a

File tree

8 files changed

+19
-101
lines changed

8 files changed

+19
-101
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ defer = "0.2"
298298
deltalake = "0.26"
299299
derive-visitor = { version = "0.4.0", features = ["std-types-drive"] }
300300
derive_more = { version = "1.0.0", features = ["full"] }
301-
display-more = { version = "0.1.0" }
301+
display-more = { version = "0.2.0" }
302302
divan = "0.1.21"
303303
dtparse = { git = "https://github.com/datafuse-extras/dtparse.git", rev = "30e28ca" }
304304
dyn-clone = "1.0.9"
@@ -652,7 +652,7 @@ async-recursion = { git = "https://github.com/datafuse-extras/async-recursion.gi
652652
backtrace = { git = "https://github.com/rust-lang/backtrace-rs.git", rev = "72265be" }
653653
color-eyre = { git = "https://github.com/eyre-rs/eyre.git", rev = "e5d92c3" }
654654
deltalake = { git = "https://github.com/delta-io/delta-rs", rev = "9954bff" }
655-
display-more = { git = "https://github.com/databendlabs/display-more", tag = "v0.1.3" }
655+
display-more = { git = "https://github.com/databendlabs/display-more", tag = "v0.2.0" }
656656
map-api = { git = "https://github.com/databendlabs/map-api", tag = "v0.2.3" }
657657
openai_api_rust = { git = "https://github.com/datafuse-extras/openai-api", rev = "819a0ed" }
658658
openraft = { git = "https://github.com/databendlabs/openraft", tag = "v0.10.0-alpha.9" }

src/meta/api/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub async fn send_txn(
248248
debug!("send txn: {}", txn_req);
249249
let tx_reply = kv_api.transaction(txn_req).await?;
250250
let (succ, responses) = unpack_txn_reply(tx_reply);
251-
debug!("txn success: {}: {}", succ, responses.display_n::<20>());
251+
debug!("txn success: {}: {}", succ, responses.display_n(20));
252252
Ok((succ, responses))
253253
}
254254

src/meta/app/src/principal/procedure.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl Display for ProcedureMeta {
7373
f,
7474
"Lanuage: {:?}, args {} return_type: {}, CreatedOn: {:?}, Script: {}, Comment: {:?}",
7575
self.procedure_language,
76-
self.arg_names.display_n::<1000>(),
77-
self.return_types.display_n::<1000>(),
76+
self.arg_names.display_n(1000),
77+
self.return_types.display_n(1000),
7878
self.created_on,
7979
self.script,
8080
self.comment

src/meta/kvapi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ anyhow = { workspace = true }
1313
async-trait = { workspace = true }
1414
databend-common-meta-app-types = { workspace = true }
1515
databend-common-meta-types = { workspace = true }
16+
display-more = { workspace = true }
1617
futures-util = { workspace = true }
1718
log = { workspace = true }
1819
serde = { workspace = true }

src/meta/kvapi/src/kvapi/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::fmt::Formatter;
1717

1818
use databend_common_meta_types::seq_value::SeqV;
1919
use databend_common_meta_types::Change;
20-
use databend_common_meta_types::VecDisplay;
20+
use display_more::display_slice::DisplaySliceExt;
2121

2222
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
2323
pub struct GetKVReq {
@@ -47,7 +47,7 @@ impl MGetKVReq {
4747

4848
impl fmt::Display for MGetKVReq {
4949
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
50-
write!(f, "{}", VecDisplay::new_at_most(&self.keys, 5))
50+
write!(f, "{}", self.keys.display())
5151
}
5252
}
5353

src/meta/types/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ pub use match_seq::MatchSeqExt;
8989
pub use node::Node;
9090
pub use operation::MetaId;
9191
pub use operation::Operation;
92-
pub use proto_display::VecDisplay;
9392
pub use protobuf::txn_condition;
9493
pub use protobuf::txn_condition::ConditionResult;
9594
pub use protobuf::txn_op;

src/meta/types/src/proto_display/mod.rs

Lines changed: 8 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -48,64 +48,6 @@ use crate::TxnPutResponse;
4848
use crate::TxnReply;
4949
use crate::TxnRequest;
5050

51-
struct OptionDisplay<'a, T: Display> {
52-
t: &'a Option<T>,
53-
}
54-
55-
impl<T: Display> Display for OptionDisplay<'_, T> {
56-
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
57-
match &self.t {
58-
None => {
59-
write!(f, "None")
60-
}
61-
Some(x) => x.fmt(f),
62-
}
63-
}
64-
}
65-
66-
pub struct VecDisplay<'a, T: Display> {
67-
at_most: Option<usize>,
68-
vec: &'a Vec<T>,
69-
}
70-
71-
impl<'a, T> VecDisplay<'a, T>
72-
where T: Display
73-
{
74-
pub fn new(vec: &'a Vec<T>) -> Self {
75-
VecDisplay { at_most: None, vec }
76-
}
77-
78-
pub fn new_at_most(vec: &'a Vec<T>, at_most: usize) -> Self {
79-
VecDisplay {
80-
at_most: Some(at_most),
81-
vec,
82-
}
83-
}
84-
}
85-
86-
impl<T: Display> Display for VecDisplay<'_, T> {
87-
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
88-
write!(f, "[")?;
89-
90-
for (i, t) in self.vec.iter().enumerate() {
91-
if i > 0 {
92-
write!(f, ",")?;
93-
}
94-
95-
if let Some(at_most) = self.at_most {
96-
if i >= at_most {
97-
write!(f, "...")?;
98-
break;
99-
}
100-
}
101-
102-
write!(f, "{}", t)?;
103-
}
104-
105-
write!(f, "]")
106-
}
107-
}
108-
10951
impl Display for TxnRequest {
11052
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
11153
write!(f, "TxnRequest{{",)?;
@@ -114,9 +56,9 @@ impl Display for TxnRequest {
11456
write!(f, "{{ {} }}, ", op)?;
11557
}
11658

117-
write!(f, "if:{} ", VecDisplay::new_at_most(&self.condition, 10),)?;
118-
write!(f, "then:{} ", VecDisplay::new_at_most(&self.if_then, 10),)?;
119-
write!(f, "else:{}", VecDisplay::new_at_most(&self.else_then, 10),)?;
59+
write!(f, "if:{} ", self.condition.display_n(10),)?;
60+
write!(f, "then:{} ", self.if_then.display_n(10),)?;
61+
write!(f, "else:{}", self.else_then.display_n(10),)?;
12062

12163
write!(f, "}}",)
12264
}
@@ -126,9 +68,7 @@ impl Display for TxnCondition {
12668
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
12769
let expect: ConditionResult = FromPrimitive::from_i32(self.expected).unwrap();
12870

129-
write!(f, "{} {} {}", self.key, expect, OptionDisplay {
130-
t: &self.target
131-
})
71+
write!(f, "{} {} {}", self.key, expect, self.target.display())
13272
}
13373
}
13474

@@ -148,7 +88,7 @@ impl Display for ConditionResult {
14888

14989
impl Display for TxnOp {
15090
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
151-
write!(f, "{}", OptionDisplay { t: &self.request })
91+
write!(f, "{}", self.request.display())
15292
}
15393
}
15494

@@ -231,14 +171,14 @@ impl Display for TxnReply {
231171
f,
232172
"TxnReply{{ success: {}, responses: {}}}",
233173
self.success,
234-
VecDisplay::new_at_most(&self.responses, 5),
174+
self.responses.display()
235175
)
236176
}
237177
}
238178

239179
impl Display for TxnOpResponse {
240180
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
241-
write!(f, "TxnOpResponse: {}", OptionDisplay { t: &self.response })
181+
write!(f, "TxnOpResponse: {}", self.response.display())
242182
}
243183
}
244184

@@ -342,7 +282,7 @@ impl Display for ConditionalOperation {
342282
f,
343283
"if:({}) then:{}",
344284
self.predicate.display(),
345-
self.operations.display_n::<10>()
285+
self.operations.display_n(10)
346286
)
347287
}
348288
}
@@ -380,29 +320,6 @@ mod tests {
380320
use crate::protobuf::TxnCondition;
381321
use crate::TxnOp;
382322

383-
#[test]
384-
fn test_vec_display() {
385-
assert_eq!(
386-
format!("{}", super::VecDisplay::new(&vec![1, 2, 3])),
387-
"[1,2,3]"
388-
);
389-
390-
assert_eq!(
391-
format!("{}", super::VecDisplay::new_at_most(&vec![1, 2, 3], 3)),
392-
"[1,2,3]"
393-
);
394-
395-
assert_eq!(
396-
format!("{}", super::VecDisplay::new_at_most(&vec![1, 2, 3, 4], 3)),
397-
"[1,2,3,...]"
398-
);
399-
400-
assert_eq!(
401-
format!("{}", super::VecDisplay::new_at_most(&vec![1, 2, 3, 4], 0)),
402-
"[...]"
403-
);
404-
}
405-
406323
#[test]
407324
fn test_tx_display_with_bool_expression() {
408325
let expr = BooleanExpression::from_conditions_and([

0 commit comments

Comments
 (0)