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 /test/path.ml | |
| parent | d1047791e9e25bc5a30ffc62f31c3d898843f042 (diff) | |
Path over contents evaluation
Diffstat (limited to 'test/path.ml')
| -rw-r--r-- | test/path.ml | 65 |
1 files changed, 65 insertions, 0 deletions
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 ] |
