summaryrefslogtreecommitdiff
path: root/lib/configuration
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-19 09:57:49 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-19 09:57:49 +0100
commite1bf7686f408b52a870d4ed4f3c565dbbcc3954f (patch)
treeee7e156032cc0c43ca10879051e64c3dfeab1515 /lib/configuration
parenta9628fda0fc027c54b5ed5313a0c41731d79e41f (diff)
Management of lists in the settings
Diffstat (limited to 'lib/configuration')
-rw-r--r--lib/configuration/ast.ml29
-rw-r--r--lib/configuration/ast.mli16
-rw-r--r--lib/configuration/read_conf.ml59
-rw-r--r--lib/configuration/tests/configuration.t44
-rw-r--r--lib/configuration/tests/errors.t20
5 files changed, 124 insertions, 44 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml
index 607031e..6d29d0c 100644
--- a/lib/configuration/ast.ml
+++ b/lib/configuration/ast.ml
@@ -3,8 +3,10 @@ module Splay = Splay.Make (Model.Identifier)
let escaped = String.escaped
-type list' = { id : string; source : string }
-type setting = { mutable contents : Model.content Splay.t; lists : list' list }
+type setting = {
+ mutable contents : Model.content Splay.t;
+ lists : Model.List.t list;
+}
let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
@@ -12,14 +14,16 @@ let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
let get_content : setting -> Model.Identifier.t -> Model.content option =
fun settings name -> Splay.find name settings.contents
-(** Check whether a list with the given name is declared in the setting *)
-let has_list : setting -> Model.Identifier.t -> bool =
+(** Get the list declared with the given name, if any *)
+let get_list : setting -> Model.Identifier.t -> Model.List.t option =
fun setting name ->
- List.exists setting.lists ~f:(fun { id; _ } ->
- String.equal id (Model.Identifier.to_string name))
+ List.find_opt setting.lists ~f:(fun l ->
+ Model.Identifier.equal l.Model.List.id name)
+
+(** All the lists declared in the setting *)
+let lists : setting -> Model.List.t list = fun setting -> setting.lists
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
-exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Register a content and all the associated subcontent in the top level,
giving the possibility to fetch any content or subcontent by its name
@@ -35,7 +39,7 @@ let register_content : setting -> Model.content -> unit =
in
traverse content
-let register_list : setting -> list' -> setting =
+let register_list : setting -> Model.List.t -> setting =
fun t l -> { t with lists = l :: t.lists }
(** Force the evaluation of the field target and ensure it points to an existing
@@ -49,8 +53,13 @@ let get_reference_target : Model.field -> Model.content option =
| Text -> None
| Integer -> None
| MonoReference r | MultiReference r -> (
- try Some (Model.Reference.get r.Model.Typ.t)
- with BrokenLink _ as e -> raise_notrace e)
+ (* References to static lists are leaves of the dependency graph: they
+ are not followed here. *)
+ match r.Model.Typ.t with
+ | Model.Typ.List _ -> None
+ | Model.Typ.Content ref -> (
+ try Some (Model.Reference.get ref)
+ with BrokenLink _ as e -> raise_notrace e))
(** Extract the other contents pointed by a given content, and the field used
for it. *)
diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli
index 298e2d5..ce5d1b6 100644
--- a/lib/configuration/ast.mli
+++ b/lib/configuration/ast.mli
@@ -1,13 +1,15 @@
type setting
-type list' = { id : string; source : string }
val init : unit -> setting
val get_content : setting -> Model.Identifier.t -> Model.content option
(** Get the content from the setting. *)
-val has_list : setting -> Model.Identifier.t -> bool
-(** Check whether a list with the given name is declared in the setting *)
+val get_list : setting -> Model.Identifier.t -> Model.List.t option
+(** Get the list declared with the given name, if any *)
+
+val lists : setting -> Model.List.t list
+(** All the lists declared in the setting *)
val root_contents : setting -> Model.content list
(** Extract all the content in the topological order.
@@ -20,12 +22,10 @@ val register_content : setting -> Model.content -> unit
without traversing the whole tree each time. *)
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
-(** Exception raised when a reference in the configuration targets a
- non-existing content *)
-
-exception NotImplemented : ('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 *)
-val register_list : setting -> list' -> setting
+val register_list : setting -> Model.List.t -> setting
val check : setting -> (unit, exn) result
(** Check the setting and return the the error as an exception *)
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index bcd8f8a..c7149ae 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -34,19 +34,22 @@ module Make (S : Decoders.Decode.S) = struct
targets) and a type id (to attribute broken links to this parsing pass in
the error messages). *)
- (** Build the reference target for a field: a lazy thunk closing over the
- setting. The thunk is 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. *)
+ (** Build the reference target for a field. Lists are decoded before the
+ contents, 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.Reference.t =
- Model.Reference.v (fun () ->
- match Ast.get_content env.settings name with
- | Some content -> content
- | None ->
- if Ast.has_list env.settings name then
- raise_notrace (Ast.NotImplemented (env.ty, value, name))
- else raise_notrace (Ast.BrokenLink (env.ty, value, name)))
+ Model.target =
+ match Ast.get_list env.settings name with
+ | Some list -> Model.Typ.List list
+ | None ->
+ Model.Typ.Content
+ (Model.Reference.v (fun () ->
+ match Ast.get_content env.settings name with
+ | Some content -> content
+ | None -> raise_notrace (Ast.BrokenLink (env.ty, value, name))))
let field_type (env : env) : Model.field_ex S.decoder =
let text_type : Model.field_ex S.decoder =
@@ -105,9 +108,23 @@ module Make (S : Decoders.Decode.S) = struct
in
S.succeed (Model.Content.v id sections subcontents))
- let list' : Ast.list' S.decoder =
- let* id = S.field "id" S.string and* source = S.field "source" S.string in
- S.succeed Ast.{ id; source }
+ let list_source : Model.List.source S.decoder =
+ (* The source is either a plain kind string ("csv"), or an object with a
+ "type" and an optional "file" path. *)
+ let as_object =
+ let* kind = S.field "type" S.string
+ and* file = S.field_opt "file" S.string in
+ S.succeed Model.List.{ kind; file }
+ in
+ let as_string =
+ S.map (fun kind -> Model.List.{ kind; file = None }) S.string
+ in
+ S.one_of [ ("object", as_object); ("string", as_string) ]
+
+ let list : Model.List.t S.decoder =
+ let* id = S.field "id" identifier
+ and* source = S.field "source" list_source in
+ S.succeed Model.List.{ id; source }
let t : Ast.setting S.decoder =
(* Register a type id to attribute broken links to this parsing pass *)
@@ -115,7 +132,7 @@ module Make (S : Decoders.Decode.S) = struct
(* Decode the lists first, then the contents: the reference targets are
lazy thunks closing over [settings], forced only by [Ast.check] once
every content has been registered. *)
- let* lists = S.field_opt_or ~default:[] "list" (S.list list') in
+ let* lists = S.field_opt_or ~default:[] "list" (S.list list) in
let settings =
List.fold_left lists ~init:(Ast.init ()) ~f:Ast.register_list
in
@@ -136,16 +153,6 @@ module Make (S : Decoders.Decode.S) = struct
^ "\". Expected existing list or content"
in
S.fail_with (Decoders.Error.make ~context message))
- | Error (Ast.NotImplemented (ty', context, name) as e) -> (
- match Type.Id.provably_equal ty ty' with
- | None -> raise e
- | Some Type.Equal ->
- let message =
- "Unsupported target \""
- ^ Model.Identifier.to_string name
- ^ "\". Lists as target are not implemented yet"
- in
- S.fail_with (Decoders.Error.make ~context message))
| Error (Graph_tree.Cycle contents) ->
let content_name =
List.map contents ~f:(fun c ->
diff --git a/lib/configuration/tests/configuration.t b/lib/configuration/tests/configuration.t
index e44b91a..880e690 100644
--- a/lib/configuration/tests/configuration.t
+++ b/lib/configuration/tests/configuration.t
@@ -75,3 +75,47 @@ Cycles between elements are accepted when they are not mandatory to each other
> }
+
+Lists are declared in the "list" key, with a source given either as a plain
+kind string or as an object with a "type" and an optional "file". A reference
+field can target a list, like a content:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "user_type_list",
+ > "source": "csv"
+ > },
+ > {
+ > "id": "status_list",
+ > "source": { "type": "csv", "file": "status.csv" }
+ > }
+ > ],
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "user_type",
+ > "label": {},
+ > "type": { "content": "user_type_list" }
+ > },
+ > {
+ > "name": "statuses",
+ > "label": {},
+ > "type": {
+ > "content": "status_list",
+ > "multivaluated": true
+ > }
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+
diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t
index bad5b3b..99ad465 100644
--- a/lib/configuration/tests/errors.t
+++ b/lib/configuration/tests/errors.t
@@ -215,3 +215,23 @@ Reference targets are validated the same way:
in field "content":
Expected an identifier with only the accepted characters, but got
"bad\"target"
+
+A list ID follows the same rules as a content ID:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "foo.bar",
+ > "source": "csv"
+ > }
+ > ],
+ > "content": []
+ > }
+ > EOF
+ in field "list":
+ while decoding a list:
+ element 0:
+ in field "id":
+ Expected an identifier with only the accepted characters, but got
+ "foo.bar"