Skip to content

Commit

Permalink
add prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed May 24, 2024
1 parent 19fe127 commit ac9a8dd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ fn render_jinja_template(
}

// Render the template
let prefixes = get_prefixes(model);
template.render(context! {
objects => model.objects,
object_names => object_names,
title => model.name,
prefixes => prefixes,
})
}

Expand All @@ -128,3 +130,10 @@ fn convert_model_types(
}
}
}

fn get_prefixes(model: &mut DataModel) -> Vec<(String, String)> {
match &model.config {
Some(config) => config.prefixes().unwrap_or(vec![]),
None => vec![],
}
}
28 changes: 25 additions & 3 deletions src/markdown/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,31 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct FrontMatter {
#[serde(default = "default_id_field", rename = "id-field")]
id_field: bool,
prefixes: Option<HashMap<String, String>>,
nsmap: Option<HashMap<String, String>>,
pub id_field: bool,
pub prefixes: Option<HashMap<String, String>>,
pub nsmap: Option<HashMap<String, String>>,
}

impl FrontMatter {
pub fn id_field(&self) -> bool {
self.id_field
}

pub fn prefixes(&self) -> Option<Vec<(String, String)>> {
match &self.prefixes {
Some(prefixes) => Some(
prefixes
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
),
None => None,
}
}

pub fn nsmap(&self) -> &Option<HashMap<String, String>> {
&self.nsmap
}
}

impl Default for FrontMatter {
Expand Down
13 changes: 12 additions & 1 deletion templates/python-dataclass.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ from __future__ import annotations
from dataclasses import dataclass, field
from dataclasses_json import config, dataclass_json
from typing import List, Optional
from uuid import uuid4

{% for object in objects %}
@dataclass_json
@dataclass
class {{ object.name }}:

{%- for attribute in object.attributes %}
{%- if attribute.multiple is true %}
{{ attribute.name }}: List[{{ attribute.dtypes[0] }}]
Expand All @@ -16,16 +18,25 @@ class {{ object.name }}:
{%- endif %}
{%- endfor %}

# JSON-LD fields
id: str = field(
metadata=config(field_name="@id"),
default_factory=lambda: "md:{{ object.name }}/" + str(uuid4())
)
__type__: list[str] = field(
metadata=config(field_name="@type"),
default_factory = lambda: [
"ex:{{ object.name }}",
"md:{{ object.name }}",
{%- if object.term -%}"{{ object.term }}"{%- endif %}
],
)
__context__: dict[str, str | dict] = field(
metadata=config(field_name="@context"),
default_factory = lambda: {
"md": "http://mdmodel.net/",
{%- for prefix, address in prefixes %}
"{{ prefix }}": "{{ address }}",
{%- endfor %}
{%- for attribute in object.attributes %}
{%- if attribute.term %}
"{{ attribute.name }}": "{{ attribute.term }}",
Expand Down
12 changes: 8 additions & 4 deletions templates/shacl.jinja
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.org/> .
@prefix md: <http://mdmodel.net/> .
{%- for prefix, value in prefixes %}
@prefix {{ prefix }}: <{{ value }}> .
{%- endfor %}

{% for object in objects %}
ex:{{ object.name }}Shape
md:{{ object.name }}Shape
a sh:NodeShape ;
sh:targetClass ex:{{ object.name }} ;
sh:targetClass md:{{ object.name }} ;
{% for attribute in object.attributes %}
{%- if attribute.term %}
sh:property [
sh:path {{ attribute.term }} ;
{%- if attribute.dtypes[0] in object_names %}
sh:node ex:{{ attribute.dtypes[0] }}Shape ;
sh:node md:{{ attribute.dtypes[0] }}Shape ;
{%- else %}
sh:datatype xsd:{{ attribute.dtypes[0] }} ;
{%- endif %}
Expand Down

0 comments on commit ac9a8dd

Please sign in to comment.