disqus / gargoyle

Feature switches in Django
http://engineering.disqus.com
Apache License 2.0
746 stars 112 forks source link

Conditions continually appended to Switch value #96

Open pifantastic opened 9 years ago

pifantastic commented 9 years ago

It's highly possible that I'm just using gargoyle wrong, but I'm experiencing surprising behavior.

In one of my project's apps, I've created a gargoyle.py file:

from __future__ import absolute_import

from gargoyle import gargoyle

gargoyle['foo'].add_condition(
    condition_set='gargoyle.builtins.UserConditionSet(auth.user)',
    field_name='percent',
    condition='0-50'
)

My understanding is that this will make the foo switch active for 50% of users. This seems to work. The value in the database after I run my local server is:

{
  "auth.user": {
    "percent": [
      [
        "i", 
        "0-50"
      ]
    ]
  }
}

But now, if I restart my server, the condition is duplicated:

{
  "auth.user": {
    "percent": [
      [
        "i", 
        "0-50"
      ], 
      [
        "i", 
        "0-50"
      ]
    ]
  }
}

I've traced this to Switch.add_condition:

        if condition not in self.value[namespace][field_name]:
            self.value[namespace][field_name].append((exclude and EXCLUDE or INCLUDE, condition))

Here it's checking if condition which is '0-50' is "in" a list of tuples, which will always return false. Obviously this is solvable by adding commit=False to Switch.add_condition. However, it makes me worry that I'm missing something fundamental.

pifantastic commented 9 years ago

Also curious if there is documentation for switches somewhere that I'm just missing. http://gargoyle.readthedocs.org/en/latest/ref/conditions.html is empty.