diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:47:26 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:47:26 +0100 |
| commit | fa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 (patch) | |
| tree | 577a41415e67510f08f43fc48c794c36a10f6e80 /lib/configuration | |
| parent | b05e2d2be7be28ee6c577671002797cf9abaf177 (diff) | |
Updated the model for representing ocaml types in the application type
Diffstat (limited to 'lib/configuration')
| -rw-r--r-- | lib/configuration/ast.ml | 42 | ||||
| -rw-r--r-- | lib/configuration/ast.mli | 4 | ||||
| -rw-r--r-- | lib/configuration/read_conf.ml | 24 | ||||
| -rw-r--r-- | lib/configuration/tests/errors.t | 44 |
4 files changed, 103 insertions, 11 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 diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli index b014c9b..404c26e 100644 --- a/lib/configuration/ast.mli +++ b/lib/configuration/ast.mli @@ -25,6 +25,10 @@ exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn (** Exception raised when a reference in the configuration targets neither an existing content nor a declared list *) +exception ConflictingFields of Model.Identifier.t * Model.Identifier.t +(** Exception raised when a content or a list declares two fields with the same + name but different types *) + val register_list : setting -> Model.list -> setting (** Register a list in the setting, replacing any previous list with the same id. Lists are registered while they are decoded, so that a list can diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml index 67703da..4c57699 100644 --- a/lib/configuration/read_conf.ml +++ b/lib/configuration/read_conf.ml @@ -41,17 +41,16 @@ module Make (S : Decoders.Decode.S) = struct the error messages). *) (** Build the reference target for a field. Lists are decoded before the - contents and registered as soon as they are decoded, so a declared list - is matched eagerly; a content target is a lazy thunk closing over the - setting, only forced by [Ast.check] once every content has been - registered — this is what allows forward and cyclic references between - contents. The raw value is kept to report broken links. - *) + contents and registered as soon as they are decoded, so a declared list is + matched eagerly; a content target is a lazy thunk closing over the + setting, only forced by [Ast.check] once every content has been registered + — this is what allows forward and cyclic references between contents. The + raw value is kept to report broken links. *) let link (env : env) (value : S.value) (name : Model.Identifier.t) : Model.target S.decoder = match Ast.get_list env.settings name with | Some list -> S.succeed (Model.Reference.List list) - | None -> ( + | None -> if env.allow_content then S.succeed (Model.Reference.Content @@ -63,7 +62,7 @@ module Make (S : Decoders.Decode.S) = struct else S.fail_with (Decoders.Error.make ~context:value - "References in a list can only target a list declared before it")) + "References in a list can only target a list declared before it") let field_type (env : env) : Model.field_ex S.decoder = let text_type : Model.field_ex S.decoder = @@ -187,6 +186,15 @@ module Make (S : Decoders.Decode.S) = struct ("Dependency circle found between:" :: content_name) in S.fail_with (Decoders.Error.make message) + | Error (Ast.ConflictingFields (owner, name)) -> + let message = + "Two fields named \"" + ^ Model.Identifier.to_string name + ^ "\" with different types in \"" + ^ Model.Identifier.to_string owner + ^ "\"" + in + S.fail_with (Decoders.Error.make message) | Error other -> raise other end diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t index c4552d3..b12f29f 100644 --- a/lib/configuration/tests/errors.t +++ b/lib/configuration/tests/errors.t @@ -369,3 +369,47 @@ A field cannot be named "id" (reserved: it is the primary key of every row): in field "name": the name "id" is reserved: it is the primary key of every row, but got "id" + +Two fields with the same name but different types are rejected (in a content, +even across sections): + + $ tests --configuration <<EOF + > { + > "content": [ + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { "name": "code", "label": {}, "type": "text" } + > ] + > }, + > { + > "field": [ + > { "name": "code", "label": {}, "type": "integer" } + > ] + > } + > ] + > } + > ] + > } + > EOF + Two fields named "code" with different types in "entity" + +Same for a list: + + $ tests --configuration <<EOF + > { + > "list": [ + > { + > "id": "country_list", + > "field": [ + > { "name": "code", "label": {}, "type": "text" }, + > { "name": "code", "label": {}, "type": "integer" } + > ] + > } + > ], + > "content": [] + > } + > EOF + Two fields named "code" with different types in "country_list" |
