summaryrefslogtreecommitdiff
path: root/lib/configuration
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-19 11:32:16 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-19 11:32:16 +0100
commit8708171877054c98aad45079d85d4e699420f2fe (patch)
tree2e580e6891708b7b70d07f7bec03bd0a76ae171b /lib/configuration
parente1bf7686f408b52a870d4ed4f3c565dbbcc3954f (diff)
Added fields in lists
Diffstat (limited to 'lib/configuration')
-rw-r--r--lib/configuration/ast.ml32
-rw-r--r--lib/configuration/ast.mli9
-rw-r--r--lib/configuration/read_conf.ml89
-rw-r--r--lib/configuration/tests/configuration.t55
-rw-r--r--lib/configuration/tests/errors.t103
5 files changed, 227 insertions, 61 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml
index 6d29d0c..edf129f 100644
--- a/lib/configuration/ast.ml
+++ b/lib/configuration/ast.ml
@@ -5,23 +5,27 @@ let escaped = String.escaped
type setting = {
mutable contents : Model.content Splay.t;
- lists : Model.List.t list;
+ mutable lists : Model.list Splay.t;
}
-let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
+let init : unit -> setting =
+ fun () -> { contents = Splay.empty (); lists = Splay.empty () }
(** Get the content from the setting. *)
let get_content : setting -> Model.Identifier.t -> Model.content option =
fun settings name -> Splay.find name settings.contents
(** 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.find_opt setting.lists ~f:(fun l ->
- Model.Identifier.equal l.Model.List.id name)
+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 *)
-let lists : setting -> Model.List.t list = fun setting -> setting.lists
+(** 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)
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
@@ -39,8 +43,12 @@ let register_content : setting -> Model.content -> unit =
in
traverse content
-let register_list : setting -> Model.List.t -> setting =
- fun t l -> { t with lists = l :: t.lists }
+let register_list : setting -> Model.list -> setting =
+ fun setting list ->
+ let () =
+ setting.lists <- Splay.add list.Model.DataList.id list setting.lists
+ in
+ setting
(** Force the evaluation of the field target and ensure it points to an existing
content in the application.
@@ -56,8 +64,8 @@ let get_reference_target : Model.field -> Model.content option =
(* 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 -> (
+ | Model.Reference.List _ -> None
+ | Model.Reference.Content ref -> (
try Some (Model.Reference.get ref)
with BrokenLink _ as e -> raise_notrace e))
diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli
index ce5d1b6..b014c9b 100644
--- a/lib/configuration/ast.mli
+++ b/lib/configuration/ast.mli
@@ -5,10 +5,10 @@ val init : unit -> setting
val get_content : setting -> Model.Identifier.t -> Model.content option
(** Get the content from the setting. *)
-val get_list : setting -> Model.Identifier.t -> Model.List.t option
+val get_list : setting -> Model.Identifier.t -> Model.list option
(** Get the list declared with the given name, if any *)
-val lists : setting -> Model.List.t list
+val lists : setting -> Model.list list
(** All the lists declared in the setting *)
val root_contents : setting -> Model.content list
@@ -25,7 +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 *)
-val register_list : setting -> Model.List.t -> setting
+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
+ reference the lists declared before it. *)
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 c7149ae..14ee0b7 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -29,27 +29,41 @@ module Make (S : Decoders.Decode.S) = struct
| None ->
S.fail "Expected an identifier with only the accepted characters"
- type env = { ty : S.value Type.Id.t; settings : Ast.setting }
+ type env = {
+ ty : S.value Type.Id.t;
+ settings : Ast.setting;
+ allow_content : bool;
+ (** Whether a reference may target a content. References inside a list
+ can only target other lists. *)
+ }
(** The decoders below need the setting being built (to resolve the reference
targets) and a type id (to attribute broken links to this parsing pass in
the error messages). *)
(** 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.
+ 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 =
+ Model.target S.decoder =
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))))
+ | Some list -> S.succeed (Model.Reference.List list)
+ | None -> (
+ if env.allow_content then
+ S.succeed
+ (Model.Reference.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)))))
+ else
+ S.fail_with
+ (Decoders.Error.make ~context:value
+ "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 =
@@ -65,7 +79,7 @@ module Make (S : Decoders.Decode.S) = struct
and* multivaluated = S.field_opt_or ~default:false "multivaluated" S.bool
and* constraints = S.field_opt "constraints" dsl in
let value, name = content_t in
- let t = link env value name in
+ let* t = link env value name in
match multivaluated with
| true -> S.succeed (Model.Field.T (MultiReference { t; constraints }))
| false -> S.succeed (Model.Field.T (MonoReference { t; constraints }))
@@ -108,35 +122,36 @@ module Make (S : Decoders.Decode.S) = struct
in
S.succeed (Model.Content.v id sections subcontents))
- 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 list (env : env) : Model.list S.decoder =
+ (* The list has its own fields, declared directly under "field" — unlike
+ contents, there is no section grouping them. Only the id and the fields
+ are part of the model: where the entries come from is the concern of the
+ data provider, not of the configuration model. *)
let* id = S.field "id" identifier
- and* source = S.field "source" list_source in
- S.succeed Model.List.{ id; source }
+ and* fields = S.field_opt_or ~default:[] "field" (S.list (field env)) in
+ S.succeed Model.DataList.{ id; fields }
let t : Ast.setting S.decoder =
(* Register a type id to attribute broken links to this parsing pass *)
let ty : S.value Type.Id.t = Type.Id.make () in
- (* 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 settings =
- List.fold_left lists ~init:(Ast.init ()) ~f:Ast.register_list
+ let settings = Ast.init () in
+ (* Decode the lists first, registering each one as soon as it is decoded so
+ that a list can reference the lists declared before it. References in a
+ list can only target lists ([allow_content = false]). *)
+ let list_env = { ty; settings; allow_content = false } in
+ let* _lists =
+ S.field_opt_or ~default:[] "list"
+ (S.list
+ (S.and_then
+ (fun list ->
+ let _ = Ast.register_list settings list in
+ S.succeed list)
+ (list list_env)))
in
- let env = { ty; settings } in
+ (* Then the contents: the reference targets are lazy thunks closing over
+ [settings], forced only by [Ast.check] once every content has been
+ registered. *)
+ let env = { ty; settings; allow_content = true } in
let* contents = S.field "content" (S.list (content env)) in
let () = List.iter contents ~f:(Ast.register_content settings) in
diff --git a/lib/configuration/tests/configuration.t b/lib/configuration/tests/configuration.t
index 880e690..5d14bbb 100644
--- a/lib/configuration/tests/configuration.t
+++ b/lib/configuration/tests/configuration.t
@@ -76,20 +76,17 @@ 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:
+Lists are declared in the "list" key. A reference field can target a list,
+like a content:
$ tests --configuration <<EOF
> {
> "list": [
> {
- > "id": "user_type_list",
- > "source": "csv"
+ > "id": "user_type_list"
> },
> {
- > "id": "status_list",
- > "source": { "type": "csv", "file": "status.csv" }
+ > "id": "status_list"
> }
> ],
> "content": [
@@ -119,3 +116,47 @@ field can target a list, like a content:
> }
> EOF
+
+Lists can declare their own fields, directly under the "field" key (there is
+no section in a list). A reference field in a list can target another list:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "country_list",
+ > "field": [
+ > { "name": "name", "label": {}, "type": "text" },
+ > { "name": "code", "label": {}, "type": "text" }
+ > ]
+ > },
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > {
+ > "name": "country",
+ > "label": {},
+ > "type": { "content": "country_list" }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "user_type",
+ > "label": {},
+ > "type": { "content": "user_type_list" }
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+
diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t
index 99ad465..b23f237 100644
--- a/lib/configuration/tests/errors.t
+++ b/lib/configuration/tests/errors.t
@@ -222,8 +222,7 @@ A list ID follows the same rules as a content ID:
> {
> "list": [
> {
- > "id": "foo.bar",
- > "source": "csv"
+ > "id": "foo.bar"
> }
> ],
> "content": []
@@ -235,3 +234,103 @@ A list ID follows the same rules as a content ID:
in field "id":
Expected an identifier with only the accepted characters, but got
"foo.bar"
+
+A reference field in a list can only target another list, not a content:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > {
+ > "name": "entity",
+ > "label": {},
+ > "type": { "content": "entity" }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": [ { "id": "entity" } ]
+ > }
+ > EOF
+ in field "list":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "type":
+ I tried the following decoders but they all failed:
+ "text" decoder:
+ Expected a string, but got { "content": "entity" }
+ "content" decoder:
+ References in a list can only target a list declared before it, but got
+ { "content": "entity" }
+
+A list cannot reference itself:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > {
+ > "name": "parent",
+ > "label": {},
+ > "type": { "content": "user_type_list" }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": []
+ > }
+ > EOF
+ in field "list":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "type":
+ I tried the following decoders but they all failed:
+ "text" decoder:
+ Expected a string, but got { "content": "user_type_list" }
+ "content" decoder:
+ References in a list can only target a list declared before it, but got
+ { "content": "user_type_list" }
+
+A list cannot reference a list declared after it:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "first_list",
+ > "field": [
+ > {
+ > "name": "other",
+ > "label": {},
+ > "type": { "content": "second_list" }
+ > }
+ > ]
+ > },
+ > { "id": "second_list" }
+ > ],
+ > "content": []
+ > }
+ > EOF
+ in field "list":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "type":
+ I tried the following decoders but they all failed:
+ "text" decoder:
+ Expected a string, but got { "content": "second_list" }
+ "content" decoder:
+ References in a list can only target a list declared before it, but got
+ { "content": "second_list" }