to_value proc macro

This commit is contained in:
nora 2024-04-15 19:56:05 +02:00
parent 7d28815065
commit f3c37539f0
4 changed files with 57 additions and 47 deletions

View file

@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::HashMap;
use terustform::{
framework::{
@ -6,7 +6,7 @@ use terustform::{
provider::Provider,
AttrPath, DResult, Diagnostics, StringValue, ValueModel,
},
values::{Value, ValueKind},
values::Value,
};
#[tokio::main]
@ -28,6 +28,13 @@ impl Provider for ExampleProvider {
struct ExampleDataSource {}
#[derive(terustform::DataSourceModel)]
struct ExampleDataSourceModel {
name: StringValue,
meow: StringValue,
id: StringValue,
}
impl DataSource for ExampleDataSource {
fn name(&self, provider_name: &str) -> String {
format!("{provider_name}_kitty")
@ -66,7 +73,7 @@ impl DataSource for ExampleDataSource {
}
fn read(&self, config: Value) -> DResult<Value> {
let model = ExampleDataSourceModel::from_value(config, &AttrPath::root())?;
let mut model = ExampleDataSourceModel::from_value(config, &AttrPath::root())?;
let StringValue::Known(name_str) = &model.name else {
return Err(Diagnostics::error_string(
@ -75,23 +82,9 @@ impl DataSource for ExampleDataSource {
};
let meow = format!("mrrrrr i am {name_str}");
Ok(Value::Known(ValueKind::Object(BTreeMap::from([
("name".to_owned(), model.name.to_value()),
(
"meow".to_owned(),
Value::Known(ValueKind::String(meow)),
),
(
"id".to_owned(),
Value::Known(ValueKind::String("0".to_owned())),
),
]))))
model.meow = StringValue::Known(meow);
model.id = StringValue::Known("0".to_owned());
Ok(model.to_value())
}
}
#[derive(terustform::DataSourceModel)]
struct ExampleDataSourceModel {
name: StringValue,
meow: StringValue,
id: StringValue,
}