1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
open StdLabels
module Splay = Splay.Make (Model.Identifier)
let escaped = String.escaped
type list' = { id : string; source : string }
type setting = {
mutable contents : Model.Reference.t Model.Content.t Splay.t;
lists : list' list;
}
let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
(** Get the content from the setting. *)
let get_content :
setting -> Model.Identifier.t -> Model.Reference.t Model.Content.t option =
fun settings name -> Splay.find name settings.contents
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Convert the content having fields referencing element by name into content
having fields referencing other contents *)
let rec convert :
'a Type.Id.t ->
setting ->
('a * Model.Identifier.t) Model.Content.t ->
Model.Reference.t Model.Content.t =
fun ty setting content ->
let subcontents =
List.map (Model.Content.subcontents content) ~f:(convert ty setting)
in
let sections =
List.map
~f:(fun section ->
let section' : Model.Reference.t Model.Section.t =
{
section with
fields =
List.map section.Model.Section.fields ~f:(fun field ->
Model.Field.map
(fun (value, name) ->
(* Raise a local exception here, which will be checked
later once the configuration will be completely
loaded *)
Model.Reference.v (fun () ->
match get_content setting name with
| Some v -> v
| None ->
(* Also check from the list catalog *)
begin match
List.find_opt setting.lists ~f:(fun { id; _ } ->
String.equal id
(Model.Identifier.to_string name))
with
| Some v ->
ignore v;
raise_notrace
(NotImplemented (ty, value, name))
| None ->
raise_notrace (BrokenLink (ty, value, name))
end))
field);
}
in
section')
(Model.Content.sections content)
in
Model.Content.v (Model.Content.id content) sections subcontents
(** Register a content and all the associated subcontent in the top level,
giving the possibility to fetch any content or subcontent by is’s name
without traversing the whole tree each time. *)
let register_content :
'a Type.Id.t -> setting -> ('a * Model.Identifier.t) Model.Content.t -> unit =
fun ty setting content ->
let rec traverse content =
let () =
setting.contents <-
Splay.add (Model.Content.id content) content setting.contents
in
List.iter ~f:traverse (Model.Content.subcontents content)
in
traverse (convert ty setting content)
let register_list : setting -> list' -> setting =
fun t l -> { t with lists = l :: t.lists }
(** Force the evaluation of the field target and ensure it points to an existing
content in the application.
Return the content targeted *)
let get_reference_target :
Model.Reference.t Model.Field.t -> Model.Reference.t Model.Content.t option
=
fun field ->
let (T ty) = Model.Field.type_ field in
match ty with
| Text -> None
| Integer -> None
| MonoReference r | MultiReference r -> (
try Some (Model.Reference.get r.Model.Typ.t)
with BrokenLink _ as e -> raise_notrace e)
(** Extract the other contents pointed by a given content, and the field used
for it. *)
let targeted_contents :
Model.Reference.t Model.Content.t ->
(Model.Reference.t Model.Field.t * Model.Reference.t Model.Content.t) Seq.t
=
fun t ->
Seq.filter_map
(fun field ->
match get_reference_target field with
| None -> None
| Some ref -> Some (field, ref))
(Model.Content.fields t)
let tree : ?required:bool -> setting -> Graph_tree.t =
fun ?(required = false) setting ->
let tree = Graph_tree.create () in
let () =
Splay.fold
(fun () (_, src) ->
let targets = targeted_contents src in
let root = Model.Content.root src in
Graph_tree.add_content tree root;
Seq.iter
(fun (field, dst) ->
(* We build the dependencies tree with the root contents only.
This does not change the logic, and this makes things easer
too *)
let add_edge = (not required) || Model.Field.required field in
match add_edge with
| false -> ()
| true ->
Graph_tree.add_edge tree root field (Model.Content.root dst))
targets)
() setting.contents
in
tree
(** Return all the contents declared in the settings.
The list provide the root contents in the topological order *)
let root_contents : setting -> Model.Reference.t Model.Content.t list =
fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) []
let check : setting -> (unit, exn) result =
fun setting ->
(* Ensure each content referenced exists.
We accept mutually recursive dependancy and need to load all the content
first before checking the settings *)
try
let _ = tree setting in
(* Check cycle dependencies *)
let required_tree = tree ~required:true setting in
Graph_tree.check_cycle required_tree;
Ok ()
with e -> Error e
|