package validate

  1. Overview
  2. Docs

Source file field.ml

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
open Ppxlib

type record_field = { name : string; loc_type : loc_type }
and loc_type = { loc : Location.t; typ : simple_type }

and simple_type =
  | Bool
  | Int
  | Float
  | String
  | Option of (simple_type * core_type)
  | Other of longident
  | List of (simple_type * core_type)
  | Tuple of (simple_type * core_type) list

let rec extract_field_type label_loc (t : core_type) =
  let field_type =
    match t.ptyp_desc with
    | Ptyp_constr ({ txt = Lident "string"; _ }, []) -> String
    | Ptyp_constr ({ txt = Lident "int"; _ }, []) -> Int
    | Ptyp_constr ({ txt = Lident "bool"; _ }, []) -> Bool
    | Ptyp_constr ({ txt = Lident "float"; _ }, []) -> Float
    | Ptyp_constr ({ txt = Lident "option"; _ }, [ arg ]) ->
        Option (extract_field_type label_loc arg, arg)
    | Ptyp_constr ({ txt = Lident "list"; _ }, [ arg ]) ->
        List (extract_field_type label_loc arg, arg)
    | Ptyp_constr ({ txt = name; _ }, []) -> Other name
    | Ptyp_tuple inner_types ->
        Tuple
          (List.map
             (fun t ->
               let field_type = extract_field_type label_loc t in
               (field_type, t))
             inner_types)
    | _ -> Location.raise_errorf ~loc:label_loc "Unsupported type"
  in
  field_type

let extract_loc_type (t : core_type) =
  { loc = t.ptyp_loc; typ = extract_field_type t.ptyp_loc t }

let extract_record_field (ld : label_declaration) =
  let name = ld.pld_name.txt in
  let loc_type = extract_loc_type ld.pld_type in
  { name; loc_type }
OCaml

Innovation. Community. Security.