Use class matches in resource.update()
This was introduced in 3.10, and seems like a much better way to handle matching a type. Signed-off-by: Nic Cope <nicc@rk0n.org>
This commit is contained in:
parent
5fe3b5c9e0
commit
df655cabec
|
@ -36,20 +36,18 @@ def update(r: fnv1.Resource, source: dict | structpb.Struct | pydantic.BaseModel
|
|||
|
||||
The source can be a dictionary, a protobuf Struct, or a Pydantic model.
|
||||
"""
|
||||
# If the resource has a model_dump attribute it's a Pydantic BaseModel.
|
||||
if hasattr(source, "model_dump"):
|
||||
r.resource.update(source.model_dump(exclude_defaults=True, warnings=False))
|
||||
return
|
||||
|
||||
# If the resource has a get_or_create_struct attribute it's a Struct.
|
||||
if hasattr(source, "get_or_create_struct"):
|
||||
# TODO(negz): Use struct_to_dict and update to match other semantics?
|
||||
r.resource.MergeFrom(source)
|
||||
return
|
||||
|
||||
# If it has neither, it must be a dictionary.
|
||||
r.resource.update(source)
|
||||
return
|
||||
match source:
|
||||
case pydantic.BaseModel():
|
||||
r.resource.update(source.model_dump(exclude_defaults=True, warnings=False))
|
||||
case structpb.Struct():
|
||||
# TODO(negz): Use struct_to_dict and update to match other semantics?
|
||||
r.resource.MergeFrom(source)
|
||||
case dict():
|
||||
r.resource.update(source)
|
||||
case _:
|
||||
t = type(source)
|
||||
msg = f"Unsupported type: {t}"
|
||||
raise TypeError(msg)
|
||||
|
||||
|
||||
def dict_to_struct(d: dict) -> structpb.Struct:
|
||||
|
|
Loading…
Reference in New Issue