Skip to content

Commit

Permalink
Merge pull request #36 from tinythings/isbm-inheritance-relative-path
Browse files Browse the repository at this point in the history
Relative paths in Inherited model
  • Loading branch information
isbm authored Oct 29, 2024
2 parents ff5adb8 + 4af4258 commit 2e1a1c8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/modeldescr/inheritance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ is not supported. Inheritance is started via ``inherits`` directive. Example:
Parent model can also inheriting some other model etc.

.. note::

Path usually is absolute but it can be also relative to the model location.

Inheriting Data
---------------

Expand Down
3 changes: 1 addition & 2 deletions examples/models/inherited/model.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ config:
# Path where modules are
modules: target/debug

#inherits: ../router
inherits: /home/bo/work/sysinspect/examples/models/router
inherits: ../router
9 changes: 5 additions & 4 deletions libsysinspect/src/mdescr/mspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{datapatch, mspecdef::ModelSpec};
use crate::SysinspectError;
use serde_yaml::Value;
use std::{
fs,
fs::{self},
path::{Path, PathBuf},
};
use walkdir::WalkDir;
Expand All @@ -20,8 +20,8 @@ struct SpecLoader {

impl SpecLoader {
// Constructor
fn new(pth: &str) -> Self {
Self { pth: PathBuf::from(pth), init: false }
fn new(pth: PathBuf) -> Self {
Self { pth, init: false }
}

/// Collect YAML parts of the model from a different files
Expand Down Expand Up @@ -110,6 +110,7 @@ impl SpecLoader {
// Try inheriting
if !mpt.is_empty() {
if let Some(ipth) = serde_yaml::from_value::<ModelSpec>(mpt[0].to_owned())?.inherits() {
let ipth = fs::canonicalize(self.pth.join(ipth))?; // Redirect path
base.insert(0, mpt[0].to_owned());
base.extend(self.collect_by_path(&ipth, true)?);
iht.extend(mpt[1..].iter().map(|e| e.to_owned()).collect::<Vec<Value>>());
Expand All @@ -131,5 +132,5 @@ impl SpecLoader {

/// Load spec from a given path
pub fn load(path: &str) -> Result<ModelSpec, SysinspectError> {
SpecLoader::new(path).load()
SpecLoader::new(fs::canonicalize(path)?).load()
}

0 comments on commit 2e1a1c8

Please sign in to comment.