Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Preserve schema variables' ordering when parsing from the input YAML config file. #22

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ edition = "2021"

[dependencies]
clap = "4.5.23"
indexmap = { version = "2.7.0", features = ["serde"] }
regex-syntax = "0.8.5"
serde_yaml = "0.9.34"
2 changes: 1 addition & 1 deletion examples/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ variables:
float: '\-{0,1}[0-9]+\.[0-9]+'
hex: '0x(((\d|[a-f])+)|((\d|[A-F])+))'
loglevel: '(INFO)|(DEBUG)|(WARN)|(ERROR)|(TRACE)|(FATAL)'
thread_identifier: '\[(\w)+\]'
field_identifier: '\[(\w)+\]'
path: '(/(\w|\.|\-|\*)+)+(/)*'
26 changes: 21 additions & 5 deletions src/parser/schema_parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::error_handling::Error::{
};
use crate::error_handling::Result;
use crate::parser::regex_parser::parser::RegexParser;
use indexmap::IndexMap;
use regex_syntax::ast::Ast;
use serde_yaml::Value;
use std::collections::{HashMap, HashSet};
use std::io::Read;
use std::rc::Rc;

Expand Down Expand Up @@ -105,20 +105,20 @@ impl SchemaConfig {
}

fn get_key_value<'a>(
kv_map: &'a HashMap<String, Value>,
kv_map: &'a IndexMap<String, Value>,
key: &'static str,
) -> Result<&'a Value> {
kv_map.get(key).ok_or_else(|| MissingSchemaKey(key))
}

fn load_kv_pairs_from_yaml_content(
yaml_content: &str,
) -> serde_yaml::Result<HashMap<String, Value>> {
let kv_map_result: HashMap<String, Value> = serde_yaml::from_str(&yaml_content)?;
) -> serde_yaml::Result<IndexMap<String, Value>> {
let kv_map_result: IndexMap<String, Value> = serde_yaml::from_str(&yaml_content)?;
Ok(kv_map_result)
}

fn load_from_kv_pairs(kv_pairs: HashMap<String, Value>) -> Result<Self> {
fn load_from_kv_pairs(kv_pairs: IndexMap<String, Value>) -> Result<Self> {
// Handle timestamps
let mut ts_schemas: Vec<TimestampSchema> = Vec::new();
let timestamps = Self::get_key_value(&kv_pairs, Self::TIMESTAMP_KEY)?;
Expand Down Expand Up @@ -177,6 +177,7 @@ impl SchemaConfig {
#[cfg(test)]
mod tests {
use super::*;
use clap::builder::Str;

#[test]
fn test_read_example_schema_file() -> Result<()> {
Expand All @@ -189,6 +190,21 @@ mod tests {
assert_eq!(parsed_schema.get_ts_schemas().len(), 5);
assert_eq!(parsed_schema.get_var_schemas().len(), 6);

let expected_var_names: Vec<String> = vec![
"int".to_string(),
"float".to_string(),
"hex".to_string(),
"loglevel".to_string(),
"field_identifier".to_string(),
"path".to_string(),
];
let actual_var_names: Vec<String> = parsed_schema
.get_var_schemas()
.iter()
.map(|v| v.get_name().to_string())
.collect();
assert_eq!(expected_var_names, actual_var_names);

let delimiters: Vec<char> = vec![' ', '\t', '\n', '\r', ':', ',', '!', ';', '%'];
for delimiter in delimiters {
assert!(parsed_schema.has_delimiter(delimiter));
Expand Down
Loading