Source file dropbox_json.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(* This module gathers parsing functions to supplement atdgen. *)

module Date = Dropbox_date

let float_of_json = function
  | `Float f -> f
  | `Int i -> float i

module Photo = struct
    type info = { time_taken: Date.t option;
                  lat_long: (float * float) option }

    type t = [ `None | `Pending | `Some of info ]

    let set_info time_taken lat_long = function
      | ("time_taken", `String d) ->
         time_taken := Some(Date.of_string d)
      | ("lat_long", (`List [(`Float _ | `Int _) as la;
                             (`Float _ | `Int _) as lo]
                     | `Tuple [(`Float _ | `Int _) as la;
                               (`Float _ | `Int _) as lo])) ->
         lat_long := Some (float_of_json la, float_of_json lo)
      | _ -> ()

    let wrap : Yojson.Safe.t -> t = function
      | `String _ -> `Pending
      | `Assoc l ->
         let time_taken = ref None in
         let lat_long = ref None in
         List.iter (set_info time_taken lat_long) l;
         `Some { time_taken = !time_taken;  lat_long = !lat_long }
      | _ -> `None

    let unwrap : t -> Yojson.Safe.t = function
      | `None -> `Null
      | `Pending -> `String "pending"
      | `Some info ->
         let l = match info.time_taken with
           | Some d -> ["time_taken", `String(Date.to_string d)]
           | None -> [] in
         let l = match info.lat_long with
           | Some(la, lo) -> ("lat_long", `List [`Float la; `Float lo]) :: l
           | None -> l in
         `Assoc l
  end

module Video = struct
    type info = { time_taken: Date.t option;
                  duration: float option;
                  lat_long: (float * float) option }

    type t = [ `None | `Pending | `Some of info ]

    let set_info time_taken duration lat_long = function
      | ("time_taken", `String d) ->
         time_taken := Some(Date.of_string d)
      | ("duration", (`Float _ | `Int _ as d)) ->
         duration := Some(float_of_json d)
      | ("lat_long", (`List [(`Float _ | `Int _) as la;
                             (`Float _ | `Int _) as lo]
                     | `Tuple [(`Float _ | `Int _) as la;
                               (`Float _ | `Int _) as lo])) ->
         lat_long := Some (float_of_json la, float_of_json lo)
      | _ -> ()

    let wrap : Yojson.Safe.t -> t = function
      | `String _ -> `Pending
      | `Assoc l ->
         let time_taken = ref None in
         let duration = ref None in
         let lat_long = ref None in
         List.iter (set_info time_taken duration lat_long) l;
         `Some { time_taken = !time_taken;
                 duration = !duration;
                 lat_long = !lat_long }
      | _ -> `None

    let unwrap : t -> Yojson.Safe.t = function
      | `None -> `Null
      | `Pending -> `String "pending"
      | `Some info ->
         let l = match info.time_taken with
           | Some d -> ["time_taken", `String(Date.to_string d)]
           | None -> [] in
         let l = match info.duration with
           | Some d -> ("duration", `Float d) :: l
           | None -> l in
         let l = match info.lat_long with
           | Some(la, lo) -> ("lat_long", `List [`Float la; `Float lo]) :: l
           | None -> l in
         `Assoc l
  end

module Visibility = struct
  type t = [
    | `Public
    | `Team_only
    | `Password
    | `Team_and_password
    | `Shared_folder_only
    | `Other of string
    ]

  let wrap : Yojson.Safe.t -> t = function
    | `String "PUBLIC" -> `Public
    | `String "TEAM_ONLY" -> `Team_only
    | `String "PASSWORD" -> `Password
    | `String "TEAM_AND_PASSWORD" -> `Team_and_password
    | `String "SHARED_FOLDER_ONLY" -> `Shared_folder_only
    | `String s -> `Other s
    | _ -> Yojson.json_error "Visibility of shared link is not a string"

  let unwrap : t -> Yojson.Safe.t = function
    | `Public -> `String "PUBLIC"
    | `Team_only -> `String "TEAM_ONLY"
    | `Password -> `String "PASSWORD"
    | `Team_and_password -> `String "TEAM_AND_PASSWORD"
    | `Shared_folder_only -> `String "SHARED_FOLDER_ONLY"
    | `Other s -> `String s
end