From c632b0c1ad110f1655465416bdf557f78084aa1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Fern=C3=A1ndez?= <7312236+fernandezcuesta@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:07:47 +0100 Subject: [PATCH] feat: use json_format.ParseDict to convert dicts to structs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com> --- crossplane/function/resource.py | 4 +-- tests/test_resource.py | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/crossplane/function/resource.py b/crossplane/function/resource.py index 0892e7d..c33e7aa 100644 --- a/crossplane/function/resource.py +++ b/crossplane/function/resource.py @@ -58,9 +58,7 @@ def dict_to_struct(d: dict) -> structpb.Struct: function makes it possible to work with a Python dict, then convert it to a struct in a RunFunctionResponse. """ - s = structpb.Struct() - s.update(d) - return s + return json_format.ParseDict(d, structpb.Struct()) def struct_to_dict(s: structpb.Struct) -> dict: diff --git a/tests/test_resource.py b/tests/test_resource.py index c04ca14..52de5f5 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -244,6 +244,66 @@ class TestResource(unittest.TestCase): dataclasses.asdict(case.want), dataclasses.asdict(got), "-want, +got" ) + def test_dict_to_struct(self) -> None: + @dataclasses.dataclass + class TestCase: + reason: str + d: dict + want: structpb.Struct + + cases = [ + TestCase( + reason="Convert an empty dictionary to a struct.", + d={}, + want=structpb.Struct(), + ), + TestCase( + reason="Convert a dictionary with a single field to a struct.", + d={"foo": "bar"}, + want=structpb.Struct( + fields={"foo": structpb.Value(string_value="bar")} + ), + ), + TestCase( + reason="Convert a nested dictionary to a struct.", + d={"foo": {"bar": "baz"}}, + want=structpb.Struct( + fields={ + "foo": structpb.Value( + struct_value=structpb.Struct( + fields={"bar": structpb.Value(string_value="baz")} + ) + ) + } + ), + ), + TestCase( + reason="Convert a nested dictionary containing lists to a struct.", + d={"foo": {"bar": ["baz", "qux"]}}, + want=structpb.Struct( + fields={ + "foo": structpb.Value( + struct_value=structpb.Struct( + fields={ + "bar": structpb.Value( + list_value=structpb.ListValue( + values=[ + structpb.Value(string_value="baz"), + structpb.Value(string_value="qux"), + ] + ) + ) + } + ) + ) + } + ), + ), + ] + for case in cases: + got = resource.dict_to_struct(case.d) + self.assertEqual(case.want, got, "-want, +got") + def test_struct_to_dict(self) -> None: @dataclasses.dataclass class TestCase: