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> { fn read(&self, config: Value) -> DResult<Value> {
Ok(Value::Known(ValueKind::Object(BTreeMap::from([ let name = match config {
(
"name".to_owned(),
match config {
Value::Known(ValueKind::Object(mut obj)) => obj.remove("name").unwrap(), Value::Known(ValueKind::Object(mut obj)) => obj.remove("name").unwrap(),
_ => unreachable!(), _ => unreachable!(),
}, };
), let name_str = match &name {
Value::Known(ValueKind::String(s)) => s.clone(),
_ => unreachable!(),
};
Ok(Value::Known(ValueKind::Object(BTreeMap::from([
("name".to_owned(), name),
( (
"meow".to_owned(), "meow".to_owned(),
Value::Known(ValueKind::String("mrrrrr".to_owned())), Value::Known(ValueKind::String(format!("mrrrrr i am {name_str}"))),
), ),
( (
"id".to_owned(), "id".to_owned(),

View file

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

View file

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

View file

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

View file

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