Derive macro getting works

This commit is contained in:
nora 2024-04-15 19:48:17 +02:00
parent 63bd32c3cd
commit 7d28815065
9 changed files with 245 additions and 41 deletions

View file

@ -6,6 +6,5 @@ edition = "2021"
[dependencies]
eyre = "0.6.12"
terustform = { path = "../terustform" }
terustform-macros = { path = "../terustform-macros" }
tokio = { version = "1.37.0", features = ["full"] }

View file

@ -4,8 +4,7 @@ use terustform::{
framework::{
datasource::{self, DataSource},
provider::Provider,
value::StringValue,
DResult,
AttrPath, DResult, Diagnostics, StringValue, ValueModel,
},
values::{Value, ValueKind},
};
@ -29,11 +28,6 @@ impl Provider for ExampleProvider {
struct ExampleDataSource {}
#[derive(terustform_macros::DataSourceModel)]
struct _ExampleDataSourceModel {
name: StringValue,
}
impl DataSource for ExampleDataSource {
fn name(&self, provider_name: &str) -> String {
format!("{provider_name}_kitty")
@ -72,20 +66,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!(),
let model = ExampleDataSourceModel::from_value(config, &AttrPath::root())?;
let StringValue::Known(name_str) = &model.name else {
return Err(Diagnostics::error_string(
"model name must be known".to_owned(),
));
};
let meow = format!("mrrrrr i am {name_str}");
Ok(Value::Known(ValueKind::Object(BTreeMap::from([
("name".to_owned(), name),
("name".to_owned(), model.name.to_value()),
(
"meow".to_owned(),
Value::Known(ValueKind::String(format!("mrrrrr i am {name_str}"))),
Value::Known(ValueKind::String(meow)),
),
(
"id".to_owned(),
@ -94,3 +88,10 @@ impl DataSource for ExampleDataSource {
]))))
}
}
#[derive(terustform::DataSourceModel)]
struct ExampleDataSourceModel {
name: StringValue,
meow: StringValue,
id: StringValue,
}