-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core.app] Use `equal' to compare configurations.
- Loading branch information
Showing
1 changed file
with
18 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,22 @@ function configure (new_config) | |
configuration = new_config | ||
end | ||
|
||
-- Returns true if x and y are structurally similar (isomorphic). | ||
function equal (x, y) | ||
if type(x) ~= type(y) then return false end | ||
if type(x) == 'table' then | ||
for k, v in pairs(x) do | ||
if not equal(v, y[k]) then return false end | ||
end | ||
for k, _ in pairs(x) do | ||
if y[k] == nil then return false end | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
javierguerragiraldez
Contributor
|
||
end | ||
return true | ||
else | ||
return x == y | ||
end | ||
end | ||
|
||
-- Return the configuration actions needed to migrate from old config to new. | ||
-- The return value is a table: | ||
-- app_name -> stop | start | keep | restart | reconfig | ||
|
@@ -52,8 +68,8 @@ function compute_config_actions (old, new) | |
local action = nil | ||
if not old.apps[appname] then action = 'start' | ||
elseif old.apps[appname].class ~= class then action = 'restart' | ||
elseif (type(old.apps[appname].arg) == "string" and | ||
old.apps[appname].arg ~= arg) then action = 'reconfig' | ||
elseif not equal(old.apps[appname].arg, arg) | ||
then action = 'reconfig' | ||
else action = 'keep' end | ||
actions[appname] = action | ||
end | ||
|
I know that Lua's table semantics are a bit special, but this code still looks fishy to me. If we survive the first loop, we know that all elements that exist in X have equal element in the same places at Y. The second loop then checks whether there are "nil" elements at some places that exist in X. But we already know that if this is the case, the corresponding elements in X are also "nil" (because they have been tested to be equal()).
What is missing is a check whether Y has any additional elements that X doesn't have, right?
[I had some proposed alternative code here but it was bullshit so I removed it, sorry.]