summaryrefslogtreecommitdiff
path: root/test/path.ml
blob: 73daf4a33f0097035846dc202360cfa5caacb395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 = Common.read_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 ]