summaryrefslogtreecommitdiff
path: root/lib/configuration/ast.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/configuration/ast.ml')
-rw-r--r--lib/configuration/ast.ml42
1 files changed, 39 insertions, 3 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml
index edf129f..8ea92b2 100644
--- a/lib/configuration/ast.ml
+++ b/lib/configuration/ast.ml
@@ -19,16 +19,20 @@ let get_content : setting -> Model.Identifier.t -> Model.content option =
let get_list : setting -> Model.Identifier.t -> Model.list option =
fun setting name -> Splay.find name setting.lists
-(** All the lists declared in the setting, in a deterministic (identifier)
- order *)
+(** All the lists declared in the setting, in a deterministic (identifier) order
+*)
let lists : setting -> Model.list list =
fun setting ->
Splay.fold (fun acc (_, list) -> list :: acc) [] setting.lists
|> List.sort ~cmp:(fun a b ->
- Model.Identifier.compare a.Model.DataList.id b.Model.DataList.id)
+ Model.Identifier.compare a.Model.DataList.id b.Model.DataList.id)
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
+exception ConflictingFields of Model.Identifier.t * Model.Identifier.t
+(** [ConflictingFields (owner, field)]: the content or list [owner] declares two
+ fields named [field] with different types *)
+
(** Register a content and all the associated subcontent in the top level,
giving the possibility to fetch any content or subcontent by its name
without traversing the whole tree each time. *)
@@ -110,6 +114,23 @@ let tree : ?required:bool -> setting -> Graph_tree.t =
let root_contents : setting -> Model.content list =
fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) []
+(** Ensure that two fields with the same name in a content or a list always have
+ the same type — otherwise reading the field by name would be ambiguous. *)
+let check_fields : Model.Identifier.t -> Model.field Seq.t -> unit =
+ fun owner fields ->
+ let seen : (Model.Identifier.t, Model.field) Hashtbl.t = Hashtbl.create 16 in
+ Seq.iter
+ (fun field ->
+ let name = Model.Field.name field in
+ match Hashtbl.find_opt seen name with
+ | None -> Hashtbl.replace seen name field
+ | Some first ->
+ let (T t1) = Model.Field.type_ first in
+ let (T t2) = Model.Field.type_ field in
+ if Option.is_none (Model.Typ.eq t1 t2) then
+ raise (ConflictingFields (owner, name)))
+ fields
+
let check : setting -> (unit, exn) result =
fun setting ->
(* Ensure each content referenced exists.
@@ -122,5 +143,20 @@ let check : setting -> (unit, exn) result =
(* Check cycle dependencies *)
let required_tree = tree ~required:true setting in
Graph_tree.check_cycle required_tree;
+
+ (* Check that fields sharing a name have the same type *)
+ let () =
+ Splay.fold
+ (fun () (_, content) ->
+ check_fields (Model.Content.id content) (Model.Content.fields content))
+ () setting.contents
+ in
+ let () =
+ Splay.fold
+ (fun () (_, list) ->
+ check_fields list.Model.DataList.id
+ (List.to_seq list.Model.DataList.fields))
+ () setting.lists
+ in
Ok ()
with e -> Error e