bartkl / metamorph

Metamorph is a Clojure libary that enables the generation of an Avro schema from a given input SHACL model.
https://bartkl.github.io/metamorph/
Apache License 2.0
2 stars 3 forks source link

Parse RDF lists outside as part of SHACL processing #4

Open bartkl opened 2 years ago

bartkl commented 2 years ago

Now, RDF lists in the Asami results are converted to seqs (using rdf-list->seq) during the Avro processing. That's bad separation of concerns.

Spec'ing made me catch this! Because I now had to deal with RDF lists in my SHACL specs.

bartkl commented 1 year ago

Making matters more pressing: the SQL schema generation code also does this list parsing itself, and now that we're implementing OpenAPI as a new schema target, we would yet again have to repeat ourselves.

This is the time to fix this.

bartkl commented 1 year ago
(ns metamorph.graph.db
  (:require [clojure.walk :as walk]
            [asami.core :as d]
            [metamorph.rdf.datatype :refer [rdf-list->seq]]))

;; ...

(defn parse-rdf-lists [n]
  (letfn [(rdf-list? [f] (and (:rdf/first f) (:rdf/rest f)))
          (single-property? [f] (and
                                 (vector? f)
                                 (= (first f) :sh/property)
                                 (not (set? (second f)))))]
    (println (:id n))
    (->> n
         (walk/prewalk #(cond
                          (rdf-list? %) (into [] (rdf-list->seq %))
                          (single-property? %) [(first %) (hash-set (second %))]
                          :else %)))))

;; ...

(defn get-resource [conn iri]
  (parse-rdf-lists (d/entity conn iri true)))

;; And of course remove the existing calls to `rdf-list->seq`.