Skip to content

Commit a41d2b6

Browse files
committed
fix: preserve property order
1 parent bf09321 commit a41d2b6

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ serde = { version = "1", features = ["derive"] }
2121
toml = "0.9"
2222
dirs = "6"
2323
chrono = "0.4.41"
24+
indexmap = "2.11.4"
2425

2526
[dependencies.clap]
2627
version = "4"

sample.json.array.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message": "test", "x": [ "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"]}

src/config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::collections::BTreeMap;
2-
use std::{fs, path::PathBuf};
1+
use std::{collections::BTreeMap, fs, path::PathBuf};
32

43
use serde::{Deserialize, Serialize};
54

src/log.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::log_settings::LogSettings;
22
use crate::time::try_convert_timestamp_to_readable;
33
use handlebars::Handlebars;
4+
use indexmap::IndexMap;
45
use serde_json::{Map, Value};
56
use std::borrow::ToOwned;
67
use std::collections::BTreeMap;
@@ -58,8 +59,8 @@ pub fn print_log_line(
5859
}
5960
}
6061

61-
fn flatten_json(log_entry: &Map<String, Value>, prefix: &str) -> BTreeMap<String, String> {
62-
let mut flattened_json: BTreeMap<String, String> = BTreeMap::new();
62+
fn flatten_json(log_entry: &Map<String, Value>, prefix: &str) -> IndexMap<String, String> {
63+
let mut flattened_json: IndexMap<String, String> = IndexMap::new();
6364
for (key, value) in log_entry {
6465
match value {
6566
Value::String(string_value) => {
@@ -97,7 +98,7 @@ fn flatten_json(log_entry: &Map<String, Value>, prefix: &str) -> BTreeMap<String
9798
flattened_json
9899
}
99100

100-
fn flatten_array(key: &str, prefix: &str, array_values: &[Value], flattened_json: &mut BTreeMap<String, String>) {
101+
fn flatten_array(key: &str, prefix: &str, array_values: &[Value], flattened_json: &mut IndexMap<String, String>) {
101102
for (index, array_value) in array_values.iter().enumerate() {
102103
let key = format!("{}[{}]", key, index + 1); // lua tables indexes start with 1
103104

@@ -113,17 +114,17 @@ fn flatten_array(key: &str, prefix: &str, array_values: &[Value], flattened_json
113114
}
114115
}
115116

116-
fn get_string_value(value: &BTreeMap<String, String>, keys: &[String]) -> Option<String> {
117+
fn get_string_value(value: &IndexMap<String, String>, keys: &[String]) -> Option<String> {
117118
keys
118119
.iter()
119120
.fold(None::<String>, |maybe_match, key| maybe_match.or_else(|| value.get(key).map(ToOwned::to_owned)))
120121
}
121122

122-
fn get_string_value_or_default(value: &BTreeMap<String, String>, keys: &[String], default: &str) -> String {
123+
fn get_string_value_or_default(value: &IndexMap<String, String>, keys: &[String], default: &str) -> String {
123124
get_string_value(value, keys).unwrap_or_else(|| default.to_string())
124125
}
125126

126-
fn write_additional_values(out: &mut dyn Write, log_entry: &BTreeMap<String, String>, additional_values: &[String], handlebars: &Handlebars<'static>) {
127+
fn write_additional_values(out: &mut dyn Write, log_entry: &IndexMap<String, String>, additional_values: &[String], handlebars: &Handlebars<'static>) {
127128
for additional_value_prefix in additional_values {
128129
for additional_value in log_entry
129130
.keys()

src/log_settings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::{config::Config, substitution::Substitution};
21
use std::collections::BTreeMap;
32

3+
use crate::{config::Config, substitution::Substitution};
4+
45
pub struct LogSettings {
56
pub message_keys: Vec<String>,
67
pub time_keys: Vec<String>,

0 commit comments

Comments
 (0)