From fb75994d097cb1d6a1a0ea871c2d2213aa4e3c31 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 19 Nov 2026 13:26:42 +0100 Subject: Path over contents evaluation --- lib/configuration/tests/path.t | 161 +++++++++++++++++++++++++++++++++++++++++ lib/model/model.ml | 1 + lib/model/model.mli | 5 ++ lib/model/path.ml | 61 ++++++++++++++++ lib/model/path.mli | 35 +++++++++ 5 files changed, 263 insertions(+) create mode 100644 lib/configuration/tests/path.t create mode 100644 lib/model/path.ml create mode 100644 lib/model/path.mli (limited to 'lib') diff --git a/lib/configuration/tests/path.t b/lib/configuration/tests/path.t new file mode 100644 index 0000000..1532643 --- /dev/null +++ b/lib/configuration/tests/path.t @@ -0,0 +1,161 @@ +Resolve a path from a content: each segment is the name of a reference field, +and the result is the target of the last segment. + + $ tests --path legalEntity entity.employee.country < { + > "list": [ + > { + > "id": "region_list", + > "field": [ { "name": "label", "label": {}, "type": "text" } ] + > }, + > { + > "id": "country_list", + > "field": [ + > { "name": "code", "label": {}, "type": "text" }, + > { + > "name": "region", + > "label": {}, + > "type": { "content": "region_list" } + > } + > ] + > } + > ], + > "content": [ + > { + > "id": "legalEntity", + > "section": [ + > { + > "field": [ + > { + > "name": "entity", + > "label": {}, + > "type": { "content": "entity" } + > } + > ] + > } + > ] + > }, + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { + > "name": "employee", + > "label": {}, + > "type": { "content": "employee" } + > } + > ] + > } + > ] + > }, + > { + > "id": "employee", + > "section": [ + > { + > "field": [ + > { "name": "name", "label": {}, "type": "text" }, + > { + > "name": "country", + > "label": {}, + > "type": { "content": "country_list" } + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + field country (reference -> list country_list) + +Intermediate segments resolve to the targeted content: + + $ tests --path legalEntity entity < { + > "content": [ + > { + > "id": "legalEntity", + > "section": [ + > { + > "field": [ + > { "name": "entity", "label": {}, "type": { "content": "entity" } } + > ] + > } + > ] + > }, + > { "id": "entity" } + > ] + > } + > EOF + field entity (reference -> content entity) + +The path can go through the fields of a list: + + $ tests --path legalEntity entity.employee.country.region < { + > "list": [ + > { "id": "region_list" }, + > { + > "id": "country_list", + > "field": [ + > { "name": "region", "label": {}, "type": { "content": "region_list" } } + > ] + > } + > ], + > "content": [ + > { + > "id": "legalEntity", + > "section": [ { "field": [ + > { "name": "entity", "label": {}, "type": { "content": "entity" } } + > ] } ] + > }, + > { + > "id": "entity", + > "section": [ { "field": [ + > { "name": "employee", "label": {}, "type": { "content": "employee" } } + > ] } ] + > }, + > { + > "id": "employee", + > "section": [ { "field": [ + > { "name": "country", "label": {}, "type": { "content": "country_list" } } + > ] } ] + > } + > ] + > } + > EOF + field region (reference -> list region_list) + +A segment that is not a reference field, or does not exist, does not resolve: + + $ tests --path legalEntity entity.employee.name < { + > "content": [ + > { + > "id": "legalEntity", + > "section": [ { "field": [ + > { "name": "entity", "label": {}, "type": { "content": "entity" } } + > ] } ] + > }, + > { + > "id": "entity", + > "section": [ { "field": [ + > { "name": "employee", "label": {}, "type": { "content": "employee" } } + > ] } ] + > }, + > { + > "id": "employee", + > "section": [ { "field": [ + > { "name": "name", "label": {}, "type": "text" } + > ] } ] + > } + > ] + > } + > EOF + field name (text) + + $ tests --path legalEntity unknown.path < { "content": [ { "id": "legalEntity" } ] } + > EOF + unresolved path unknown.path diff --git a/lib/model/model.ml b/lib/model/model.ml index 20914b1..bd8c010 100644 --- a/lib/model/model.ml +++ b/lib/model/model.ml @@ -7,6 +7,7 @@ module Dsl = Dsl module Typ = Typ module Reference = Reference module DataList = Data_list +module Path = Path module Field = Field module Section = Section module Content = Content diff --git a/lib/model/model.mli b/lib/model/model.mli index e81a844..f51386c 100644 --- a/lib/model/model.mli +++ b/lib/model/model.mli @@ -124,6 +124,11 @@ module DataList = Data_list editable by the user. Lists have their own fields (no sections); a reference field in a list can only target another list. *) +module Path = Path +(** Evaluation of a path through fields: from a content or a list, follow the + fields named by the path (intermediate segments must be reference fields) + and get the final field. *) + module Section = Section (** A group of fields displayed together, with a per-language [label] and an optional [criteria] controlling when the whole section is displayed. *) diff --git a/lib/model/path.ml b/lib/model/path.ml new file mode 100644 index 0000000..dd62864 --- /dev/null +++ b/lib/model/path.ml @@ -0,0 +1,61 @@ +(** Evaluation of a path through the fields. + + A path is a list of field names (as {!Identifier.t}), like the ones parsed + from the DSL expressions ({!Dsl.ContentPath}, written [$a.b.c] in a + configuration). *) + +(** All the fields of a target: the fields of the content, or the fields of the + list *) +let fields_of_target : Reference.target -> Reference.target Field.t list = + function + | Reference.Content ref -> List.of_seq (Content.fields (Reference.get ref)) + | Reference.List list -> list.Data_list.fields + +let find_field : + Reference.target Field.t list -> + Identifier.t -> + Reference.target Field.t option = + fun fields name -> + List.find_opt (fun field -> Identifier.equal (Field.name field) name) fields + +(** The target of a reference field, [None] for the other field types *) +let target_of_field : Reference.target Field.t -> Reference.target option = + fun field -> + let (Field.T ty) = Field.type_ field in + match ty with + | Typ.MonoReference reference | Typ.MultiReference reference -> + Some reference.Typ.t + | Typ.Text | Typ.Integer -> None + +(** Resolve a path from a target. + + Each segment is the name of a field of the current target (a content or a + list). Intermediate segments must be reference fields: the evaluation then + moves to their target. The last segment can be any field. + + The result is the field designated by the last segment — for a reference + field, its target is given by {!target_of_field}. + + [None] when a segment does not exist, or an intermediate segment is not a + reference field. *) +let rec resolve : + Reference.target -> Identifier.t list -> Reference.target Field.t option = + fun target path -> + match path with + | [] -> None + | [ name ] -> find_field (fields_of_target target) name + | name :: rest -> ( + match find_field (fields_of_target target) name with + | None -> None + | Some field -> ( + match target_of_field field with + | Some next -> resolve next rest + | None -> None)) + +(** Resolve a path starting from a content *) +let resolve_from_content : + Reference.target Content.t -> + Identifier.t list -> + Reference.target Field.t option = + fun content path -> + resolve (Reference.Content (Reference.v (fun () -> content))) path diff --git a/lib/model/path.mli b/lib/model/path.mli new file mode 100644 index 0000000..50fd1b0 --- /dev/null +++ b/lib/model/path.mli @@ -0,0 +1,35 @@ +(** Evaluation of a path through the fields. + + A path is a list of field names ({!Identifier.t}), like the ones parsed from + the DSL expressions ({!Dsl.ContentPath}, written [$a.b.c] in a + configuration). + + Example: from a content [legalEntity], the path [entity; employee; country] + follows the reference field [entity], then the reference field [employee] of + its target, and resolves to the field [country] of the [employee] content — + whose target is the list [country_list]. A path can also end on a + non-reference field, e.g. [entity; country; code] resolves to the text field + [code] of the [country_list] list, usable in a rule like + [$entity.country.code = 'FR']. *) + +val resolve : + Reference.target -> Identifier.t list -> Reference.target Field.t option +(** Resolve a path from a target (a content or a list). + + Each segment is the name of a field of the current target. Intermediate + segments must be reference fields: the evaluation moves to their target. The + last segment can be any field. + + The result is the field designated by the last segment. [None] when a + segment does not exist, or an intermediate segment is not a reference field. +*) + +val resolve_from_content : + Reference.target Content.t -> + Identifier.t list -> + Reference.target Field.t option +(** Resolve a path starting from a content *) + +val target_of_field : Reference.target Field.t -> Reference.target option +(** The target of a reference field (the content or list it points to), [None] + for the other field types *) -- cgit v1.2.3