refactor: moved get_template into new impl for wgu::types::ObjectTemplate

This commit is contained in:
selene 2025-12-18 17:24:24 +01:00
parent 3342ef5332
commit 226b880fb6
Signed by: sel
SSH key fingerprint: SHA256:33R/4Rx5Lu4o81LyJyXdMrmP5CJ6j7j1Soo0Dn7mKc0

View file

@ -6,7 +6,8 @@ use crate::types::{ObjectInstance, ObjectTemplate};
use serde_json::{Value, json};
fn get_template(object_type: &str) -> Result<ObjectTemplate, String> {
impl ObjectTemplate {
fn from_object_type(object_type: &str) -> Result<ObjectTemplate, String> {
let modules = item_registry();
let template: ObjectTemplate = match modules.get(object_type) {
@ -17,6 +18,7 @@ fn get_template(object_type: &str) -> Result<ObjectTemplate, String> {
Ok(template)
}
}
impl ObjectInstance {
pub fn from_template(object_type: &str, input: Value) -> Result<Self, String> {
@ -33,7 +35,7 @@ impl ObjectInstance {
pub fn validate(obj: &ObjectInstance) -> Result<(), String> {
let modules = item_registry();
let object_type = &obj.object_type;
let template = get_template(object_type)?;
let template = ObjectTemplate::from_object_type(object_type)?;
match modules.get(format!("{}:func:validator", object_type).as_str()) {
Some(ModuleItem::Validator(validate)) => validate(&obj)?,
@ -66,7 +68,7 @@ impl ObjectInstance {
pub fn transform(obj: &ObjectInstance, source: &str, destination: &str) -> Result<Self, String> {
let object_type = &obj.object_type;
let template = get_template(object_type)?;
let template = ObjectTemplate::from_object_type(object_type)?;
// assuming that the property to transform (source) is 'input.some.thing.nya',
// source_parts[0] is the category (input)