ResolveValue

Trait ResolveValue 

Source
pub trait ResolveValue {
    // Provided methods
    fn merge_properties<'a>(
        value: &'a mut Value,
        _data_path: &DataPath<'_>,
    ) -> Result<&'a mut Value, DataResolverError> { ... }
    fn init_with_identifier(_identifier: Value) -> Value { ... }
    fn resolve_value(
        data_path: DataPath<'_>,
    ) -> Result<Value, DataResolverError> { ... }
    fn resolve_vec_base(_data_path: &DataPath<'_>) -> Value { ... }
}
Expand description

This trait, when implemented on a type, attaches methods for retrieving a serde_yaml::Value representation of that type. For primitives, a default impl will do. For structs, you will mostly specify the merge_properties function in a very straightforward way, i.e.

use confql_data_resolver::{DataPath, DataResolverError, Merge, ResolveValue};
use serde_yaml;

struct MyObj {
    id: i32,
    name: String,
}

impl ResolveValue for MyObj {
    fn merge_properties<'a>(
        value: &'a mut serde_yaml::Value,
        data_path: &DataPath,
    ) -> Result<&'a mut serde_yaml::Value, DataResolverError> {
        if let Ok(id) = i32::resolve_value(data_path.join("id")) {
            value.merge_at("id", id)?;
        }
        if let Ok(name) = String::resolve_value(data_path.join("name")) {
            value.merge_at("name", name)?;
        }
        Ok(value)
    }
}

In fact, that’s what a procedural macro in the codebase does for you.

Provided Methods§

Source

fn merge_properties<'a>( value: &'a mut Value, _data_path: &DataPath<'_>, ) -> Result<&'a mut Value, DataResolverError>

Implement this for structs as described in ResolveValue.

Source

fn init_with_identifier(_identifier: Value) -> Value

Create a base value from an identifier. Useful when building an array, where some fields are defined with @confql(arrayIdentifier: true) in the GraphQL schema. Then you can pre-populate said fields with a file name or mapping key.

Source

fn resolve_value(data_path: DataPath<'_>) -> Result<Value, DataResolverError>

Resolve data from the given DataPath. The default implementation should be sufficient in most cases.

Source

fn resolve_vec_base(_data_path: &DataPath<'_>) -> Value

Resolve a starting value before data acquisition from actual file content. Null (default impl) is a good starting value in most cases, because it accepts any merge. Explicitly implement this in cases like impl<T: ResolveValue> ResolveValue for Vec<T> where the initial value might not be null (i.e. in the Vec case, some fields may be predefined by the file stem of your DataPath.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ResolveValue for bool

Source§

impl ResolveValue for f64

Source§

impl ResolveValue for i32

Source§

impl ResolveValue for String

Source§

impl ResolveValue for ID

Source§

impl<T: ResolveValue> ResolveValue for Option<T>

Source§

impl<T: ResolveValue> ResolveValue for Vec<T>

Source§

fn merge_properties<'a>( value: &'a mut Value, data_path: &DataPath<'_>, ) -> Result<&'a mut Value, DataResolverError>

Implementors§