blob: 374b23bdc4c3665b1f5b0c3ca376b5731605c6c7 (
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
|
(** 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
| Types.MonoReference reference | Types.MultiReference reference ->
Some reference.Types.t
| Types.Text | Types.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
|