Debian Codegraph @

deps/ada-toml/tests/api/merge/main.adb

raw
1 with Ada.Text_IO;
2 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
3
4 with TOML;
5
6 procedure Main is
7 Table1 : constant TOML.TOML_Value := TOML.Create_Table;
8 Table2 : constant TOML.TOML_Value := TOML.Create_Table;
9 Table3 : constant TOML.TOML_Value := TOML.Create_Table;
10 Arr : constant TOML.TOML_Value := TOML.Create_Array;
11 begin
12 -- Populate tables
13 Table1.Set ("a", TOML.Create_Integer (1));
14
15 Arr.Append (TOML.Create_Integer (1));
16 Table2.Set ("b", Arr);
17
18 Table3.Set ("a", TOML.Create_Integer (3));
19
20 -- With the resolver-less Merge overload, expect a constraint error on
21 -- duplicate keys.
22 declare
23 Dummy : TOML.TOML_Value;
24 begin
25 Dummy := TOML.Merge (Table1, Table3);
26 Ada.Text_IO.Put_Line ("No exception...");
27 exception
28 when Constraint_Error =>
29 Ada.Text_IO.Put_Line
30 ("Merging two tables with duplicate keys raises an exception");
31 end;
32 Ada.Text_IO.New_Line;
33
34 -- With the resolver overload, check that conflicts are resolved as
35 -- expected
36 Ada.Text_IO.Put_Line ("Merging with conflict resolution...");
37 declare
38 function Merge_Entries
39 (Key : TOML.Unbounded_UTF8_String;
40 L, R : TOML.TOML_Value) return TOML.TOML_Value
41 is (TOML.Create_String (Key & ": " & L.As_String & " | " & R.As_String));
42
43 L : constant TOML.TOML_Value := TOML.Create_Table;
44 R : constant TOML.TOML_Value := TOML.Create_Table;
45
46 Merged : TOML.TOML_Value;
47 begin
48 L.Set ("a", TOML.Create_String ("good"));
49 L.Set ("b", TOML.Create_String ("hello"));
50
51 R.Set ("b", TOML.Create_String ("world"));
52 R.Set ("c", TOML.Create_String ("bye"));
53
54 Merged := TOML.Merge (L, R, Merge_Entries'Access);
55
56 Ada.Text_IO.Put_Line (Merged.Dump_As_String);
57 end;
58
59 declare
60 Merged : constant TOML.TOML_Value := TOML.Merge (Table1, Table2);
61 begin
62 -- Change array value to see the shallow copy modified
63 Arr.Append (TOML.Create_Integer (2));
64
65 Ada.Text_IO.Put_Line ("Merged table:");
66 Ada.Text_IO.Put_Line (Merged.Dump_As_String);
67 end;
68 end Main;