start parsing values correctly

This commit is contained in:
nora 2024-04-14 21:46:10 +02:00
parent a7822229f3
commit 43747f9cda
5 changed files with 31 additions and 25 deletions

View file

@ -61,17 +61,20 @@ impl DataSource for ExampleDataSource {
}
fn read(&self, config: Value) -> DResult<Value> {
let name = match config {
Value::Known(ValueKind::Object(mut obj)) => obj.remove("name").unwrap(),
_ => unreachable!(),
};
let name_str = match &name {
Value::Known(ValueKind::String(s)) => s.clone(),
_ => unreachable!(),
};
Ok(Value::Known(ValueKind::Object(BTreeMap::from([
(
"name".to_owned(),
match config {
Value::Known(ValueKind::Object(mut obj)) => obj.remove("name").unwrap(),
_ => unreachable!(),
},
),
("name".to_owned(), name),
(
"meow".to_owned(),
Value::Known(ValueKind::String("mrrrrr".to_owned())),
Value::Known(ValueKind::String(format!("mrrrrr i am {name_str}"))),
),
(
"id".to_owned(),

View file

@ -20,7 +20,7 @@ async fn main() -> eyre::Result<()> {
async fn serve(provider: &dyn Provider) -> eyre::Result<()> {
tracing_subscriber::fmt()
.with_max_level(Level::ERROR)
.with_max_level(Level::DEBUG)
.with_writer(std::io::stderr)
.without_time()
.init();

View file

@ -255,14 +255,7 @@ impl Provider for super::ProviderHandler {
}
};
let state = ds.read(crate::values::Value::Known(
crate::values::ValueKind::Object(BTreeMap::from([(
"name".to_owned(),
crate::values::Value::Known(crate::values::ValueKind::String(
"mykitten".to_owned(),
)),
)])),
));
let state = ds.read(config);
let (state, diagnostics) = match state {
Ok(s) => (
Some(tfplugin6::DynamicValue {

View file

@ -10,6 +10,7 @@ use rmp::encode::write_bool;
use crate::framework::{DResult, Diagnostics};
#[derive(Debug)]
pub enum Type {
Bool,
Number,
@ -166,6 +167,7 @@ impl Value {
}
pub fn msg_unpack(data: &[u8], typ: &Type) -> DResult<Self> {
tracing::debug!(?typ, ?data, "Unpacking message");
let mut read = io::Cursor::new(data);
Self::msg_unpack_inner(&mut read, typ)
}
@ -180,7 +182,7 @@ impl Value {
let read_string = |rd: &mut io::Cursor<&[u8]>| -> DResult<String> {
let len = std::cmp::min(mp::read_str_len(rd)?, 1024 * 1024); // you're not gonna get more than a 1MB string...
let mut buf = Vec::with_capacity(len as usize);
let mut buf = vec![0; len as usize];
rd.read_exact(&mut buf)?;
Ok(String::from_utf8(buf)?)
};
@ -209,13 +211,15 @@ impl Value {
Type::Dynamic => todo!("dynamic"),
Type::List { elem } => {
let len = mp::read_array_len(rd)?;
let elems = (0..len)
.map(|_| Value::msg_unpack_inner(rd, &elem))
.collect::<Result<Vec<_>, _>>()?;
ValueKind::List(elems)
}
Type::Map { elem } => {
let len = mp::read_array_len(rd)?;
let len = mp::read_map_len(rd)?;
let elems = (0..len)
.map(|_| -> DResult<_> {
let key = read_string(rd)?;
@ -227,6 +231,7 @@ impl Value {
}
Type::Set { elem } => {
let len = mp::read_array_len(rd)?;
let elems = (0..len)
.map(|_| Value::msg_unpack_inner(rd, &elem))
.collect::<Result<Vec<_>, _>>()?;
@ -234,7 +239,8 @@ impl Value {
}
Type::Object { attrs, optionals } => {
assert!(optionals.is_empty());
let len = mp::read_array_len(rd)?;
let len = mp::read_map_len(rd)?;
if attrs.len() != (len as usize) {
return Err(Diagnostics::error_string(format!(
"expected {} attrs, found {len} attrs in object",
@ -245,7 +251,7 @@ impl Value {
.map(|_| -> DResult<_> {
let key = read_string(rd)?;
let typ = attrs.get(&key).ok_or_else(|| {
Diagnostics::error_string(format!("unexpected attribute: {key}"))
Diagnostics::error_string(format!("unexpected attribute: '{key}'"))
})?;
let value = Value::msg_unpack_inner(rd, &typ)?;
Ok((key, value))
@ -261,11 +267,12 @@ impl Value {
elems.len()
)));
}
let elems = elems
.iter()
.map(|typ| Value::msg_unpack_inner(rd, &typ))
.collect::<Result<Vec<_>, _>>()?;
ValueKind::List(elems)
ValueKind::Tuple(elems)
}
};

View file

@ -11,16 +11,19 @@ provider "terustform" {}
//resource "terustform_hello" "test1" {}
data "terustform_kitty" "kitty" {
name = "mykitten"
name = "aa mykitten"
}
data "terustform_kitty" "hellyes" {
name = "a cute kitty"
name = "aa a cute kitty"
}
output "meow" {
value = data.terustform_kitty.kitty.id
}
output "hellyes" {
output "cat1" {
value = data.terustform_kitty.kitty.meow
}
output "cat2" {
value = data.terustform_kitty.hellyes.meow
}