From 8708171877054c98aad45079d85d4e699420f2fe Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 19 Nov 2026 11:32:16 +0100 Subject: Added fields in lists --- lib/configuration/read_conf.ml | 89 ++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 37 deletions(-) (limited to 'lib/configuration/read_conf.ml') 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 -- cgit v1.2.3