fix: recursively convert struct to dict

Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com>
This commit is contained in:
Jesús Fernández 2024-08-13 10:10:01 +02:00
parent 50f301a73e
commit 556607ede7
No known key found for this signature in database
GPG Key ID: F009C46F547D9A66
2 changed files with 41 additions and 1 deletions

View File

@ -42,7 +42,10 @@ def struct_to_dict(s: structpb.Struct) -> dict:
protobuf struct. This function makes it possible to convert resources to a
dictionary.
"""
return dict(s)
return {
k: (struct_to_dict(v) if isinstance(v, structpb.Struct) else v)
for k, v in s.items()
}
@dataclasses.dataclass

View File

@ -165,6 +165,43 @@ class TestResource(unittest.TestCase):
dataclasses.asdict(case.want), dataclasses.asdict(got), "-want, +got"
)
def test_struct_to_dict(self) -> None:
@dataclasses.dataclass
class TestCase:
reason: str
s: structpb.Struct
want: dict
cases = [
TestCase(
reason="Convert a struct with no fields to an empty dictionary.",
s=structpb.Struct(),
want={},
),
TestCase(
reason="Convert a struct with a single field to a dictionary.",
s=structpb.Struct(fields={"foo": structpb.Value(string_value="bar")}),
want={"foo": "bar"},
),
TestCase(
reason="Convert a nested struct to a dictionary.",
s=structpb.Struct(
fields={
"foo": structpb.Value(
struct_value=structpb.Struct(
fields={"bar": structpb.Value(string_value="baz")}
)
)
}
),
want={"foo": {"bar": "baz"}},
),
]
for case in cases:
got = resource.struct_to_dict(case.s)
self.assertEqual(case.want, got, "-want, +got")
if __name__ == "__main__":
unittest.main()