Skip to content

Commit

Permalink
Typed variants of Alire.Config.Edit.Set (#1353)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mosteo authored Mar 30, 2023
1 parent 9072fab commit ec767ef
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 26 deletions.
2 changes: 1 addition & 1 deletion alire.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion deps/clic
2 changes: 1 addition & 1 deletion dev/edit.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gnatstudio -P alr_env &
gnatstudio -P alr_env & >/dev/null 2>&1
18 changes: 18 additions & 0 deletions src/alire/alire-config-edit.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
--------------
Expand Down
10 changes: 9 additions & 1 deletion src/alire/alire-config-edit.ads
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions src/alire/alire-selftest.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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;

------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/alire/alire-toolchains.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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;

-----------------------------
Expand All @@ -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;

------------------------
Expand Down
18 changes: 9 additions & 9 deletions src/alire/alire-toolchains.ads
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
--------------
Expand All @@ -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 --
--------------------
Expand Down
27 changes: 21 additions & 6 deletions src/alr/alr-commands-config.adb
Original file line number Diff line number Diff line change
@@ -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

-------------
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/alr/alr-commands-get.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ec767ef

Please sign in to comment.