Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
fixing error finding elements in the model, and report of line locati…
Browse files Browse the repository at this point in the history
…on correctly for scalar YAML nodes. fixes #46 #43
  • Loading branch information
antoniogarrote committed Apr 25, 2017
1 parent 4f84db6 commit a2dbc44
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 99 deletions.
12 changes: 6 additions & 6 deletions js/js-support-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1472,10 +1472,10 @@ function readPlainScalar(state, nodeIndent, withinFlowCollection) {
var endIndex = state.position;

var positions = {
"start-line": startLine,
"start-line": startLine + 1,
"start-column": startColumn,
"start-index": startIndex,
"end-line": endLine,
"end-line": endLine + 1,
"end-column": endColumn,
"end-index": endIndex
};
Expand Down Expand Up @@ -1534,10 +1534,10 @@ function readSingleQuotedScalar(state, nodeIndent) {
var endIndex = state.position;

var positions = {
"start-line": startLine,
"start-line": startLine + 1,
"start-column": startColumn,
"start-index": startIndex,
"end-line": endLine,
"end-line": endLine + 1,
"end-column": endColumn,
"end-index": endIndex
};
Expand Down Expand Up @@ -1599,10 +1599,10 @@ function readDoubleQuotedScalar(state, nodeIndent) {
var endIndex = state.position;

var positions = {
"start-line": startLine,
"start-line": startLine + 1,
"start-column": startColumn,
"start-index": startIndex,
"end-line": endLine,
"end-line": endLine + 1,
"end-column": endColumn,
"end-index": endIndex
};
Expand Down
12 changes: 6 additions & 6 deletions js/js-yaml/lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,10 @@ function readPlainScalar(state, nodeIndent, withinFlowCollection) {
var endIndex = state.position;

var positions = {
"start-line": startLine,
"start-line": startLine + 1,
"start-column": startColumn,
"start-index": startIndex,
"end-line": endLine,
"end-line": endLine + 1,
"end-column": endColumn,
"end-index": endIndex
};
Expand Down Expand Up @@ -574,10 +574,10 @@ function readSingleQuotedScalar(state, nodeIndent) {
var endIndex = state.position;

var positions = {
"start-line": startLine,
"start-line": startLine + 1,
"start-column": startColumn,
"start-index": startIndex,
"end-line": endLine,
"end-line": endLine + 1,
"end-column": endColumn,
"end-index": endIndex
};
Expand Down Expand Up @@ -639,10 +639,10 @@ function readDoubleQuotedScalar(state, nodeIndent) {
var endIndex = state.position;

var positions = {
"start-line": startLine,
"start-line": startLine + 1,
"start-column": startColumn,
"start-index": startIndex,
"end-line": endLine,
"end-line": endLine + 1,
"end-column": endColumn,
"end-index": endIndex
};
Expand Down
32 changes: 21 additions & 11 deletions src/api_modeling_framework/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,27 @@

(defn find-element* [model id]
(cond
(map? model) (if (= id (or (:id model) (get model "@id")))
model
(->> (vals model)
(map (fn [m] (find-element* m id)))
(filter some?)
first))
(coll? model) (->> model
(map (fn [m] (find-element* m id)))
(filter some?)
first)
:else nil))
;; we prefer elements in declarations if present
(and (map? model)
(:declares model)
(some #(= id (:id %))
(:declares model))) (->> (:declares model)
(filter #(= id (:id %)))
first)

(map? model) (if (= id (or (:id model) (get model "@id")))
model
(->> (vals model)
(map (fn [m] (find-element* m id)))
(filter some?)
first))

(coll? model) (->> model
(map (fn [m] (find-element* m id)))
(filter some?)
first)

:else nil))

(defn to-model
([res]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@
(if (utils/object-no-properties? base)
(first types)
(assoc base :type (first types)))
{:type types})))
{:type "union"
:anyOf types})))

(defmethod parse-shape :raml-expression [shape _] (-> shape (get (v/shapes-ns "ramlTypeExpression")) first (get "@value")))

Expand Down
52 changes: 29 additions & 23 deletions src/api_modeling_framework/parser/document/raml.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,38 @@
(mapv (fn [annotation]
[(document/name annotation) annotation]))
(into {}))
working-declarations library-declarations
;; we parse traits and types and add the information into the context
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
:fragments fragments
:references working-declarations
:document-parser parse-ast
:parsed-location location})
working-declarations (merge working-declarations types)
doc-annotations (domain-parser/process-annotations (syntax/<-data node) {:base-uri location
:references working-declarations
:location (str location "#")
:parsed-location (str location "#")})
annotations (merge library-declarations doc-annotations)
;; we parse traits and types and add the information into the context
traits (domain-parser/process-traits (syntax/<-data node) {:location (str location "#")
:fragments fragments
:references library-declarations
:references working-declarations
:document-parser parse-ast
:annotations annotations
:parsed-location (str location "#")})
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
:fragments fragments
:references library-declarations
:annotations annotations
:document-parser parse-ast
:parsed-location location})
declarations (merge traits types)
working-declarations (merge working-declarations traits)
encoded (domain-parser/parse-ast (syntax/<-data node) {:location (str location "#")
:fragments fragments
:parsed-location (str location "#")
:annotations annotations
:references (merge declarations library-declarations)
:references working-declarations
:document-parser parse-ast
:is-fragment false})]
(-> (document/map->ParsedDocument {:id location
:location location
:encodes encoded
:declares (concat (vals declarations)
:declares (concat (vals types)
(vals traits)
(vals doc-annotations))
:references (compute-fragments
(concat (vals @fragments)
Expand All @@ -137,33 +140,36 @@
(mapv (fn [annotation]
[(document/name annotation) annotation]))
(into {}))
working-declarations library-declarations
;; we parse traits and types and add the information into the context
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
:fragments fragments
:alias-chain alias-chain
:references working-declarations
:document-parser parse-ast
:parsed-location location})
working-declarations (merge working-declarations types)
doc-annotations (domain-parser/process-annotations (syntax/<-data node) {:base-uri location
:location (str location "#")
:parsed-location (str location "#/annotations")
:references library-declarations})
:references working-declarations})
annotations (merge libraries-annotation doc-annotations)
;; we parse traits and types and add the information into the context
traits (domain-parser/process-traits (syntax/<-data node) {:location (str location "#")
:fragments fragments
:references library-declarations
:references working-declarations
:alias-chain alias-chain
:document-parser parse-ast
:annotations annotations
:parsed-location (str location "#")})
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
:fragments fragments
:alias-chain alias-chain
:references library-declarations
:annotations annotations
:document-parser parse-ast
:parsed-location location})
declarations (merge traits types)

usage (:usage (syntax/<-data node))]
(-> (document/map->ParsedModule (utils/clean-nils
{:id location
:location location
:description usage
:declares (concat (vals declarations) (vals doc-annotations))
:declares (concat (vals types)
(vals traits)
(vals doc-annotations))
:references (compute-fragments
(concat (vals @fragments)
(flatten (vals libraries))))
Expand Down
79 changes: 45 additions & 34 deletions src/api_modeling_framework/parser/domain/raml.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -345,44 +345,53 @@
(assoc acc (keyword (utils/alias-chain trait-name context)) parsed-trait)))
{}))))

(defn process-types [node {:keys [location parsed-location alias-chain] :as context}]
(defn process-types [node {:keys [location parsed-location alias-chain references] :as context}]
(let [types (or (:types node) (:schemas node) {})
path-label (if (some? (:types node)) "types" "schemas")
location (utils/path-join parsed-location "/" path-label)
nested-context (-> context (assoc :location location) (assoc :parsed-location parsed-location))]
(debug "Processing " (count types) " types")
(->> types
(reduce (fn [acc [type-name type-node]]
(debug (str "Processing type " type-name))
(let [location-meta (meta type-node)
type-node (common/purge-ast type-node)
type-node (if (syntax/fragment? type-node)
;; avoiding situations where we transform this into an include
;; and then we cannot transform this back into type because there's
;; no way to tell it without source maps
{:type type-node}
type-node)
type-name (url/url-encode (utils/safe-str type-name))
type-id (common/type-reference parsed-location type-name)
references (get nested-context :references {})
type-fragment (parse-ast type-node (-> nested-context
(assoc :references (merge references acc))
(assoc :location location)
(assoc :parsed-location type-id)
(assoc :is-fragment false)
(assoc :type-hint :type)))
sources (or (-> type-fragment :sources) [])
;; we annotate the parsed type with the is-type source map so we can distinguish it from other declarations
sources (concat sources (common/generate-is-type-sources type-name
(utils/path-join location type-name)
type-id))
parsed-type (assoc type-fragment :sources sources)
parsed-type (if (nil? (:name parsed-type))
(assoc parsed-type :name type-name)
parsed-type)
parsed-type (assoc parsed-type :lexical location-meta)]
(assoc acc (keyword (utils/alias-chain type-name context)) parsed-type)))
{}))))
(let [;; we will mark the positions of references in the types node
ahead-references (->> types
(map (fn [[type-name _]]
(let [type-name (url/url-encode (utils/safe-str type-name))
type-id (common/type-reference parsed-location type-name)]
[(keyword type-name) {:x-ahead-declaration type-id}])))
(into {}))
working-references (atom (merge references ahead-references))]
(->> types
(reduce (fn [acc [type-name type-node]]
(debug (str "Processing type " type-name))
(let [location-meta (meta type-node)
type-node (common/purge-ast type-node)
type-node (if (syntax/fragment? type-node)
;; avoiding situations where we transform this into an include
;; and then we cannot transform this back into type because there's
;; no way to tell it without source maps
{:type type-node}
type-node)
type-name (url/url-encode (utils/safe-str type-name))
type-id (common/type-reference parsed-location type-name)
type-fragment (parse-ast type-node (-> nested-context
(assoc :references @working-references)
(assoc :location location)
(assoc :parsed-location type-id)
(assoc :is-fragment false)
(assoc :type-hint :type)))
sources (or (-> type-fragment :sources) [])
;; we annotate the parsed type with the is-type source map so we can distinguish it from other declarations
sources (concat sources (common/generate-is-type-sources type-name
(utils/path-join location type-name)
type-id))
parsed-type (assoc type-fragment :sources sources)
parsed-type (if (nil? (:name parsed-type))
(assoc parsed-type :name type-name)
parsed-type)
parsed-type (assoc parsed-type :lexical location-meta)]
;; let's also update the working reference to this ahead declaration
(swap! working-references (fn [old-working-references] (assoc old-working-references (keyword type-name) parsed-type)))
(assoc acc (keyword (utils/alias-chain type-name context)) parsed-type)))
{})))))

(defn find-extend-tags [{:keys [location parsed-location references] :as context}]
(->> references
Expand Down Expand Up @@ -428,7 +437,9 @@
:scheme (utils/ensure-not-blank (root->scheme node))
:base-path (utils/ensure-not-blank (base-uri->basepath (extract-scalar (:baseUri node))))
:accepts (filterv some? (flatten [(extract-scalar (:mediaType node))]))
:content-type (filterv some? (flatten [(:mediaType node)]))
:content-type (->> (flatten [(:mediaType node)])
(map extract-scalar)
(filterv some?))
:version (extract-scalar (:version node))
:provider nil
:terms-of-service nil
Expand Down
Loading

0 comments on commit a2dbc44

Please sign in to comment.