diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-19 13:26:42 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-19 13:26:42 +0100 |
| commit | fb75994d097cb1d6a1a0ea871c2d2213aa4e3c31 (patch) | |
| tree | 2e783d1472971c1823ec6e7a9cb484ebda2b0b43 | |
| parent | d1047791e9e25bc5a30ffc62f31c3d898843f042 (diff) | |
Path over contents evaluation
| -rw-r--r-- | lib/configuration/tests/path.t | 161 | ||||
| -rw-r--r-- | lib/model/model.ml | 1 | ||||
| -rw-r--r-- | lib/model/model.mli | 5 | ||||
| -rw-r--r-- | lib/model/path.ml | 61 | ||||
| -rw-r--r-- | lib/model/path.mli | 35 | ||||
| -rw-r--r-- | test/path.ml | 65 | ||||
| -rw-r--r-- | test/tests.ml | 1 |
7 files changed, 329 insertions, 0 deletions
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 <<EOF + > { + > "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 <<EOF + > { + > "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 <<EOF + > { + > "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 <<EOF + > { + > "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 <<EOF + > { "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 *) diff --git a/test/path.ml b/test/path.ml new file mode 100644 index 0000000..465606e --- /dev/null +++ b/test/path.ml @@ -0,0 +1,65 @@ +open StdLabels + +let name = __FILE__ +let description = "Resolve a path from a content and display the target" +let speclist () = [] + +type t = (string list, Configuration.Read_conf.error) result + +let pp : Format.formatter -> t -> unit = + Format.pp_print_result ~error:Configuration.Read_conf.pp_error + ~ok: + (Format.pp_print_list ~pp_sep:Format.pp_print_newline + Format.pp_print_string) + +let ( let+ ) opt cont = Result.map cont opt + +let string_of_target : Model.target -> string = function + | Model.Reference.Content ref -> + "content " + ^ Model.Identifier.to_string (Model.Content.id (Model.Reference.get ref)) + | Model.Reference.List list -> + "list " ^ Model.Identifier.to_string list.Model.DataList.id + +let describe_field : Model.field -> string = + fun field -> + let name = Model.Identifier.to_string (Model.Field.name field) in + let (Model.Field.T ty) = Model.Field.type_ field in + match ty with + | Model.Typ.Text -> "field " ^ name ^ " (text)" + | Model.Typ.Integer -> "field " ^ name ^ " (integer)" + | Model.Typ.MonoReference reference -> + "field " ^ name ^ " (reference -> " + ^ string_of_target reference.Model.Typ.t + ^ ")" + | Model.Typ.MultiReference reference -> + "field " ^ name ^ " (multireference -> " + ^ string_of_target reference.Model.Typ.t + ^ ")" + +let exec : string list -> t = + fun args -> + let stdin = In_channel.input_all stdin in + let+ configuration = Configuration.Read_conf.read_string_conf stdin in + (* Anonymous arguments arrive reversed: [content; path] *) + match List.rev args with + | [] -> [ "missing argument: <content> <path>" ] + | content_name :: path -> + let path = + String.concat ~sep:"." path + |> String.split_on_char ~sep:'.' + |> List.map ~f:Model.Identifier.of_string + in + let id = Model.Identifier.of_string content_name in + let result = + match Configuration.Ast.get_content configuration id with + | None -> "unknown content " ^ content_name + | Some content -> ( + match Model.Path.resolve_from_content content path with + | None -> + "unresolved path " + ^ String.concat ~sep:"." + (List.map ~f:Model.Identifier.to_string path) + | Some field -> describe_field field) + in + [ result ] diff --git a/test/tests.ml b/test/tests.ml index 15fb51e..c6ab006 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -35,6 +35,7 @@ let () = register (module Configuration_); register (module Insert); register (module Insert_list); + register (module Path); register (module Update); register (module Identifier); ] |
