Skip to content

Commit

Permalink
keep order of meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
eWert-Online committed Apr 10, 2024
1 parent 3855cf4 commit 78e8cfa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions lib/pinc_backend/Interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ let eval_meta sources =
let state =
State.make
~mode:`Portal_Collection
~tag_meta:StringMap.empty
~tag_meta:[]
~root_tag_data_provider:noop_data_provider
~tag_data_provider:noop_data_provider
declarations
Expand Down Expand Up @@ -1399,7 +1399,7 @@ let eval_declarations ~tag_data_provider ~root declarations =
State.make
~root_tag_data_provider:tag_data_provider
~tag_data_provider
~tag_meta:StringMap.empty
~tag_meta:[]
~mode:`Portal_Collection
declarations
in
Expand All @@ -1416,7 +1416,7 @@ let eval_declarations ~tag_data_provider ~root declarations =
in

let html = state |> State.get_output |> Value.to_string in
let meta_tree = state.tag_meta |> StringMap.to_seq |> List.of_seq in
let meta_tree = state.tag_meta in

(html, meta_tree)
;;
Expand Down
16 changes: 9 additions & 7 deletions lib/pinc_backend/State.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ let add_tag_meta ~meta key t =
match meta with
| None -> t
| Some meta ->
{
t with
tag_meta =
StringMap.union
(fun _ a b -> Some (Helpers.TagMeta.merge a b))
let merged =
match t.tag_meta |> List.mem_assoc key with
| false -> t.tag_meta @ [ (key, meta) ]
| true -> (
t.tag_meta
(StringMap.singleton key meta);
}
|> List.map @@ function
| k, v when k = key -> (k, Helpers.TagMeta.merge v meta)
| v -> v)
in
{ t with tag_meta = merged }
;;
13 changes: 7 additions & 6 deletions lib/pinc_backend/Tag.ml
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ module Tag_Record = struct
"Expected attribute %s to be a record."
(key |> List.rev |> List.hd)))
|> Option.map (fun _ ->
let state = { state with tag_path = key; tag_meta = StringMap.empty } in
let state = { state with tag_path = key; tag_meta = [] } in
match of' with
| None ->
Pinc_Diagnostics.error t.tag_loc "Attribute `of` is required on #Record."
Expand All @@ -586,15 +586,14 @@ module Tag_Record = struct
type of values in this record."
in
(value, meta))
|> Option.value ~default:(Helpers.Value.null ~loc:t.tag_loc (), StringMap.empty)
|> Option.value ~default:(Helpers.Value.null ~loc:t.tag_loc (), [])
in

let meta =
meta
|> Option.map
(Helpers.TagMeta.map @@ function
| `SubTagPlaceholder ->
child_meta |> StringMap.to_seq |> List.of_seq |> Helpers.TagMeta.record
| `SubTagPlaceholder -> child_meta |> Helpers.TagMeta.record
| v -> v)
in

Expand Down Expand Up @@ -622,7 +621,7 @@ module Tag_Array = struct
| None ->
Pinc_Diagnostics.error t.tag_loc "Attribute `of` is required on #Array."
| Some children ->
let state = { state with tag_path = key; tag_meta = StringMap.empty } in
let state = { state with tag_path = key; tag_meta = [] } in
let values, metas =
List.init len (fun i ->
let binding_identifier = Some (false, string_of_int i) in
Expand All @@ -632,7 +631,9 @@ module Tag_Array = struct
in
let value = state |> State.get_output in
let meta =
state.tag_meta |> StringMap.min_binding_opt |> Option.map snd
match state.tag_meta with
| (_, meta) :: _ -> Some meta
| _ -> None
in
(value, meta))
|> List.split
Expand Down
2 changes: 1 addition & 1 deletion lib/pinc_backend/Types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ and Type_State : sig
tag_data_provider : Type_Tag.data_provider;
root_tag_data_provider : Type_Tag.data_provider;
tag_path : string list;
tag_meta : Type_Tag.meta StringMap.t;
tag_meta : (string * Type_Tag.meta) list;
context : Type_Value.value StringMap.t;
mode : [ `Portal_Collection | `Portal_Render ];
}
Expand Down

0 comments on commit 78e8cfa

Please sign in to comment.