@@ -25,8 +25,12 @@ open DT
25
25
(* Names of the modules we're going to generate (use these to prevent typos) *)
26
26
let _dm_to_string = " DM_to_String"
27
27
28
+ let _dm_to_field = " DM_to_Field"
29
+
28
30
let _string_to_dm = " String_to_DM"
29
31
32
+ let _field_to_dm = " Field_to_DM"
33
+
30
34
let _db_action = " DB_Action"
31
35
32
36
let _db_defaults = " DB_DEFAULTS"
@@ -109,6 +113,44 @@ let dm_to_string tys : O.Module.t =
109
113
~elements: (List. map (fun ty -> O.Module. Let (ty_fun ty)) tys)
110
114
()
111
115
116
+ let dm_to_field tys : O.Module.t =
117
+ let tys = List. filter type_marshalled_in_db tys in
118
+ (* For every type, we create a single function *)
119
+ let ty_fun ty =
120
+ let body =
121
+ match ty with
122
+ | DT. Map (String, String) ->
123
+ " Schema.Value.pairs"
124
+ | DT. Map (key , value ) ->
125
+ Printf. sprintf
126
+ " fun s -> s |> List.map (fun (k, v) -> %s.%s k, %s.%s v) |> \
127
+ Schema.Value.pairs"
128
+ _dm_to_string (OU. alias_of_ty key) _dm_to_string
129
+ (OU. alias_of_ty value)
130
+ | DT. Set String ->
131
+ " Schema.Value.set"
132
+ | DT. Set ty ->
133
+ Printf. sprintf " fun s -> s |> List.map %s.%s |> Schema.Value.set"
134
+ _dm_to_string (OU. alias_of_ty ty)
135
+ | DT. String ->
136
+ " Schema.Value.string"
137
+ | _ ->
138
+ Printf. sprintf " fun s -> s |> %s.%s |> Schema.Value.string"
139
+ _dm_to_string (OU. alias_of_ty ty)
140
+ in
141
+ O.Let. make ~name: (OU. alias_of_ty ty) ~params: [] ~ty: " Db_interface.field_in"
142
+ ~body: [body] ()
143
+ in
144
+ O.Module. make ~name: _dm_to_field
145
+ ~preamble:
146
+ [
147
+ " exception StringEnumTypeError of string"
148
+ ; " exception DateTimeError of string"
149
+ ]
150
+ ~letrec: true
151
+ ~elements: (List. map (fun ty -> O.Module. Let (ty_fun ty)) tys)
152
+ ()
153
+
112
154
(* * Generate a module of string to datamodel type unmarshalling functions *)
113
155
let string_to_dm tys : O.Module.t =
114
156
let tys = List. filter type_marshalled_in_db tys in
@@ -171,6 +213,53 @@ let string_to_dm tys : O.Module.t =
171
213
~elements: (List. map (fun ty -> O.Module. Let (ty_fun ty)) tys)
172
214
()
173
215
216
+ let field_to_dm tys : O.Module.t =
217
+ let tys = List. filter type_marshalled_in_db tys in
218
+ (* For every type, we create a single function *)
219
+ let ty_fun ty =
220
+ let name = OU. alias_of_ty ty in
221
+ let body =
222
+ match ty with
223
+ | DT. Map (key , value ) ->
224
+ let conv =
225
+ match (key, value) with
226
+ | DT. String , DT. String ->
227
+ " "
228
+ | _ ->
229
+ Printf. sprintf " |> List.map (fun (k, v) -> %s.%s k, %s.%s v)"
230
+ _string_to_dm (OU. alias_of_ty key) _string_to_dm
231
+ (OU. alias_of_ty value)
232
+ in
233
+ " fun s -> s |> Schema.CachedValue.maybe_unmarshal Schema.Type.Pairs \
234
+ |> Schema.CachedValue.value_of |> Schema.Value.Unsafe_cast.pairs"
235
+ ^ conv
236
+ | DT. Set ty ->
237
+ let conv =
238
+ match ty with
239
+ | DT. String ->
240
+ " "
241
+ | _ ->
242
+ Printf. sprintf " |> List.map %s.%s" _string_to_dm
243
+ (OU. alias_of_ty ty)
244
+ in
245
+ " fun s -> s |> Schema.CachedValue.maybe_unmarshal Schema.Type.Set |> \
246
+ Schema.CachedValue.value_of |> Schema.Value.Unsafe_cast.set"
247
+ ^ conv
248
+ | DT. String ->
249
+ " fun s -> s |> Schema.CachedValue.maybe_unmarshal Schema.Type.String \
250
+ |> Schema.CachedValue.value_of |> Schema.Value.Unsafe_cast.string"
251
+ | _ ->
252
+ Printf. sprintf " fun f -> f |> Schema.CachedValue.string_of |> %s.%s"
253
+ _string_to_dm name
254
+ in
255
+ O.Let. make ~name ~params: [] ~ty: (OU. alias_of_ty ty) ~body: [body] ()
256
+ in
257
+ O.Module. make ~name: _field_to_dm
258
+ ~preamble: [" exception StringEnumTypeError of string" ]
259
+ ~letrec: true
260
+ ~elements: (List. map (fun ty -> O.Module. Let (ty_fun ty)) tys)
261
+ ()
262
+
174
263
(* * True if a field is actually in this table, false if stored elsewhere
175
264
(ie Set(Ref _) are stored in foreign tables *)
176
265
let field_in_this_table = function
@@ -283,7 +372,7 @@ let open_db_module =
283
372
[
284
373
" let __t = Context.database_of __context in"
285
374
; " let module DB = (val (Xapi_database.Db_cache.get __t) : \
286
- Xapi_database.Db_interface.DB_ACCESS ) in"
375
+ Xapi_database.Db_interface.DB_ACCESS2 ) in"
287
376
]
288
377
289
378
let db_action api : O.Module.t =
@@ -331,7 +420,7 @@ let db_action api : O.Module.t =
331
420
let ty_alias = OU. alias_of_ty f.DT. ty in
332
421
let accessor = " find_regular" in
333
422
let field_name = Escaping. escape_id f.full_name in
334
- Printf. sprintf {|% s.% s (% s " %s" )| } _string_to_dm ty_alias accessor
423
+ Printf. sprintf {|% s.% s (% s " %s" )| } _field_to_dm ty_alias accessor
335
424
field_name
336
425
in
337
426
let make_field f =
@@ -433,8 +522,13 @@ let db_action api : O.Module.t =
433
522
let to_string arg =
434
523
let binding = O. string_of_param arg in
435
524
let converter = O. type_of_param arg in
436
- Printf. sprintf " let %s = %s.%s %s in" binding _dm_to_string converter
437
- binding
525
+ Printf. sprintf " let %s = %s.%s %s in" binding
526
+ ( if binding = Client. _self || binding = " ref" then
527
+ _dm_to_string
528
+ else
529
+ _dm_to_field
530
+ )
531
+ converter binding
438
532
in
439
533
let body =
440
534
match tag with
@@ -445,37 +539,38 @@ let db_action api : O.Module.t =
445
539
(Escaping. escape_id fld.DT. full_name)
446
540
| FromField (Getter, {DT. ty; full_name; _} ) ->
447
541
Printf. sprintf " %s.%s (DB.read_field __t \" %s\" \" %s\" %s)"
448
- _string_to_dm (OU. alias_of_ty ty)
542
+ _field_to_dm (OU. alias_of_ty ty)
449
543
(Escaping. escape_obj obj.DT. name)
450
544
(Escaping. escape_id full_name)
451
545
Client. _self
452
546
| FromField (Add, {DT. ty = DT. Map (_ , _ ); full_name; _} ) ->
453
547
Printf. sprintf
454
- " DB.process_structured_field __t (%s,%s) \" %s \" \" %s \" %s \
455
- AddMapLegacy"
548
+ " DB.process_structured_field __t (Schema.Value.marshal %s, \
549
+ Schema.Value.marshal %s) \" %s \" \" %s \" %s AddMapLegacy"
456
550
Client. _key Client. _value
457
551
(Escaping. escape_obj obj.DT. name)
458
552
(Escaping. escape_id full_name)
459
553
Client. _self
460
554
| FromField (Add, {DT. ty = DT. Set _ ; full_name; _} ) ->
461
555
Printf. sprintf
462
- " DB.process_structured_field __t (%s,\"\" ) \" %s\" \" %s\" %s AddSet"
556
+ " DB.process_structured_field __t (Schema.Value.marshal %s,\"\" ) \
557
+ \" %s\" \" %s\" %s AddSet"
463
558
Client. _value
464
559
(Escaping. escape_obj obj.DT. name)
465
560
(Escaping. escape_id full_name)
466
561
Client. _self
467
562
| FromField (Remove, {DT. ty = DT. Map (_ , _ ); full_name; _} ) ->
468
563
Printf. sprintf
469
- " DB.process_structured_field __t (%s,\"\" ) \" %s \" \" %s \" %s \
470
- RemoveMap"
564
+ " DB.process_structured_field __t (Schema.Value.marshal %s,\"\" ) \
565
+ \" %s \" \" %s \" %s RemoveMap"
471
566
Client. _key
472
567
(Escaping. escape_obj obj.DT. name)
473
568
(Escaping. escape_id full_name)
474
569
Client. _self
475
570
| FromField (Remove, {DT. ty = DT. Set _ ; full_name; _} ) ->
476
571
Printf. sprintf
477
- " DB.process_structured_field __t (%s,\"\" ) \" %s \" \" %s \" %s \
478
- RemoveSet"
572
+ " DB.process_structured_field __t (Schema.Value.marshal %s,\"\" ) \
573
+ \" %s \" \" %s \" %s RemoveSet"
479
574
Client. _value
480
575
(Escaping. escape_obj obj.DT. name)
481
576
(Escaping. escape_id full_name)
@@ -517,7 +612,9 @@ let db_action api : O.Module.t =
517
612
match (x.msg_params, x.msg_result) with
518
613
| [{param_name= name; _}], Some (result_ty , _ ) ->
519
614
let query =
520
- Printf. sprintf " DB.db_get_by_uuid __t \" %s\" %s"
615
+ Printf. sprintf
616
+ " DB.db_get_by_uuid __t \" %s\" (Schema.Value.Unsafe_cast.string \
617
+ %s)"
521
618
(Escaping. escape_obj obj.DT. name)
522
619
(OU. escape name)
523
620
in
@@ -530,7 +627,7 @@ let db_action api : O.Module.t =
530
627
^ " )"
531
628
in
532
629
let query_opt =
533
- Printf. sprintf " DB.db_get_by_uuid_opt __t \" %s\" %s "
630
+ Printf. sprintf " DB.db_get_by_uuid_opt __t \" %s\" (%s) "
534
631
(Escaping. escape_obj obj.DT. name)
535
632
(OU. escape name)
536
633
in
@@ -555,7 +652,9 @@ let db_action api : O.Module.t =
555
652
match (x.msg_params, x.msg_result) with
556
653
| [{param_name= name; _}], Some (Set result_ty , _ ) ->
557
654
let query =
558
- Printf. sprintf " DB.db_get_by_name_label __t \" %s\" %s"
655
+ Printf. sprintf
656
+ " DB.db_get_by_name_label __t \" %s\" \
657
+ (Schema.Value.Unsafe_cast.string %s)"
559
658
(Escaping. escape_obj obj.DT. name)
560
659
(OU. escape name)
561
660
in
@@ -606,13 +705,15 @@ let db_action api : O.Module.t =
606
705
| FromObject GetAllRecordsWhere ->
607
706
String. concat " \n "
608
707
[
609
- " let expr' = Xapi_database.Db_filter.expr_of_string expr in"
708
+ " let expr' = Xapi_database.Db_filter.expr_of_string \
709
+ (Schema.Value.Unsafe_cast.string expr) in"
610
710
; " get_records_where ~" ^ Gen_common. context ^ " ~expr:expr'"
611
711
]
612
712
| FromObject GetAllWhere ->
613
713
String. concat " \n "
614
714
[
615
- " let expr' = Xapi_database.Db_filter.expr_of_string expr in"
715
+ " let expr' = Xapi_database.Db_filter.expr_of_string \
716
+ (Schema.Value.Unsafe_cast.string expr) in"
616
717
; " get_refs_where ~" ^ Gen_common. context ^ " ~expr:expr'"
617
718
]
618
719
| _ ->
0 commit comments