package archetype

  1. Overview
  2. Docs

Source file gen_api_storage.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
open Tools
open Location
open Ident
open Model

exception Anomaly of string
type error_desc =
  | TODO
[@@deriving show {with_path = false}]


let process_api_storage (model : model) : model =

  let add (ctx : ctx_model) (l : api_item list) (i :  api_item) =
    let item = { i with only_formula = ctx.formula } in
    let res, l = List.fold_left (fun (res, accu) (x : api_item) ->
        if cmp_api_item_node x.node_item i.node_item
        then (true, { item with only_formula = x.only_formula && ctx.formula }::accu)
        else (res, x::accu)) (false, []) l in
    if res then
      l
    else
      item::l
  in

  let rec f (ctx : ctx_model) (accu : api_item list) (term : mterm) : api_item list =
    let accu = fold_term (f ctx) accu term in
    let api_items : api_item_node list =
      match term.node with
      | Mget (asset_name, _) ->
        [APIStorage (Get asset_name)]
      | Mset (asset_name, _, _, _) ->
        [APIStorage (Set asset_name)]
      | Maddasset (asset_name, _) ->
        [APIStorage (Add asset_name)]
      | Maddfield (asset_name, field_name, _, _) ->
        let (pa,_,_) = Utils.get_partition_asset_key model (dumloc asset_name) (dumloc field_name) in
        [APIStorage (Add pa); APIStorage (UpdateAdd (asset_name, field_name))]
      | Mremoveasset (asset_name, _) ->
        [APIStorage (Remove asset_name)]
      | Mremovefield (asset_name, field_name, _, _) ->
        let (pa,_,_) = Utils.get_partition_asset_key model (dumloc asset_name) (dumloc field_name) in
        [APIStorage (Remove pa);APIStorage (UpdateRemove (asset_name, field_name))]
      | Mclearasset (asset_name) ->
        [APIStorage (Clear asset_name)]
      | Mclearfield (asset_name, field_name, _) ->
        [APIStorage (UpdateClear (asset_name, field_name))]
      | Mreverseasset (asset_name) ->
        [APIStorage (Reverse asset_name)]
      | Mreversefield (asset_name, field_name, _) ->
        [APIStorage (UpdateReverse (asset_name, field_name))]
      | Mselect (asset_name, _, p) ->
        [APIStorage (Get asset_name); APIFunction (Select (asset_name, p))]
      | Msort (asset_name, _, field_name, _) ->
        [APIFunction (Sort (asset_name, field_name))]
      | Mcontains (asset_name, _, _) ->
        [APIFunction (Contains asset_name)]
      | Mnth (asset_name, _, _) ->
        [APIStorage (Get asset_name); APIFunction (Nth asset_name)]
      | Mcount (asset_name, _) ->
        [APIFunction (Count asset_name)]
      | Msum (asset_name, field_name, _) ->
        [APIFunction (Sum (asset_name, unloc field_name))]
      | Mmin (asset_name, field_name, _) ->
        [APIFunction (Min (asset_name, unloc field_name))]
      | Mmax (asset_name, field_name, _) ->
        [APIFunction (Max (asset_name, unloc field_name))]
      | Mshallow (asset_name, _) ->
        [APIFunction (Shallow asset_name)]
      | Munshallow (asset_name, _) ->
        [APIFunction (Unshallow asset_name)]
      | Mlisttocoll (asset_name, _) ->
        [APIFunction (Listtocoll asset_name)]
      | Mhead (asset_name, _, _) ->
        [APIFunction (Head asset_name)]
      | Mtail (asset_name, _, _) ->
        [APIFunction (Tail asset_name)]
      | _ -> []
    in
    List.fold_left (fun accu v -> add ctx accu (Model.mk_api_item v)) accu api_items
  in
  let l = fold_model f model []
          |> List.sort
            (fun (i1 : api_item) (i2 : api_item) ->
               let criteria_asset_name () : int =
                 let default = "_" in
                 let get_asset_name = function
                   | APIStorage (Get              an)     -> an
                   | APIStorage (Set              an)     -> an
                   | APIStorage (Add              an)     -> an
                   | APIStorage (Remove           an)     -> an
                   | APIStorage (Clear            an)     -> an
                   | APIStorage (Reverse          an)     -> an
                   | APIStorage (UpdateAdd       (an, _)) -> an
                   | APIStorage (UpdateRemove    (an, _)) -> an
                   | APIStorage (UpdateClear     (an, _)) -> an
                   | APIStorage (UpdateReverse   (an, _)) -> an
                   | APIStorage (ToKeys           an)     -> an
                   | APIFunction (Select         (an, _)) -> an
                   | APIFunction (Sort           (an, _)) -> an
                   | APIFunction (Contains        an)     -> an
                   | APIFunction (Nth             an)     -> an
                   | APIFunction (Count           an)     -> an
                   | APIFunction (Sum            (an, _)) -> an
                   | APIFunction (Min            (an, _)) -> an
                   | APIFunction (Max            (an, _)) -> an
                   | APIContainer _                       -> default
                   | APIBuiltin _                         -> default
                   | APIFunction (Shallow         an)     -> an
                   | APIFunction (Unshallow       an)     -> an
                   | APIFunction (Listtocoll      an)     -> an
                   | APIFunction (Head            an)     -> an
                   | APIFunction (Tail            an)     -> an
                 in
                 let asset_list : ident list = List.fold_left (fun accu (x : decl_node) ->
                     match x with
                     | Drecord r -> accu @ [unloc r.name]
                     | _ -> accu
                   ) [] model.decls in
                 let get_idx (i : api_item) = List.index_of (fun x -> String.equal (get_asset_name i.node_item) x) asset_list in
                 let idx1 = get_idx i1 in
                 let idx2 = get_idx i2 in
                 idx1 - idx2
               in

               let criteria_kind () : int =
                 let get_kind = function
                   | APIFunction  (Nth           _) ->  1
                   | APIFunction  (Count         _) ->  2
                   | APIFunction  (Sum           _) ->  3
                   | APIFunction  (Min           _) ->  4
                   | APIFunction  (Max           _) ->  5
                   | APIStorage   (Get           _) ->  6
                   | APIStorage   (Set           _) ->  7
                   | APIStorage   (Add           _) ->  8
                   | APIStorage   (Remove        _) ->  9
                   | APIStorage   (Clear         _) -> 10
                   | APIStorage   (Reverse       _) -> 11
                   | APIStorage   (UpdateAdd     _) -> 12
                   | APIStorage   (UpdateRemove  _) -> 13
                   | APIStorage   (UpdateClear   _) -> 14
                   | APIStorage   (UpdateReverse _) -> 15
                   | APIStorage   (ToKeys        _) -> 16
                   | APIFunction  (Select        _) -> 17
                   | APIFunction  (Sort          _) -> 18
                   | APIFunction  (Contains      _) -> 19
                   | APIContainer (AddItem       _) -> 20
                   | APIContainer (RemoveItem    _) -> 21
                   | APIContainer (ClearItem     _) -> 22
                   | APIContainer (ReverseItem   _) -> 23
                   | APIBuiltin   (MinBuiltin    _) -> 24
                   | APIBuiltin   (MaxBuiltin    _) -> 25
                   | APIFunction  (Shallow       _) -> 26
                   | APIFunction  (Unshallow     _) -> 27
                   | APIFunction  (Listtocoll    _) -> 28
                   | APIFunction  (Head          _) -> 29
                   | APIFunction  (Tail          _) -> 30
                 in
                 let idx1 = get_kind i1.node_item in
                 let idx2 = get_kind i2.node_item in
                 idx1 - idx2
               in

               let c1 = criteria_asset_name () in
               if c1 = 0
               then criteria_kind ()
               else c1
            )
  in
  { model with api_items = l }

let generate_api_storage (model : model) : model =
  model
  |> process_api_storage