Skip to content

Commit

Permalink
feat(config): support for arrays, too
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 7, 2023
1 parent 49c2190 commit ef46e5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
19 changes: 14 additions & 5 deletions crates/oro-config/src/kdl_source.rs
Expand Up @@ -66,21 +66,29 @@ fn array_kind(value: impl Iterator<Item = Value>) -> ValueKind {
fn node_value(node: &KdlNode) -> Value {
let mut entries = node.entries().iter().filter(|e| e.name().is_none());
let len = entries.clone().count();
// foo 1 => { foo: 1 }
//
// Technically, this could semantically be an array as well, but we choose
// to treat single-entries as single values.
if len == 1 {
// foo 1 => { foo: 1 }
//
// Technically, this could semantically be an array as well, but we choose
// to treat single-entries as single values.
Value::new(
None,
value_kind(entries.next().expect("checked length already").value()),
)
// foo 1 2 3 => { foo: [1, 2, 3] }
} else if len > 1 {
// foo 1 2 3 => { foo: [1, 2, 3] }
Value::new(
None,
array_kind(entries.map(|e| Value::new(None, value_kind(e.value())))),
)
} else if !node.entries().is_empty() {
// foo bar=1 => { foo: { bar: 1 } }
Value::new(None, map_kind(node.entries().iter().map(|e| {
(
e.name().expect("these should all have names").value().to_string(),
Value::new(None, value_kind(e.value())),
)
})))
} else if let Some(children) = node.children() {
let dash_children = children
.nodes()
Expand Down Expand Up @@ -114,6 +122,7 @@ fn node_value(node: &KdlNode) -> Value {
)
}
} else {
// Probably unreachable, but just in case...
Value::new(None, ValueKind::Nil)
}
}
10 changes: 9 additions & 1 deletion crates/oro-config/src/lib.rs
Expand Up @@ -70,7 +70,6 @@ impl OroConfigLayerExt for Command {
args.push(OsString::from(value));
}
} else if let Ok(value) = config.get_table(&opt) {
println!("got table: {:?}", value);
if !args.contains(&OsString::from(format!("--no-{}", opt))) {
args.push(OsString::from(format!("--{}", opt)));
let joined = value
Expand All @@ -80,6 +79,15 @@ impl OroConfigLayerExt for Command {
.join(",");
args.push(OsString::from(joined));
}
} else if let Ok(value) = config.get_array(&opt) {
if !args.contains(&OsString::from(format!("--no-{}", opt))) {
for val in value {
if let Ok(val) = val.into_string() {
args.push(OsString::from(format!("--{}", opt)));
args.push(OsString::from(val));
}
}
}
}
}
}
Expand Down

0 comments on commit ef46e5a

Please sign in to comment.