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

Add None value handling for JSONObject builder #2021

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2021.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `None` check for every put function in `JSONObjectBuilder`
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for this changelog fragment, this change is purely internal

45 changes: 32 additions & 13 deletions hikari/internal/data_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ def put(
value
The JSON type to put. This may be a non-JSON type if a conversion
is also specified. This may alternatively be undefined. In the latter
case, nothing is performed.
case, nothing is performed. This may also be [`None`][], in which case
the value isn't cast and is stored as-is.
conversion
The optional conversion to apply.
"""
Expand All @@ -309,13 +310,13 @@ def put(
self[key] = conversion(value)

@typing.overload
def put_array(self, key: str, values: undefined.UndefinedOr[typing.Iterable[JSONish]], /) -> None: ...
def put_array(self, key: str, values: undefined.UndefinedNoneOr[typing.Iterable[JSONish]], /) -> None: ...

@typing.overload
def put_array(
self,
key: str,
values: undefined.UndefinedOr[typing.Iterable[T_co]],
values: undefined.UndefinedNoneOr[typing.Iterable[T_co]],
/,
*,
conversion: typing.Callable[[T_co], JSONish],
Expand All @@ -324,7 +325,7 @@ def put_array(
def put_array(
self,
key: str,
values: undefined.UndefinedOr[typing.Iterable[typing.Any]],
values: undefined.UndefinedNoneOr[typing.Iterable[typing.Any]],
/,
*,
conversion: typing.Optional[typing.Callable[[typing.Any], JSONish]] = None,
Expand All @@ -342,15 +343,21 @@ def put_array(
values
The JSON types to put. This may be an iterable of non-JSON types if
a conversion is also specified. This may alternatively be undefined.
In the latter case, nothing is performed.
In the latter case, nothing is performed. This may also be [`None`][],
in which case the value isn't cast and is stored as-is.
conversion
The optional conversion to apply.
"""
if values is not undefined.UNDEFINED:
if values is undefined.UNDEFINED:
return

if values is not None:
if conversion is not None:
self[key] = [conversion(value) for value in values]
else:
self[key] = list(values)
else:
self[key] = values
syncblaze marked this conversation as resolved.
Show resolved Hide resolved

def put_snowflake(
self, key: str, value: undefined.UndefinedNoneOr[snowflakes.SnowflakeishOr[snowflakes.Unique]], /
Expand All @@ -366,15 +373,21 @@ def put_snowflake(
value
The JSON type to put. This may alternatively be undefined, in this
case, nothing is performed. This may also be [`None`][], in this
case the value isn't cast.
case the value isn't cast and is stored as-is.
"""
if value is not undefined.UNDEFINED and value is not None:
if value is undefined.UNDEFINED:
return

if value is not None:
self[key] = str(int(value))
elif value is None:
self[key] = None
else:
self[key] = value
syncblaze marked this conversation as resolved.
Show resolved Hide resolved

def put_snowflake_array(
self, key: str, values: undefined.UndefinedOr[typing.Iterable[snowflakes.SnowflakeishOr[snowflakes.Unique]]], /
self,
key: str,
values: undefined.UndefinedNoneOr[typing.Iterable[snowflakes.SnowflakeishOr[snowflakes.Unique]]],
/,
) -> None:
"""Put an array of snowflakes with the given key into this builder.

Expand All @@ -388,10 +401,16 @@ def put_snowflake_array(
The key to give the element.
values
The JSON snowflakes to put. This may alternatively be undefined.
In the latter case, nothing is performed.
In the latter case, nothing is performed. This may also be [`None`][],
in which case the value isn't cast and is stored as-is.
"""
if values is not undefined.UNDEFINED:
if values is undefined.UNDEFINED:
return

if values is not None:
self[key] = [str(int(value)) for value in values]
else:
self[key] = values
syncblaze marked this conversation as resolved.
Show resolved Hide resolved


def cast_variants_array(cast: typing.Callable[[T_co], T], raw_values: typing.Iterable[T_co], /) -> list[T]:
Expand Down