From 8b16776c6fb4e57b92e0556dbafe51aa01cbdab9 Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Tue, 13 Aug 2024 08:30:31 +0200 Subject: [PATCH] fix: fixed unexpected hidden fields indicators in flatten mode (#380) --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/formatting.rs | 51 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1df10c70..32d412bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -815,7 +815,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hl" -version = "0.29.7" +version = "0.29.8" dependencies = [ "bincode", "byte-strings", diff --git a/Cargo.toml b/Cargo.toml index a40e8238..084814b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = [".", "crate/encstr"] [workspace.package] repository = "https://github.com/pamburus/hl" authors = ["Pavel Ivanov "] -version = "0.29.7" +version = "0.29.8" edition = "2021" license = "MIT" diff --git a/src/formatting.rs b/src/formatting.rs index 0c7bda9b..d9099f5e 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -202,7 +202,7 @@ impl RecordFormatter { some_fields_hidden |= !self.format_field(s, k, *v, &mut fs, Some(&self.fields)); } } - if some_fields_hidden { + if some_fields_hidden || (fs.some_nested_fields_hidden && fs.flatten) { s.element(Element::Ellipsis, |s| { s.batch(|buf| buf.extend_from_slice(self.cfg.punctuation.hidden_fields_indicator.as_bytes())) }); @@ -288,6 +288,7 @@ struct FormattingState { key_prefix: KeyPrefix, flatten: bool, empty: bool, + some_nested_fields_hidden: bool, } impl FormattingState { @@ -297,6 +298,7 @@ impl FormattingState { key_prefix: KeyPrefix::default(), flatten, empty: true, + some_nested_fields_hidden: false, } } @@ -436,12 +438,12 @@ impl<'a> FieldFormatter<'a> { for (k, v) in item.fields.iter() { some_fields_hidden |= !self.format(s, k, *v, fs, filter, setting); } - if some_fields_hidden { - s.element(Element::Ellipsis, |s| { - s.batch(|buf| buf.extend(self.rf.cfg.punctuation.hidden_fields_indicator.as_bytes())) - }); - } if !fs.flatten { + if some_fields_hidden { + s.element(Element::Ellipsis, |s| { + s.batch(|buf| buf.extend(self.rf.cfg.punctuation.hidden_fields_indicator.as_bytes())) + }); + } s.batch(|buf| { if item.fields.len() != 0 { buf.push(b' '); @@ -449,6 +451,7 @@ impl<'a> FieldFormatter<'a> { buf.push(b'}'); }); } + fs.some_nested_fields_hidden |= some_fields_hidden; }); } RawValue::Array(value) => { @@ -1382,4 +1385,40 @@ mod tests { let result = format_no_color(&rec); assert_eq!(&result, r#""#, "{}", result); } + + #[test] + fn test_nested_hidden_fields_flatten() { + let val = json_raw_value(r#"{"b":{"c":{"d":1,"e":2},"f":3}}"#); + let rec = Record::from_fields(&[("a", RawObject::Json(&val).into())]); + let mut fields = IncludeExcludeKeyFilter::default(); + let b = fields.entry("a").entry("b"); + b.exclude(); + b.entry("c").entry("d").include(); + let formatter = RecordFormatter { + flatten: true, + theme: Default::default(), + fields: Arc::new(fields), + ..formatter() + }; + + assert_eq!(&formatter.format_to_string(&rec), "a.b.c.d=1 ..."); + } + + #[test] + fn test_nested_hidden_fields_no_flatten() { + let val = json_raw_value(r#"{"b":{"c":{"d":1,"e":2},"f":3}}"#); + let rec = Record::from_fields(&[("a", RawObject::Json(&val).into())]); + let mut fields = IncludeExcludeKeyFilter::default(); + let b = fields.entry("a").entry("b"); + b.exclude(); + b.entry("c").entry("d").include(); + let formatter = RecordFormatter { + flatten: false, + theme: Default::default(), + fields: Arc::new(fields), + ..formatter() + }; + + assert_eq!(&formatter.format_to_string(&rec), "a={ b={ c={ d=1 ... } ... } }"); + } }