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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533 | --
-- Copyright (C) 2022, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
with JSON_Schema.Writers.Inputs;
with JSON_Schema.Writers.Outputs;
package body JSON_Schema.Writers.Types is
procedure Write_Named_Types
(Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set);
-- Generate types for all named schemas in Map if they not present in Done
-- already. Include names of generated type in Done set.
procedure Write_Named_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set);
-- Generate a type for single named Schema. Generate Optional_ type if
-- corresponding type is included in Optional_Types set. Update Done as
-- described above.
procedure Write_Anonymous_Type
(Enclosing_Type : Schema_Name;
Property : JSON_Schema.Property;
Map : JSON_Schema.Readers.Schema_Map;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set;
Required : Boolean;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector);
-- Write a dedicated type for a schema property that contains a nested
-- schema. Currently only single level of nesting is implemented.
procedure Write_Record_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set);
-- The same for "type: object" schema
procedure Write_Record_Component
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Property : JSON_Schema.Property;
Required : Boolean;
Is_Holder : Boolean);
-- Write record component declaration for given Property. Name is a name of
-- the schema containing the property.
procedure Write_Derived_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set);
-- The same for "allOf:[]" schema
procedure Write_Union_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set);
-- The same for "anyOf:[]" schema
procedure Write_Enumeration_Type
(Name : VSS.Strings.Virtual_String;
Schema : Schema_Access);
-- The same for enumeration schema
procedure Write_Union_Variant_Type
(Map : JSON_Schema.Readers.Schema_Map;
Name : VSS.Strings.Virtual_String;
Schema : Schema_Access);
-- Write enumeration type for variant in anyOf schema
procedure Write_Public_Vectors (Array_Types : Readers.Schema_Map);
-- Write vector type public declarations for each item of Array_Types
procedure Write_Vector_Operations
(Array_Types : Readers.Schema_Map;
Kind : Declaration_Kind);
-- Write vector type operations for each item of Array_Types
procedure Write_Private_Vectors
(Array_Types : Readers.Schema_Map;
Enum_Package : VSS.Strings.Virtual_String);
-- Write vector type private declarations for each item of Array_Types
procedure Write_Public_Holders
(Map : JSON_Schema.Readers.Schema_Map;
Holders : VSS.String_Vectors.Virtual_String_Vector);
-- Write holder types
procedure Write_Holders_Operations
(Map : JSON_Schema.Readers.Schema_Map;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Kind : Declaration_Kind);
procedure Write_Privete_Holders
(Map : JSON_Schema.Readers.Schema_Map;
Holders : VSS.String_Vectors.Virtual_String_Vector);
-- Write holder private (full) types
procedure Find_Optional_Types
(Schema : Schema_Access;
Result : in out String_Sets.Set);
-- Scan Schema recursively and find all types that are referenced in
-- optional properties.
procedure Find_Array_Types
(Map : JSON_Schema.Readers.Schema_Map;
Schema : Schema_Access;
Result : in out Readers.Schema_Map);
-- Scan Schema recursively and find all types that are referenced as items
-- in an array schema.
procedure Write_Comment
(Description : VSS.Strings.Virtual_String;
Indent : Natural);
-- Write Description as an Ada comment (handle LF if any).
-- Use Indent spaces before comment markers.
procedure Write_Optional_Type (Name : Schema_Name);
-- Write an Optional_<Name> type for given type Name
procedure Write_Any_Object (Name : VSS.Strings.Virtual_String);
-- Write an Ada type that represents an unrestricted object
procedure Write_Type_Package
(Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Header : VSS.String_Vectors.Virtual_String_Vector;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Array_Types : Readers.Schema_Map);
-- Write package specification with type declarations
function Is_Enum (Schema : Schema_Access) return Boolean is
(not Schema.Enum.Is_Empty);
-- Check for enumeration schema
function Field_Type
(Map : JSON_Schema.Readers.Schema_Map;
Schema : Schema_Access;
Required : Boolean;
Fallback : VSS.Strings.Virtual_String) return VSS.Strings.Virtual_String;
-- Return an Ada type name for given Schema. Fallback if a type name for
-- properties with nested schema declaration.
-- Return an empty string for string properties with just one enumeration
-- literal defined, so we can skip such properties in the type declaration.
----------------
-- Field_Type --
----------------
function Field_Type
(Map : JSON_Schema.Readers.Schema_Map;
Schema : Schema_Access;
Required : Boolean;
Fallback : VSS.Strings.Virtual_String) return VSS.Strings.Virtual_String
is
Name : VSS.Strings.Virtual_String;
Prefix : VSS.Strings.Virtual_String;
begin
Get_Field_Type (Map, Schema, Required, Fallback, Name, Prefix);
Prefix.Append (Name);
return Prefix;
end Field_Type;
----------------------
-- Find_Array_Types --
----------------------
procedure Find_Array_Types
(Map : JSON_Schema.Readers.Schema_Map;
Schema : Schema_Access;
Result : in out Readers.Schema_Map)
is
use type Definitions.Simple_Types;
Ref : Schema_Name;
begin
if Schema.Kind.Last_Index = 1
and then Schema.Kind (1) = Definitions.An_Array
and then not Schema.Items.First_Element.Ref.Is_Empty
then
pragma Assert (Schema.Items.Last_Index = 1);
Ref := Schema.Items.First_Element.Ref;
Result.Include (Ref_To_Type_Name (Ref), Map (Ref));
end if;
for Item of Schema.All_Of loop
if Item.Ref.Is_Empty then
Find_Array_Types (Map, Item, Result);
end if;
end loop;
for Property of Schema.Properties loop
Find_Array_Types (Map, Property.Schema, Result);
end loop;
end Find_Array_Types;
-------------------------
-- Find_Optional_Types --
-------------------------
procedure Find_Optional_Types
(Schema : Schema_Access;
Result : in out String_Sets.Set)
is
procedure Append_Optional (Schema : Schema_Access);
---------------------
-- Append_Optional --
---------------------
procedure Append_Optional (Schema : Schema_Access) is
begin
if not Schema.Ref.Is_Empty then
Result.Include (Ref_To_Type_Name (Schema.Ref));
end if;
end Append_Optional;
begin
for Item of Schema.All_Of loop
if Item.Ref.Is_Empty then
Find_Optional_Types (Item, Result);
end if;
end loop;
for Property of Schema.Properties loop
if not Schema.Required.Contains (Property.Name) then
Append_Optional (Property.Schema);
end if;
Find_Optional_Types (Property.Schema, Result);
end loop;
end Find_Optional_Types;
-----------
-- Write --
-----------
procedure Write
(Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Header : VSS.String_Vectors.Virtual_String_Vector;
Holders : VSS.String_Vectors.Virtual_String_Vector)
is
Optional_Types : String_Sets.Set;
Array_Types : Readers.Schema_Map;
begin
for Schema of Map loop
Find_Optional_Types (Schema, Optional_Types);
Find_Array_Types (Map, Schema, Array_Types);
end loop;
Array_Types.Insert ("Integer", null);
Write_Type_Package
(Map, Root_Package, Enum_Package, Header, Holders,
Optional_Types, Array_Types);
JSON_Schema.Writers.Outputs.Generate_Writers
(Map, Root_Package, Enum_Package, Header, Holders, Optional_Types);
JSON_Schema.Writers.Inputs.Generate_Readers
(Map, Root_Package, Enum_Package, Header, Holders, Optional_Types);
end Write;
--------------------------
-- Write_Anonymous_Type --
--------------------------
procedure Write_Anonymous_Type
(Enclosing_Type : Schema_Name;
Property : JSON_Schema.Property;
Map : JSON_Schema.Readers.Schema_Map;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set;
Required : Boolean;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector)
is
use type VSS.Strings.Virtual_String;
Type_Name : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Enclosing_Type)
& "_" & Property.Name;
begin
-- Write dependencies
for Item of Property.Schema.Properties loop
if not Item.Schema.Ref.Is_Empty then
Write_Named_Type
(Item.Schema.Ref,
Map,
Root_Package,
Enum_Package,
Holders,
Optional_Types,
Done);
end if;
end loop;
Put ("type ");
Put (Type_Name);
Put (" is ");
Put ("record");
New_Line;
for Item of Property.Schema.Properties loop
Write_Record_Component
(Enclosing_Type,
Map,
Root_Package,
Item,
Property.Schema.Required.Contains (Item.Name),
False);
end loop;
Put ("end record;");
New_Line;
New_Line;
if not Required then
Write_Optional_Type (Type_Name);
end if;
end Write_Anonymous_Type;
----------------------
-- Write_Any_Object --
----------------------
procedure Write_Any_Object (Name : VSS.Strings.Virtual_String) is
begin
Put ("type ");
Put (Ref_To_Type_Name (Name));
Put (" is new Any_Object with null record;");
New_Line;
end Write_Any_Object;
-------------------
-- Write_Comment --
-------------------
procedure Write_Comment
(Description : VSS.Strings.Virtual_String;
Indent : Natural) is
begin
if not Description.Is_Empty then
declare
Lines : constant VSS.String_Vectors.Virtual_String_Vector :=
Description.Split_Lines;
begin
for Line in 1 .. Lines.Length loop
for J in 1 .. Indent loop
Put (" ");
end loop;
Put ("-- ");
Put (Lines (Line));
New_Line;
end loop;
end;
end if;
end Write_Comment;
------------------------
-- Write_Derived_Type --
------------------------
procedure Write_Derived_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set)
is
use type VSS.Strings.Virtual_String;
procedure On_Property
(Enclosing : Schema_Name;
Property : JSON_Schema.Property;
Required : Boolean);
-- Generate component declaration for given property
procedure On_Anonymous_Schema (Property : JSON_Schema.Property);
-- Generate anonymous type for given property
-------------------------
-- On_Anonymous_Schema --
-------------------------
procedure On_Anonymous_Schema (Property : JSON_Schema.Property) is
begin
Write_Anonymous_Type
(Name,
Property,
Map,
Optional_Types,
Done,
Property.Schema.Required.Contains (Property.Name),
Root_Package, Enum_Package, Holders);
end On_Anonymous_Schema;
-----------------
-- On_Property --
-----------------
procedure On_Property
(Enclosing : Schema_Name;
Property : JSON_Schema.Property;
Required : Boolean) is
begin
Write_Record_Component
(Enclosing, Map, Root_Package, Property, Required, False);
end On_Property;
Schema : constant Schema_Access := Map (Name);
begin
-- Write dependencies
for Used of Schema.All_Of loop
if not Used.Ref.Is_Empty then
Write_Named_Type
(Used.Ref,
Map,
Root_Package,
Enum_Package,
Holders,
Optional_Types,
Done);
end if;
for Property of Used.Properties loop
if not Property.Schema.Ref.Is_Empty
and then Property.Schema.Ref /= Name
then
Write_Named_Type
(Property.Schema.Ref,
Map,
Root_Package,
Enum_Package,
Holders,
Optional_Types,
Done);
end if;
end loop;
end loop;
Each_Anonymous_Schema (Map, Schema, On_Anonymous_Schema'Access);
pragma Assert (Schema.All_Of.Last_Index = 2);
Put ("type ");
Put (Ref_To_Type_Name (Name));
Put (" is ");
Put ("record");
New_Line;
Writers.Each_Property (Map, Name, Schema, On_Property'Access);
Put ("end record;");
New_Line;
New_Line;
end Write_Derived_Type;
----------------------------
-- Write_Enumeration_Type --
----------------------------
procedure Write_Enumeration_Type
(Name : VSS.Strings.Virtual_String;
Schema : Schema_Access) is
begin
Put ("type ");
Put (Name);
Put (" is (");
for Index in 1 .. Schema.Enum.Length loop
if Index > 1 then
Put (", ");
end if;
Put (Escape_Keywords (Schema.Enum.Element (Index)));
end loop;
Put (");");
New_Line;
New_Line;
end Write_Enumeration_Type;
------------------------------
-- Write_Holders_Operations --
------------------------------
procedure Write_Holders_Operations
(Map : JSON_Schema.Readers.Schema_Map;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Kind : Declaration_Kind)
is
procedure Each (Name : VSS.Strings.Virtual_String);
procedure Each (Name : VSS.Strings.Virtual_String) is
Type_Name : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Name);
begin
Put ("not overriding function Element"); New_Line;
Put ("(Self : aliased in out ");
Put (Type_Name);
Put ("_Holder)"); New_Line;
Put (" return ");
Put (Type_Name);
Put ("_Variable_Reference");
if Kind = Specification then
Put (" with Inline;");
New_Line;
New_Line;
else
Put (" is"); New_Line;
Put ("begin"); New_Line;
Put ("if Self.Length = 0 then"); New_Line;
Put ("Self.Append ((others => <>));"); New_Line;
Put ("end if;"); New_Line;
Put ("return (Element => Self.Data (1)'Access);"); New_Line;
Put ("end Element;"); New_Line;
New_Line;
end if;
Put ("not overriding function Value"); New_Line;
Put ("(Self : aliased ");
Put (Type_Name);
Put ("_Holder)"); New_Line;
Put (" return ");
Put (Type_Name);
Put ("_Constant_Reference");
if Kind = Specification then
Put (" with Inline;");
New_Line;
New_Line;
else
Put (" is (Element => Self.Data (1)'Access);");
New_Line;
New_Line;
end if;
end Each;
begin
Each_Holder_Type (Map, Holders, Each'Access);
end Write_Holders_Operations;
----------------------
-- Write_Named_Type --
----------------------
procedure Write_Named_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set)
is
use type Definitions.Simple_Types;
Schema : constant Schema_Access := Map (Name);
begin
if Done.Contains (Name) then
return;
elsif not Schema.Enum.Is_Empty then
Done.Insert (Name);
Write_Enumeration_Type (Ref_To_Type_Name (Name), Schema);
elsif not Schema.Any_Of.Is_Empty then
Done.Insert (Name);
Write_Union_Type
(Name, Map, Root_Package, Enum_Package, Holders,
Optional_Types, Done);
elsif not Schema.All_Of.Is_Empty then
Done.Insert (Name);
Write_Derived_Type
(Name, Map, Root_Package, Enum_Package, Holders,
Optional_Types, Done);
elsif not Schema.Properties.Is_Empty then
Done.Insert (Name);
Write_Record_Type
(Name, Map, Root_Package, Enum_Package, Holders,
Optional_Types, Done);
elsif Schema.Kind.Last_Index = 1
and then Schema.Kind.First_Element = Definitions.An_Object
then
Done.Insert (Name);
Write_Any_Object (Name);
else
Put ("-- Left:");
Put (Name);
New_Line;
end if;
if Optional_Types.Contains (Ref_To_Type_Name (Name)) then
Write_Optional_Type (Name);
end if;
end Write_Named_Type;
-----------------------
-- Write_Named_Types --
-----------------------
procedure Write_Named_Types
(Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set) is
begin
for Cursor in Map.Iterate loop
declare
Name : constant VSS.Strings.Virtual_String :=
JSON_Schema.Readers.Schema_Maps.Key (Cursor);
begin
Write_Named_Type
(Name, Map, Root_Package, Enum_Package, Holders,
Optional_Types, Done);
end;
end loop;
end Write_Named_Types;
-------------------------
-- Write_Optional_Type --
-------------------------
procedure Write_Optional_Type (Name : Schema_Name) is
Type_Name : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Name);
begin
Put ("type Optional_");
Put (Type_Name);
Put (" (Is_Set : Boolean := False) is record");
New_Line;
Put ("case Is_Set is");
New_Line;
Put ("when True => Value : ");
Put (Type_Name);
Put (";");
New_Line;
Put ("when False => null;");
New_Line;
Put ("end case; end record;");
New_Line;
New_Line;
end Write_Optional_Type;
---------------------------
-- Write_Private_Vectors --
---------------------------
procedure Write_Private_Vectors
(Array_Types : Readers.Schema_Map;
Enum_Package : VSS.Strings.Virtual_String) is
begin
for Cursor in Array_Types.Iterate loop
declare
use type VSS.Strings.Virtual_String;
Item : constant VSS.Strings.Virtual_String :=
Readers.Schema_Maps.Key (Cursor);
Schema : constant Schema_Access :=
Readers.Schema_Maps.Element (Cursor);
Element_Type : constant VSS.Strings.Virtual_String :=
(if Schema /= null
and then Is_Enum (Schema)
and then not Enum_Package.Is_Empty
then Enum_Package & "." & Item
else Item);
begin
Put ("type ");
Put (Item);
Put ("_Array is array (Positive range <>) of aliased ");
Put (Element_Type);
Put (";");
New_Line;
Put ("type ");
Put (Item);
Put ("_Array_Access is access ");
Put (Item);
Put ("_Array;");
New_Line;
Put ("type ");
Put (Item);
Put ("_Vector is new Ada.Finalization.Controlled with record");
New_Line;
Put ("Data : ");
Put (Item);
Put ("_Array_Access;");
New_Line;
Put ("Length : Natural := 0;");
New_Line;
Put ("end record;");
New_Line;
New_Line;
Put ("overriding procedure Adjust (Self : in out ");
Put (Item);
Put ("_Vector);");
New_Line;
New_Line;
Put ("overriding procedure Finalize (Self : in out ");
Put (Item);
Put ("_Vector);");
New_Line;
New_Line;
end;
end loop;
end Write_Private_Vectors;
---------------------------
-- Write_Privete_Holders --
---------------------------
procedure Write_Privete_Holders
(Map : JSON_Schema.Readers.Schema_Map;
Holders : VSS.String_Vectors.Virtual_String_Vector)
is
procedure Each (Name : Schema_Name);
procedure Each (Name : Schema_Name) is
Type_Name : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Name);
begin
Put ("type ");
Put (Type_Name);
Put ("_Holder is new ");
Put (Type_Name);
Put ("_Vector with null record;");
New_Line;
New_Line;
end Each;
begin
Each_Holder_Type (Map, Holders, Each'Access);
end Write_Privete_Holders;
--------------------------
-- Write_Public_Holders --
--------------------------
procedure Write_Public_Holders
(Map : JSON_Schema.Readers.Schema_Map;
Holders : VSS.String_Vectors.Virtual_String_Vector)
is
procedure Each (Name : Schema_Name);
procedure Each (Name : Schema_Name) is
Type_Name : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Name);
begin
Put ("type ");
Put (Type_Name);
Put ("_Holder is tagged private;");
New_Line;
New_Line;
end Each;
begin
Each_Holder_Type (Map, Holders, Each'Access);
end Write_Public_Holders;
--------------------------
-- Write_Public_Vectors --
--------------------------
procedure Write_Public_Vectors (Array_Types : Readers.Schema_Map) is
begin
for Cursor in Array_Types.Iterate loop
declare
Item : constant VSS.Strings.Virtual_String :=
Readers.Schema_Maps.Key (Cursor);
begin
Put ("type ");
Put (Item);
Put ("_Vector is tagged private");
New_Line;
Put ("with Variable_Indexing => Get_");
Put (Item);
Put ("_Variable_Reference,");
New_Line;
Put ("Constant_Indexing => Get_");
Put (Item);
Put ("_Constant_Reference;");
New_Line;
New_Line;
end;
end loop;
end Write_Public_Vectors;
----------------------------
-- Write_Record_Component --
----------------------------
procedure Write_Record_Component
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Property : JSON_Schema.Property;
Required : Boolean;
Is_Holder : Boolean)
is
use type VSS.Strings.Virtual_String;
function Get_Default_Value (Field_Type : VSS.Strings.Virtual_String)
return VSS.Strings.Virtual_String;
-- Return default value for the record component
-----------------------
-- Get_Default_Value --
-----------------------
function Get_Default_Value (Field_Type : VSS.Strings.Virtual_String)
return VSS.Strings.Virtual_String is
begin
if Required then
return VSS.Strings.Empty_Virtual_String;
elsif Field_Type = "Boolean" then
return Field_Type & "'First";
elsif Field_Type = "Integer" then
return "0";
elsif Field_Type = "Float" then
return "0.0";
else
return VSS.Strings.Empty_Virtual_String;
end if;
end Get_Default_Value;
Fallback : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Name) & "_" & Property.Name;
Field_Name : constant VSS.Strings.Virtual_String :=
Escape_Keywords (Property.Name);
Field_Type : VSS.Strings.Virtual_String :=
Writers.Types.Field_Type (Map, Property.Schema, Required, Fallback);
Default : constant VSS.Strings.Virtual_String :=
Get_Default_Value (Field_Type);
begin
if Field_Type.Is_Empty then
-- Skip unneeded properties
return;
elsif Is_Holder then
Field_Type.Append ("_Holder");
elsif Field_Name.To_Lowercase = Field_Type.To_Lowercase then
Field_Type.Prepend (".");
Field_Type.Prepend (Root_Package);
end if;
Put (Field_Name);
Put (" : ");
Put (Field_Type);
if not Default.Is_Empty then
Put (" := ");
Put (Default);
end if;
Put (";");
New_Line;
Write_Comment (Property.Schema.Description, 6);
end Write_Record_Component;
-----------------------
-- Write_Record_Type --
-----------------------
procedure Write_Record_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set)
is
use type VSS.Strings.Virtual_String;
Schema : constant Schema_Access := Map (Name);
Type_Name : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Name);
begin
-- Write dependencies
for Property of Schema.Properties loop
if not Property.Schema.Ref.Is_Empty
and then Property.Schema.Ref /= Name
then
if not Is_Holder_Field (Name, Property.Name, Holders) then
Write_Named_Type
(Property.Schema.Ref,
Map,
Root_Package,
Enum_Package,
Holders,
Optional_Types,
Done);
end if;
end if;
end loop;
Put ("type ");
Put (Type_Name);
Put (" is ");
Put ("record");
New_Line;
for Property of Schema.Properties loop
Write_Record_Component
(Name,
Map,
Root_Package,
Property,
Schema.Required.Contains (Property.Name),
Is_Holder_Field (Name, Property.Name, Holders));
end loop;
Put ("end record;");
New_Line;
New_Line;
end Write_Record_Type;
------------------------
-- Write_Type_Package --
------------------------
procedure Write_Type_Package
(Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Header : VSS.String_Vectors.Virtual_String_Vector;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Array_Types : Readers.Schema_Map)
is
Done : String_Sets.Set;
begin
for Item of Header loop
Put (Item);
New_Line;
end loop;
Put ("with Ada.Containers.Doubly_Linked_Lists;");
New_Line;
Put ("with Ada.Finalization;");
New_Line;
Put ("with VSS.JSON.Streams;");
New_Line;
Put ("with VSS.Strings;");
New_Line;
Put ("with VSS.String_Vectors;");
New_Line;
New_Line;
Put ("package ");
Put (Root_Package);
Put (" is");
New_Line;
Put
("package JSON_Event_Lists is new Ada.Containers.Doubly_Linked_Lists");
New_Line;
Put ("(VSS.JSON.Streams.JSON_Stream_Element, VSS.JSON.Streams.""="");");
New_Line;
New_Line;
Put ("type Any_Value is new JSON_Event_Lists.List with null record;");
New_Line;
Put ("type Any_Object is new Any_Value with null record;");
New_Line;
New_Line;
Write_Optional_Type ("Integer");
Write_Optional_Type ("Float");
Put ("type Integer_Or_String (Is_String : Boolean := False) is record");
New_Line;
Put ("case Is_String is");
New_Line;
Put ("when False =>");
New_Line;
Put ("Integer : Standard.Integer;");
New_Line;
Put ("when True =>");
New_Line;
Put ("String : VSS.Strings.Virtual_String;");
New_Line;
Put ("end case;");
New_Line;
Put ("end record;");
New_Line;
New_Line;
Write_Optional_Type ("Integer_Or_String");
New_Line;
Write_Public_Vectors (Array_Types);
Write_Public_Holders (Map, Holders);
New_Line;
-- Write all enumeration types. Use a nested package to avoid
-- name collisions between enumeration literals and types.
if not Enum_Package.Is_Empty then
Put ("package ");
Put (Enum_Package);
Put (" is");
New_Line;
New_Line;
end if;
declare
procedure On_Enumeration_Type
(Name : Schema_Name;
Property : VSS.Strings.Virtual_String;
Schema : Schema_Access;
Optional : Boolean);
-- Generate enumeration type for Schema
procedure On_Enumeration_Type
(Name : Schema_Name;
Property : VSS.Strings.Virtual_String;
Schema : Schema_Access;
Optional : Boolean)
is
Type_Name : VSS.Strings.Virtual_String := Ref_To_Type_Name (Name);
begin
if Property.Is_Empty then
Done.Include (Name);
end if;
if Schema.Enum.Length > 1 then
if not Property.Is_Empty then
Type_Name.Append ("_");
Type_Name.Append (Property);
end if;
Write_Enumeration_Type (Type_Name, Schema);
if Optional then
Write_Optional_Type (Type_Name);
end if;
end if;
end On_Enumeration_Type;
begin
Each_Enumeration_Type
(Map, Optional_Types, On_Enumeration_Type'Access);
end;
declare
procedure On_Union_Type
(Name : Schema_Name;
Property : VSS.Strings.Virtual_String;
Schema : Schema_Access;
Optional : Boolean);
-- Generate enumeration type for anyOf Schema
procedure On_Union_Type
(Name : Schema_Name;
Property : VSS.Strings.Virtual_String;
Schema : Schema_Access;
Optional : Boolean)
is
pragma Unreferenced (Optional);
Type_Name : VSS.Strings.Virtual_String := Ref_To_Type_Name (Name);
begin
if not Property.Is_Empty then
Type_Name.Append ("_");
Type_Name.Append (Property);
end if;
Type_Name.Append ("_Variant");
Write_Union_Variant_Type (Map, Type_Name, Schema);
end On_Union_Type;
begin
Each_Union_Type
(Map, Optional_Types, On_Union_Type'Access);
end;
if not Enum_Package.Is_Empty then
Put ("end ");
Put (Enum_Package);
Put (";");
New_Line;
New_Line;
end if;
Write_Named_Types
(Map, Root_Package, Enum_Package, Holders, Optional_Types, Done);
Write_Vector_Operations (Array_Types, Specification);
Write_Holders_Operations (Map, Holders, Specification);
Put ("private");
New_Line;
Write_Private_Vectors (Array_Types, Enum_Package);
Write_Privete_Holders (Map, Holders);
Put ("end ");
Put (Root_Package);
Put (";");
New_Line;
Print_Vector (Header);
Put ("with Ada.Unchecked_Deallocation;");
New_Line;
New_Line;
Put ("package body ");
Put (Root_Package);
Put (" is");
New_Line;
Write_Vector_Operations (Array_Types, Implemenetation);
Write_Holders_Operations (Map, Holders, Implemenetation);
Put ("end ");
Put (Root_Package);
Put (";");
New_Line;
New_Line;
end Write_Type_Package;
----------------------
-- Write_Union_Type --
----------------------
procedure Write_Union_Type
(Name : Schema_Name;
Map : JSON_Schema.Readers.Schema_Map;
Root_Package : VSS.Strings.Virtual_String;
Enum_Package : VSS.Strings.Virtual_String;
Holders : VSS.String_Vectors.Virtual_String_Vector;
Optional_Types : String_Sets.Set;
Done : in out String_Sets.Set)
is
use type VSS.Strings.Virtual_String;
procedure On_Anonymous_Schema (Property : JSON_Schema.Property);
-- Generate anonymous type for given property
-------------------------
-- On_Anonymous_Schema --
-------------------------
procedure On_Anonymous_Schema (Property : JSON_Schema.Property) is
begin
Write_Anonymous_Type
(Name,
Property,
Map,
Optional_Types,
Done,
Property.Schema.Required.Contains (Property.Name),
Root_Package, Enum_Package, Holders);
end On_Anonymous_Schema;
Schema : constant Schema_Access := Map (Name);
Enum_Prefix : constant VSS.Strings.Virtual_String :=
(if Enum_Package.Is_Empty then VSS.Strings.Empty_Virtual_String
else Enum_Package & ".");
Type_Name : constant VSS.Strings.Virtual_String :=
Ref_To_Type_Name (Name);
begin
-- Write dependencies
for Item of Schema.Any_Of loop
if not Item.Ref.Is_Empty then
Write_Named_Type
(Item.Ref,
Map,
Root_Package,
Enum_Package,
Holders,
Optional_Types,
Done);
end if;
end loop;
Each_Anonymous_Schema (Map, Schema, On_Anonymous_Schema'Access);
Put ("type ");
Put (Type_Name);
Put ("_Union (Kind : ");
Put (Enum_Prefix);
Put (Type_Name);
Put ("_Variant := ");
Put (Enum_Prefix);
Put (Type_Name);
Put ("_Variant'First) is ");
Put ("record");
New_Line;
Put ("case Kind is");
New_Line;
for Index in 1 .. Schema.Any_Of.Last_Index loop
declare
Variant : constant VSS.Strings.Virtual_String :=
Escape_Keywords
(Variant_Name (Map, Schema.Any_Of.Element (Index)));
Property : constant JSON_Schema.Property :=
(Variant, Schema.Any_Of.Element (Index));
begin
Put ("when ");
Put (Enum_Prefix);
Put (Variant);
Put (" =>");
New_Line;
Write_Record_Component
(Name, Map, Root_Package, Property, True, False);
end;
end loop;
Put ("end case;");
New_Line;
Put ("end record;");
New_Line;
New_Line;
-- We need a wrapper around discriminanted type to be able to change
-- discriminant in Input_Type procedure, otherwise we will get
-- Constraint_Error exception.
Put ("type ");
Put (Type_Name);
Put (" is record"); New_Line;
Put ("Union : ");
Put (Type_Name);
Put ("_Union;"); New_Line;
Put ("end record;");
New_Line;
New_Line;
end Write_Union_Type;
------------------------------
-- Write_Union_Variant_Type --
------------------------------
procedure Write_Union_Variant_Type
(Map : JSON_Schema.Readers.Schema_Map;
Name : VSS.Strings.Virtual_String;
Schema : Schema_Access) is
begin
Put ("type ");
Put (Name);
Put (" is (");
for Index in 1 .. Schema.Any_Of.Last_Index loop
if Index > 1 then
Put (", ");
end if;
Put
(Escape_Keywords
(Variant_Name (Map, Schema.Any_Of.Element (Index))));
end loop;
Put (");");
New_Line;
New_Line;
end Write_Union_Variant_Type;
-----------------------------
-- Write_Vector_Operations --
-----------------------------
procedure Write_Vector_Operations
(Array_Types : Readers.Schema_Map;
Kind : Declaration_Kind)
is
use type VSS.Strings.Virtual_String;
begin
for Cursor in Array_Types.Iterate loop
declare
Item : constant VSS.Strings.Virtual_String :=
Readers.Schema_Maps.Key (Cursor);
Schema : constant Schema_Access :=
Readers.Schema_Maps.Element (Cursor);
Element_Type : constant VSS.Strings.Virtual_String :=
(if Schema /= null and then Is_Enum (Schema)
then "Enum." & Item else Item);
begin
if Kind = Implemenetation then
Put ("procedure Free is new Ada.Unchecked_Deallocation");
New_Line;
Put ("(");
Put (Item);
Put ("_Array, ");
Put (Item);
Put ("_Array_Access);");
New_Line;
New_Line;
Put ("overriding procedure Adjust (Self : in out ");
Put (Item);
Put ("_Vector) is");
New_Line;
Put ("begin");
New_Line;
Put ("if Self.Length > 0 then");
New_Line;
Put ("Self.Data :=");
New_Line;
Put ("new ");
Put (Item);
Put ("_Array'(Self.Data (1 .. Self.Length));");
New_Line;
Put ("end if;");
New_Line;
Put ("end Adjust;");
New_Line;
New_Line;
Put ("overriding procedure Finalize (Self : in out ");
Put (Item);
Put ("_Vector) is");
New_Line;
Put ("begin");
New_Line;
Put ("Free (Self.Data);");
New_Line;
Put ("Self.Length := 0;");
New_Line;
Put ("end Finalize;");
New_Line;
New_Line;
end if;
Put ("function Length (Self : ");
Put (Item);
Put ("_Vector) return Natural");
if Kind = Implemenetation then
Put (" is (Self.Length)");
end if;
Put (";");
New_Line;
New_Line;
Put ("procedure Clear (Self : in out ");
Put (Item);
Put ("_Vector)");
if Kind = Implemenetation then
Put (" is");
New_Line;
Put ("begin");
New_Line;
Put (" Self.Length := 0;");
New_Line;
Put ("end Clear");
end if;
Put (";");
New_Line;
New_Line;
Put ("procedure Append");
New_Line;
Put ("(Self : in out ");
Put (Item);
Put ("_Vector;");
New_Line;
Put ("Value : ");
Put (Element_Type);
Put (")");
if Kind = Implemenetation then
Put (" is");
New_Line;
Put ("Init_Length : constant Positive :=");
New_Line;
Put ("Positive'Max (1, 256 / ");
Put (Element_Type);
Put ("'Size);");
New_Line;
Put ("Self_Data_Saved : ");
Put (Item);
Put ("_Array_Access := Self.Data;");
New_Line;
Put ("begin");
New_Line;
Put ("if Self.Length = 0 then");
New_Line;
Put ("Self.Data := new ");
Put (Item);
Put ("_Array (1 .. Init_Length);");
New_Line;
Put ("elsif Self.Length = Self.Data'Last then");
New_Line;
Put ("Self.Data :=");
New_Line;
Put ("new ");
Put (Item);
Put ("_Array'");
Put ("(Self.Data.all");
New_Line;
Put (" & ");
Put (Item);
Put ("_Array'(1 .. Self.Length => <>));");
New_Line;
Put ("Free (Self_Data_Saved);");
New_Line;
Put ("end if;");
New_Line;
Put ("Self.Length := Self.Length + 1;");
New_Line;
Put ("Self.Data (Self.Length) := Value;");
New_Line;
Put ("end Append");
end if;
Put (";");
New_Line;
New_Line;
if Kind = Specification then
Put ("type ");
Put (Item);
Put ("_Variable_Reference");
New_Line;
Put ("(Element : not null access ");
Put (Element_Type);
Put (") is null record");
New_Line;
Put ("with Implicit_Dereference => Element;");
New_Line;
New_Line;
end if;
Put ("not overriding function Get_");
Put (Item);
Put ("_Variable_Reference");
New_Line;
Put ("(Self : aliased in out ");
Put (Item);
Put ("_Vector;");
New_Line;
Put ("Index : Positive)");
New_Line;
Put ("return ");
Put (Item);
Put ("_Variable_Reference");
New_Line;
if Kind = Specification then
Put ("with Inline;");
else
Put ("is (Element => Self.Data (Index)'Access);");
end if;
New_Line;
New_Line;
if Kind = Specification then
Put ("type ");
Put (Item);
Put ("_Constant_Reference");
New_Line;
Put ("(Element : not null access constant ");
Put (Element_Type);
Put (") is null record");
New_Line;
Put ("with Implicit_Dereference => Element;");
New_Line;
New_Line;
end if;
Put ("not overriding function Get_");
Put (Item);
Put ("_Constant_Reference");
New_Line;
Put ("(Self : aliased ");
Put (Item);
Put ("_Vector;");
New_Line;
Put ("Index : Positive)");
New_Line;
Put ("return ");
Put (Item);
Put ("_Constant_Reference");
New_Line;
if Kind = Specification then
Put ("with Inline;");
else
Put ("is (Element => Self.Data (Index)'Access);");
end if;
New_Line;
New_Line;
end;
end loop;
end Write_Vector_Operations;
end JSON_Schema.Writers.Types;
|