Skip to content

Commit

Permalink
Merge pull request #14 from journy-io/null-array-properties
Browse files Browse the repository at this point in the history
Add support for null values and arrays in properties
  • Loading branch information
hansott authored Jun 29, 2021
2 parents 1aee9b2 + 181f94e commit 90cd5e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Note: `full_name`, `first_name`, `last_name`, `phone` and `registered_at` are de
```python
from journyio.client import Properties
from journyio.user_identified import UserIdentified
from datetime import datetime

user = UserIdentified("userId", "name@domain.tld")
# or
Expand All @@ -78,8 +79,12 @@ properties["full_name"] = "John Doe"
properties["first_name"] = "John"
properties["last_name"] = "Doe"
properties["phone"] = "123"
properties["registered_at"] = datetime.now()
properties["is_admin"] = True
properties["registered_at"] = datetime.now()
properties["age"] = 26
properties["array_of_values"] = ["value1", "value2"]
properties["key_with_empty_value"] = ""
properties["this_property_will_be_deleted"] = None

result = client.upsert_user(user, properties)
if isinstance(result, Success):
Expand Down Expand Up @@ -109,6 +114,9 @@ properties["mrr"] = 399
properties["plan"] = "Pro"
properties["registered_at"] = datetime.now()
properties["is_paying"] = True
properties["array_of_values"] = ["value1", "value2"]
properties["key_with_empty_value"] = ""
properties["this_property_will_be_deleted"] = None

member1 = UserIdentified.by_user_id("memberId1")
member2 = UserIdentified.by_user_id("memberId2")
Expand Down
9 changes: 6 additions & 3 deletions journyio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ def __getitem__(self, key: str):
assert_journy(isinstance(key, str), "The key is not a string.")
return self.properties.get(key.lower().strip())

def __setitem__(self, key: str, value: str or list):
def __setitem__(self, key: str, value: str or List[str] or bool or int or datetime or None):
assert_journy(isinstance(key, str), "The key is not a string.")
if isinstance(value, str) or isinstance(value, int) or isinstance(value, bool) or isinstance(value, datetime):
if isinstance(value, str) or isinstance(value, int) or isinstance(value, bool) or isinstance(value, datetime) \
or value is None or (isinstance(value, list) and all([isinstance(el, str) for el in value])):
if isinstance(value, datetime):
value = str(value.isoformat())
self.properties.__setitem__(key.lower().strip(), value)
else:
raise JournyException("Value is not a string, number, boolean or datetime.")
raise JournyException("Value is not a string, number, boolean, datetime or None.")

def union(self, other):
self.properties.update(other.properties)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def test_properties():
properties["doesexisttoo"] = 2
assert (properties["doesexisttoo"] == 2)
properties["thistoo"] = True
properties["will_be_deleted"] = None
properties["array_of_values"] = ["first_value", "second_value"]
assert (properties["thistoo"])
with pytest.raises(JournyException):
properties[2] = "hallo"
Expand Down

0 comments on commit 90cd5e2

Please sign in to comment.