From ec767ef60d23815df2a1c6954bdd29ce6f6980a0 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Thu, 30 Mar 2023 13:19:42 +0200 Subject: [PATCH] Typed variants of Alire.Config.Edit.Set (#1353) * Typed variants of Alire.Config.Edit.Set Mostly because it's too easy to mix Boolean with String * Migrate raw uses to typed uses * Leave only needed instances * Update clic dependency --- alire.toml | 2 +- deps/clic | 2 +- dev/edit.sh | 2 +- src/alire/alire-config-edit.adb | 18 ++++++++++++++++++ src/alire/alire-config-edit.ads | 10 +++++++++- src/alire/alire-selftest.adb | 20 ++++++++++++++++++++ src/alire/alire-toolchains.adb | 10 +++++----- src/alire/alire-toolchains.ads | 18 +++++++++--------- src/alr/alr-commands-config.adb | 27 +++++++++++++++++++++------ src/alr/alr-commands-get.adb | 6 ++++-- 10 files changed, 89 insertions(+), 26 deletions(-) diff --git a/alire.toml b/alire.toml index e5e37818..0c772e9b 100644 --- a/alire.toml +++ b/alire.toml @@ -47,7 +47,7 @@ windows = { ALIRE_OS = "windows" } [[pins]] aaa = { url = "https://github.com/mosteo/aaa", commit = "906d9eaf4fb8efabfbc3d8cfb34d04ceec340e13" } ada_toml = { url = "https://github.com/mosteo/ada-toml", commit = "da4e59c382ceb0de6733d571ecbab7ea4919b33d" } -clic = { url = "https://github.com/alire-project/clic", commit = "5b46b3c57fd0c6a62db1608c9f5573b1866e0dbe" } +clic = { url = "https://github.com/alire-project/clic", commit = "ce4668206bbd038e857ce6a2d463c3de9a0cc9bb" } gnatcoll = { url = "https://github.com/alire-project/gnatcoll-core.git", commit = "92bb91130a9ec628b4c48b7ef9fe7f24d9dc25fa" } semantic_versioning = { url = "https://github.com/alire-project/semantic_versioning", commit = "2f23fc5f6b4855b836b599adf292fed9c0ed4144" } simple_logging = { url = "https://github.com/alire-project/simple_logging", commit = "3505dc645f3eef6799a486aae223d37e88cfc4d5" } diff --git a/deps/clic b/deps/clic index 5b46b3c5..ce466820 160000 --- a/deps/clic +++ b/deps/clic @@ -1 +1 @@ -Subproject commit 5b46b3c57fd0c6a62db1608c9f5573b1866e0dbe +Subproject commit ce4668206bbd038e857ce6a2d463c3de9a0cc9bb diff --git a/dev/edit.sh b/dev/edit.sh index dcc39a49..66ca7ca9 100755 --- a/dev/edit.sh +++ b/dev/edit.sh @@ -1 +1 @@ -gnatstudio -P alr_env & +gnatstudio -P alr_env & >/dev/null 2>&1 diff --git a/src/alire/alire-config-edit.adb b/src/alire/alire-config-edit.adb index fa3e8f2e..bedfb046 100644 --- a/src/alire/alire-config-edit.adb +++ b/src/alire/alire-config-edit.adb @@ -67,6 +67,24 @@ package body Alire.Config.Edit is end case; end Set; + ----------------- + -- Set_Boolean -- + ----------------- + + procedure Set_Boolean (Level : Config.Level; + Key : CLIC.Config.Config_Key; + Value : Boolean; + Check : CLIC.Config.Check_Import := null) + is + function Set_Boolean_Impl is new CLIC.Config.Edit.Set_Typed + (Boolean, TOML_Boolean, Boolean'Image); + begin + Assert (Set_Boolean_Impl (Filepath (Level), Key, Value, Check), + "Cannot set config key '" & Key & "' at level " & Level'Image); + -- Reload after change + Load_Config; + end Set_Boolean; + -------------- -- Filepath -- -------------- diff --git a/src/alire/alire-config-edit.ads b/src/alire/alire-config-edit.ads index 0547a8d6..423ea828 100644 --- a/src/alire/alire-config-edit.ads +++ b/src/alire/alire-config-edit.ads @@ -7,7 +7,8 @@ with Alire.Directories; package Alire.Config.Edit is - -- Shortcuts that use the standard config locations: + -- Shortcuts that use the standard config locations. These interpret the + -- value in string as a TOML type whenever possible. procedure Set_Locally (Key : CLIC.Config.Config_Key; Value : String; @@ -22,6 +23,13 @@ package Alire.Config.Edit is Value : String; Check : CLIC.Config.Check_Import := null); + -- Typed alternatives + + procedure Set_Boolean (Level : Config.Level; + Key : CLIC.Config.Config_Key; + Value : Boolean; + Check : CLIC.Config.Check_Import := null); + -- To ease the pain with circularities in old GNAT versions, we have also -- here all non-preelaborable things related to config loading. This -- way, querying stays preelaborable. diff --git a/src/alire/alire-selftest.adb b/src/alire/alire-selftest.adb index 38ca72fb..9d2ab80e 100644 --- a/src/alire/alire-selftest.adb +++ b/src/alire/alire-selftest.adb @@ -14,6 +14,26 @@ package body Alire.Selftest is Config.Edit.Set_Globally (Key, Val); pragma Assert (Config.DB.Defined (Key)); pragma Assert (Config.DB.Get (Key, "snafu") = Val); + + -- Check typed storing + + -- Raw storing of integer + Config.Edit.Set_Globally (Key, "777"); + pragma Assert (Integer (Config.DB.Get (Key, 0)) = 777); + + -- Raw storing of boolean + Config.Edit.Set_Globally (Key, "true"); + pragma Assert (Config.DB.Get (Key, False) = True); + + -- Typed storing of boolean + Config.Edit.Set_Boolean (Config.Global, Key, False); + pragma Assert (Config.DB.Get (Key, True) = False); + + -- Raw storing of boolean with wrong type + Config.Edit.Set_Globally (Key, "True"); + -- This causes a string to be stored, as in TOML only "true" is bool + pragma Assert (Config.DB.Get (Key, "False") = "True"); + end Check_Config_Changes; ------------------------ diff --git a/src/alire/alire-toolchains.adb b/src/alire/alire-toolchains.adb index 2ff8ecb5..ff712eb5 100644 --- a/src/alire/alire-toolchains.adb +++ b/src/alire/alire-toolchains.adb @@ -346,10 +346,10 @@ package body Alire.Toolchains is (Level, Key => Tool_Key (Release.Name), Value => Release.Milestone.Image); - Alire.Config.Edit.Set + Alire.Config.Edit.Set_Boolean (Level, Key => Tool_Key (Release.Name, For_Is_External), - Value => Boolean'(not Release.Origin.Is_Regular)'Image); + Value => not Release.Origin.Is_Regular); end Set_As_Default; ----------------------------- @@ -359,9 +359,9 @@ package body Alire.Toolchains is procedure Set_Automatic_Assistant (Enabled : Boolean; Level : Config.Level) is begin - Config.Edit.Set (Level, - Config.Keys.Toolchain_Assistant, - (if Enabled then "true" else "false")); + Config.Edit.Set_Boolean (Level, + Config.Keys.Toolchain_Assistant, + Enabled); end Set_Automatic_Assistant; ------------------------ diff --git a/src/alire/alire-toolchains.ads b/src/alire/alire-toolchains.ads index be55f355..74c4692a 100644 --- a/src/alire/alire-toolchains.ads +++ b/src/alire/alire-toolchains.ads @@ -113,6 +113,15 @@ private function Assistant_Enabled return Boolean is (Config.DB.Get (Config.Keys.Toolchain_Assistant, Default => True)); + ---------------------- + -- Tool_Is_External -- + ---------------------- + + function Tool_Is_External (Crate : Crate_Name) return Boolean + is (Boolean'Value + (Config.DB.Get_As_String -- because it could be stored as bool or string + (Tool_Key (Crate, For_Is_External), "True"))); + -------------- -- Tool_Key -- -------------- @@ -128,15 +137,6 @@ private when For_Is_External => Config.Keys.Toolchain_External) & "." & Crate.As_String)); - ---------------------- - -- Tool_Is_External -- - ---------------------- - - function Tool_Is_External (Crate : Crate_Name) return Boolean - is (Boolean'Value - (Config.DB.Get - (Tool_Key (Crate, For_Is_External), Default => "True"))); - -------------------- -- Tool_Milestone -- -------------------- diff --git a/src/alr/alr-commands-config.adb b/src/alr/alr-commands-config.adb index b6699c74..b990a089 100644 --- a/src/alr/alr-commands-config.adb +++ b/src/alr/alr-commands-config.adb @@ -1,10 +1,12 @@ -with CLIC.Config.Info; -with CLIC.Config.Edit; +with AAA.Enum_Tools; with Alire.Config; with Alire.Config.Edit; with Alire.Root; +with CLIC.Config.Info; +with CLIC.Config.Edit; + package body Alr.Commands.Config is ------------- @@ -105,10 +107,23 @@ package body Alr.Commands.Config is Key & "'"); end if; - Alire.Config.Edit.Set - (Lvl, - Key, Val, - Check => Alire.Config.Edit.Valid_Builtin'Access); + -- Check explicitly for booleans to store the proper TOML type + -- regardless of the capitalization used by the user. + declare + function Is_Boolean is new AAA.Enum_Tools.Is_Valid (Boolean); + begin + if Is_Boolean (Val) then + Alire.Config.Edit.Set_Boolean + (Lvl, + Key, Boolean'Value (Val), + Check => Alire.Config.Edit.Valid_Builtin'Access); + else + Alire.Config.Edit.Set + (Lvl, + Key, Val, + Check => Alire.Config.Edit.Valid_Builtin'Access); + end if; + end; end; elsif Cmd.Unset then diff --git a/src/alr/alr-commands-get.adb b/src/alr/alr-commands-get.adb index 7cc9db04..c1fcc191 100644 --- a/src/alr/alr-commands-get.adb +++ b/src/alr/alr-commands-get.adb @@ -161,8 +161,10 @@ package body Alr.Commands.Get is Trace.Info ("Because --only was used, automatic dependency" & " retrieval is disabled in this workspace:" & " use `alr update` to apply dependency changes"); - Alire.Config.Edit.Set_Locally - (Alire.Config.Keys.Update_Manually, "true"); + Alire.Config.Edit.Set_Boolean + (Alire.Config.Local, + Alire.Config.Keys.Update_Manually, + True); if not CLIC.User_Input.Not_Interactive then Alire.Roots.Print_Nested_Crates (Cmd.Root.Path); -- 2.39.5