-
-
Notifications
You must be signed in to change notification settings - Fork 652
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
Inline multiple config files into options sources. #20549
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e2cf754
Inline multiple config files into options sources.
benjyw 02bd9ed
More simplification
benjyw 7c4af56
Merge branch 'main' into option_sources
benjyw 8683a37
Clippy
benjyw 02adf44
Merge branch 'main' into option_sources
benjyw 9d3bebb
Code review feedback
benjyw 5b6e3d3
Fix unwrap
benjyw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
use std::collections::{HashMap, HashSet}; | ||
use std::ffi::OsString; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
|
@@ -216,133 +215,16 @@ fn toml_table_to_dict(table: &Value) -> HashMap<String, Val> { | |
} | ||
} | ||
|
||
#[derive(Clone)] | ||
struct ConfigSource { | ||
#[allow(dead_code)] | ||
path: Option<OsString>, | ||
config: Value, | ||
} | ||
|
||
impl ConfigSource { | ||
fn option_name(id: &OptionId) -> String { | ||
id.name("_", NameTransform::None) | ||
} | ||
|
||
fn get_value(&self, id: &OptionId) -> Option<&Value> { | ||
self.config | ||
.get(id.scope()) | ||
.and_then(|table| table.get(Self::option_name(id))) | ||
} | ||
|
||
fn get_list<T: FromValue>( | ||
&self, | ||
id: &OptionId, | ||
parse_list: fn(&str) -> Result<Vec<ListEdit<T>>, ParseError>, | ||
) -> Result<Vec<ListEdit<T>>, String> { | ||
let mut list_edits = vec![]; | ||
if let Some(table) = self.config.get(id.scope()) { | ||
let option_name = Self::option_name(id); | ||
if let Some(value) = table.get(&option_name) { | ||
match value { | ||
Value::Table(sub_table) => { | ||
if sub_table.is_empty() | ||
|| !sub_table.keys().collect::<HashSet<_>>().is_subset( | ||
&["add".to_owned(), "remove".to_owned()] | ||
.iter() | ||
.collect::<HashSet<_>>(), | ||
) | ||
{ | ||
return Err(format!( | ||
"Expected {option_name} to contain an 'add' element, a 'remove' element or both but found: {sub_table:?}" | ||
)); | ||
} | ||
if let Some(add) = sub_table.get("add") { | ||
list_edits.push(ListEdit { | ||
action: ListEditAction::Add, | ||
items: T::extract_list(&format!("{option_name}.add"), add)?, | ||
}) | ||
} | ||
if let Some(remove) = sub_table.get("remove") { | ||
list_edits.push(ListEdit { | ||
action: ListEditAction::Remove, | ||
items: T::extract_list(&format!("{option_name}.remove"), remove)?, | ||
}) | ||
} | ||
} | ||
Value::String(v) => { | ||
list_edits.extend(parse_list(v).map_err(|e| e.render(option_name))?); | ||
} | ||
value => list_edits.push(ListEdit { | ||
action: ListEditAction::Replace, | ||
items: T::extract_list(&option_name, value)?, | ||
}), | ||
} | ||
} | ||
} | ||
Ok(list_edits) | ||
} | ||
|
||
fn get_dict(&self, id: &OptionId) -> Result<Option<DictEdit>, String> { | ||
if let Some(table) = self.config.get(id.scope()) { | ||
let option_name = Self::option_name(id); | ||
if let Some(value) = table.get(&option_name) { | ||
match value { | ||
Value::Table(sub_table) => { | ||
if let Some(add) = sub_table.get("add") { | ||
if sub_table.len() == 1 && add.is_table() { | ||
return Ok(Some(DictEdit { | ||
action: DictEditAction::Add, | ||
items: toml_table_to_dict(add), | ||
})); | ||
} | ||
} | ||
return Ok(Some(DictEdit { | ||
action: DictEditAction::Replace, | ||
items: toml_table_to_dict(value), | ||
})); | ||
} | ||
Value::String(v) => { | ||
return Ok(Some(parse_dict(v).map_err(|e| e.render(option_name))?)); | ||
} | ||
_ => { | ||
return Err(format!( | ||
"Expected {option_name} to be a toml table or Python dict, but given {value}." | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct Config { | ||
sources: Vec<ConfigSource>, | ||
value: Value, | ||
} | ||
|
||
impl Config { | ||
pub(crate) fn parse<P: AsRef<Path>>( | ||
files: &[P], | ||
seed_values: &InterpolationMap, | ||
) -> Result<Config, String> { | ||
let mut sources = vec![]; | ||
for file in files { | ||
sources.push(Self::parse_source(file, seed_values)?); | ||
} | ||
Ok(Config { sources }) | ||
} | ||
|
||
pub(crate) fn merge(self, other: Config) -> Config { | ||
Config { | ||
sources: self.sources.into_iter().chain(other.sources).collect(), | ||
} | ||
} | ||
|
||
fn parse_source<P: AsRef<Path>>( | ||
file: P, | ||
seed_values: &InterpolationMap, | ||
) -> Result<ConfigSource, String> { | ||
) -> Result<Config, String> { | ||
let config_contents = fs::read_to_string(&file).map_err(|e| { | ||
format!( | ||
"Failed to read config file {}: {}", | ||
|
@@ -419,29 +301,100 @@ impl Config { | |
}; | ||
|
||
let new_table = Table::from_iter(new_sections?); | ||
Ok(ConfigSource { | ||
path: Some(file.as_ref().as_os_str().into()), | ||
config: Value::Table(new_table), | ||
Ok(Self { | ||
value: Value::Table(new_table), | ||
}) | ||
} | ||
|
||
fn option_name(id: &OptionId) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved from ConfigSource |
||
id.name("_", NameTransform::None) | ||
} | ||
|
||
fn get_value(&self, id: &OptionId) -> Option<&Value> { | ||
self.sources | ||
.iter() | ||
.rev() | ||
.find_map(|source| source.get_value(id)) | ||
self.value | ||
.get(id.scope()) | ||
.and_then(|table| table.get(Self::option_name(id))) | ||
} | ||
|
||
fn get_list<T: FromValue>( | ||
&self, | ||
id: &OptionId, | ||
parse_list: fn(&str) -> Result<Vec<ListEdit<T>>, ParseError>, | ||
) -> Result<Option<Vec<ListEdit<T>>>, String> { | ||
let mut edits: Vec<ListEdit<T>> = vec![]; | ||
for source in self.sources.iter() { | ||
edits.append(&mut source.get_list(id, parse_list)?); | ||
let mut list_edits = vec![]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved from ConfigSource |
||
if let Some(table) = self.value.get(id.scope()) { | ||
let option_name = Self::option_name(id); | ||
if let Some(value) = table.get(&option_name) { | ||
match value { | ||
Value::Table(sub_table) => { | ||
if sub_table.is_empty() | ||
|| !sub_table.keys().collect::<HashSet<_>>().is_subset( | ||
&["add".to_owned(), "remove".to_owned()] | ||
.iter() | ||
.collect::<HashSet<_>>(), | ||
) | ||
{ | ||
return Err(format!( | ||
"Expected {option_name} to contain an 'add' element, a 'remove' element or both but found: {sub_table:?}" | ||
)); | ||
} | ||
if let Some(add) = sub_table.get("add") { | ||
list_edits.push(ListEdit { | ||
action: ListEditAction::Add, | ||
items: T::extract_list(&format!("{option_name}.add"), add)?, | ||
}) | ||
} | ||
if let Some(remove) = sub_table.get("remove") { | ||
list_edits.push(ListEdit { | ||
action: ListEditAction::Remove, | ||
items: T::extract_list(&format!("{option_name}.remove"), remove)?, | ||
}) | ||
} | ||
} | ||
Value::String(v) => { | ||
list_edits.extend(parse_list(v).map_err(|e| e.render(option_name))?); | ||
} | ||
value => list_edits.push(ListEdit { | ||
action: ListEditAction::Replace, | ||
items: T::extract_list(&option_name, value)?, | ||
}), | ||
} | ||
} | ||
} | ||
Ok(Some(list_edits)) | ||
} | ||
|
||
fn get_dict(&self, id: &OptionId) -> Result<Option<DictEdit>, String> { | ||
if let Some(table) = self.value.get(id.scope()) { | ||
let option_name = Self::option_name(id); | ||
if let Some(value) = table.get(&option_name) { | ||
match value { | ||
Value::Table(sub_table) => { | ||
if let Some(add) = sub_table.get("add") { | ||
if sub_table.len() == 1 && add.is_table() { | ||
return Ok(Some(DictEdit { | ||
action: DictEditAction::Add, | ||
items: toml_table_to_dict(add), | ||
})); | ||
} | ||
} | ||
return Ok(Some(DictEdit { | ||
action: DictEditAction::Replace, | ||
items: toml_table_to_dict(value), | ||
})); | ||
} | ||
Value::String(v) => { | ||
return Ok(Some(parse_dict(v).map_err(|e| e.render(option_name))?)); | ||
} | ||
_ => { | ||
return Err(format!( | ||
"Expected {option_name} to be a toml table or Python dict, but given {value}." | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
Ok(Some(edits)) | ||
Ok(None) | ||
} | ||
} | ||
|
||
|
@@ -482,13 +435,7 @@ impl OptionsSource for Config { | |
self.get_list(id, parse_string_list) | ||
} | ||
|
||
fn get_dict(&self, id: &OptionId) -> Result<Option<Vec<DictEdit>>, String> { | ||
let mut edits = vec![]; | ||
for source in self.sources.iter() { | ||
if let Some(edit) = source.get_dict(id)? { | ||
edits.push(edit); | ||
} | ||
} | ||
Ok(if edits.is_empty() { None } else { Some(edits) }) | ||
fn get_dict(&self, id: &OptionId) -> Result<Option<DictEdit>, String> { | ||
self.get_dict(id) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We get rid of ConfigSource entirely, and fold its functionality directly into
Config
below, since they are now one-to-one.