mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 10:17:59 -05:00
Fix creation of IdentifierData in config raw methods (#3829)
* Fix creation of IdentifierData in config Also adds some new tests regarding partial primary keys. Resolves #3796. Signed-off-by: Toby Harradine <tobyharradine@gmail.com> Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
@@ -334,7 +334,7 @@ class Group(Value):
|
||||
"""
|
||||
is_group = self.is_group(item)
|
||||
is_value = not is_group and self.is_value(item)
|
||||
new_identifiers = self.identifier_data.add_identifier(item)
|
||||
new_identifiers = self.identifier_data.get_child(item)
|
||||
if is_group:
|
||||
return Group(
|
||||
identifier_data=new_identifiers,
|
||||
@@ -381,7 +381,7 @@ class Group(Value):
|
||||
dict access. These are casted to `str` for you.
|
||||
"""
|
||||
path = tuple(str(p) for p in nested_path)
|
||||
identifier_data = self.identifier_data.add_identifier(*path)
|
||||
identifier_data = self.identifier_data.get_child(*path)
|
||||
await self.driver.clear(identifier_data)
|
||||
|
||||
def is_group(self, item: Any) -> bool:
|
||||
@@ -499,7 +499,7 @@ class Group(Value):
|
||||
else:
|
||||
default = poss_default
|
||||
|
||||
identifier_data = self.identifier_data.add_identifier(*path)
|
||||
identifier_data = self.identifier_data.get_child(*path)
|
||||
try:
|
||||
raw = await self.driver.get(identifier_data)
|
||||
except KeyError:
|
||||
@@ -583,7 +583,7 @@ class Group(Value):
|
||||
The value to store.
|
||||
"""
|
||||
path = tuple(str(p) for p in nested_path)
|
||||
identifier_data = self.identifier_data.add_identifier(*path)
|
||||
identifier_data = self.identifier_data.get_child(*path)
|
||||
if isinstance(value, dict):
|
||||
value = _str_key_dict(value)
|
||||
await self.driver.set(identifier_data, value=value)
|
||||
|
||||
@@ -109,6 +109,28 @@ class IdentifierData:
|
||||
def __hash__(self) -> int:
|
||||
return hash((self.uuid, self.category, self.primary_key, self.identifiers))
|
||||
|
||||
def get_child(self, *keys: str) -> "IdentifierData":
|
||||
if not all(isinstance(i, str) for i in keys):
|
||||
raise ValueError("Identifiers must be strings.")
|
||||
|
||||
primary_keys = self.primary_key
|
||||
identifiers = self.identifiers
|
||||
num_missing_pkeys = self.primary_key_len - len(self.primary_key)
|
||||
if num_missing_pkeys > 0:
|
||||
primary_keys += keys[:num_missing_pkeys]
|
||||
if len(keys) > num_missing_pkeys:
|
||||
identifiers += keys[num_missing_pkeys:]
|
||||
|
||||
return IdentifierData(
|
||||
self.cog_name,
|
||||
self.uuid,
|
||||
self.category,
|
||||
primary_keys,
|
||||
identifiers,
|
||||
self.primary_key_len,
|
||||
self.is_custom,
|
||||
)
|
||||
|
||||
def add_identifier(self, *identifier: str) -> "IdentifierData":
|
||||
if not all(isinstance(i, str) for i in identifier):
|
||||
raise ValueError("Identifiers must be strings.")
|
||||
|
||||
Reference in New Issue
Block a user