Skip to content

Commit

Permalink
fix: fixed unexpected hidden fields indicators in flatten mode (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus authored Aug 13, 2024
1 parent 1f44589 commit 8b16776
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [".", "crate/encstr"]
[workspace.package]
repository = "https://github.com/pamburus/hl"
authors = ["Pavel Ivanov <[email protected]>"]
version = "0.29.7"
version = "0.29.8"
edition = "2021"
license = "MIT"

Expand Down
51 changes: 45 additions & 6 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
});
Expand Down Expand Up @@ -288,6 +288,7 @@ struct FormattingState {
key_prefix: KeyPrefix,
flatten: bool,
empty: bool,
some_nested_fields_hidden: bool,
}

impl FormattingState {
Expand All @@ -297,6 +298,7 @@ impl FormattingState {
key_prefix: KeyPrefix::default(),
flatten,
empty: true,
some_nested_fields_hidden: false,
}
}

Expand Down Expand Up @@ -436,19 +438,20 @@ 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' ');
}
buf.push(b'}');
});
}
fs.some_nested_fields_hidden |= some_fields_hidden;
});
}
RawValue::Array(value) => {
Expand Down Expand Up @@ -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 ... } ... } }");
}
}

0 comments on commit 8b16776

Please sign in to comment.