Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for tables for default in AuthorOptions #999

Merged
merged 6 commits into from Dec 8, 2018
Merged

Check for tables for default in AuthorOptions #999

merged 6 commits into from Dec 8, 2018

Conversation

adamhl8
Copy link

@adamhl8 adamhl8 commented Dec 6, 2018

Fixes #998

Copy link
Contributor

@emptyrivers emptyrivers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will work, but it should be recursive to be fully correct.

@@ -1147,13 +1147,26 @@ end

local function allChoicesAreDefault(data)
for _, option in ipairs(data.authorOptions) do
if option.key ~= nil and option.default ~= nil and option.default ~= data.config[option.key] then
if option.key ~= nil and option.default ~= nil and (option.default ~= data.config[option.key] or compareTables(option.default, data.config[option.key])) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work because the logic is the wrong way around. You want to check if the tables are different.

Also this will not work if you are comparing two indentical values, as in that case compareValues is called with two values, and calling pairs on that will result in a error.

@adamhl8
Copy link
Author

adamhl8 commented Dec 6, 2018

ef5be30

I believe that should do it. It's recursive and fixes the logic for checking if the values match.

local function allChoicesAreDefault(data)
for _, option in ipairs(data.authorOptions) do
if option.key ~= nil and option.default ~= nil and option.default ~= data.config[option.key] then
return false
return tablesAreEqual(option.default, data.config[option.key])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop needs to continue if the values are equal. It should only return false if the values are not equal.
Also your function also checks for equality for non-table values, so the original check "option.default ~= data.config[option.key]" should not be necessary.

We might have a table comparing equal function already somewhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, sorry. Latest commit should (finally) be correct.

@emptyrivers emptyrivers added the 🏨 Bugfix This pull request fixes an issue. label Dec 8, 2018
if option.key ~= nil and option.default ~= nil and option.default ~= data.config[option.key] then
return false
end
if option.key ~= nil and option.default ~= nil then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 if option.key ~= nil and option.default ~= nil then
-  if tablesAreEqual(option.default, data.config[option.key]) then
-  elseif option.default ~= data.config[option.key] then
+ if not tablesAreEqual(option.default, data.config[option.key]) then
    return false
  end
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's still not quite correct, but your if branch was too complex.

Copy link
Author

@adamhl8 adamhl8 Dec 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah that's more simple.

Why is that not correct? With that, it still returns true/false as it should, right (looking at the example inputs from my latest comment)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, for one thing, you can simply check that the option class is not noninteractive (see the top of the file), instead of checking that the key and default aren't nil.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right it's indeed correct. But that's not how that check should be written. A if, whose only purpose is to act as a additional check for the elseif branch is unnecessarily complex.

@adamhl8
Copy link
Author

adamhl8 commented Dec 8, 2018

That's why it's only exiting the loop on finding something unequal, but continues when the values are the same.
For tables, the same must happen. If the tables are equal, continue with the next option. If they aren't equal, we found a option that isn't on the default vaue and should exit that function.

With that latest commit, it does only exit the loop when it finds something unequal.

For example:

Say the function is passed 5, 5.
tablesAreEqual returns true, loop continues.

Say it's passed 5, 7.
tablesAreEqual returns false, elseif option.default ~= data.config[option.key] is checked (5 ~= 7), function returns false, as it should.

Pass {1, 2}, {1, 2}.
Same thing, tablesAreEqual returns true, loop continues.

Pass {1, 2}, {1, 3}.
tablesAreEqual returns false, elseif option.default ~= data.config[option.key] is checked (tables are unique), function returns false, as it should.

Apologies if I'm being difficult, but I'm fairly sure that's correct.

Edit:

Main thing is that if tablesAreEqual returns true, then the elseif is NOT checked, so the loop only exits if the two options are different.

@adamhl8
Copy link
Author

adamhl8 commented Dec 8, 2018

Alright, that's my last try. If that doesn't work I'll give up and let you guys do it :P

@emptyrivers
Copy link
Contributor

I pushed a fix to your whitespace (you should install editorconfig so that it's taken care of for you)

@InfusOnWoW InfusOnWoW merged commit a78607d into WeakAuras:master Dec 8, 2018
@InfusOnWoW
Copy link
Contributor

Thanks, merged now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏨 Bugfix This pull request fixes an issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants