From 25e49cf3bdf787bacfa7f00cb634ff409a384706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Fern=C3=A1ndez?= <7312236+fernandezcuesta@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:37:51 +0200 Subject: [PATCH] fix: structs containing listvalues are properly converted 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> --- .../proto/v1/run_function_pb2_grpc.py | 2 +- .../proto/v1beta1/run_function_pb2_grpc.py | 2 +- crossplane/function/resource.py | 6 ++--- tests/test_resource.py | 22 +++++++++++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/crossplane/function/proto/v1/run_function_pb2_grpc.py b/crossplane/function/proto/v1/run_function_pb2_grpc.py index cc1d191..551cf18 100644 --- a/crossplane/function/proto/v1/run_function_pb2_grpc.py +++ b/crossplane/function/proto/v1/run_function_pb2_grpc.py @@ -5,7 +5,7 @@ import warnings from crossplane.function.proto.v1 import run_function_pb2 as crossplane_dot_function_dot_proto_dot_v1_dot_run__function__pb2 -GRPC_GENERATED_VERSION = '1.66.0' +GRPC_GENERATED_VERSION = '1.66.2' GRPC_VERSION = grpc.__version__ _version_not_supported = False diff --git a/crossplane/function/proto/v1beta1/run_function_pb2_grpc.py b/crossplane/function/proto/v1beta1/run_function_pb2_grpc.py index 04e7b83..156d057 100644 --- a/crossplane/function/proto/v1beta1/run_function_pb2_grpc.py +++ b/crossplane/function/proto/v1beta1/run_function_pb2_grpc.py @@ -5,7 +5,7 @@ import warnings from crossplane.function.proto.v1beta1 import run_function_pb2 as crossplane_dot_function_dot_proto_dot_v1beta1_dot_run__function__pb2 -GRPC_GENERATED_VERSION = '1.66.0' +GRPC_GENERATED_VERSION = '1.66.2' GRPC_VERSION = grpc.__version__ _version_not_supported = False diff --git a/crossplane/function/resource.py b/crossplane/function/resource.py index 322a482..0892e7d 100644 --- a/crossplane/function/resource.py +++ b/crossplane/function/resource.py @@ -18,6 +18,7 @@ import dataclasses import datetime import pydantic +from google.protobuf import json_format from google.protobuf import struct_pb2 as structpb import crossplane.function.proto.v1.run_function_pb2 as fnv1 @@ -69,10 +70,7 @@ def struct_to_dict(s: structpb.Struct) -> dict: protobuf struct. This function makes it possible to convert resources to a dictionary. """ - return { - k: (struct_to_dict(v) if isinstance(v, structpb.Struct) else v) - for k, v in s.items() - } + return json_format.MessageToDict(s, preserving_proto_field_name=True) @dataclasses.dataclass diff --git a/tests/test_resource.py b/tests/test_resource.py index e9c818f..c04ca14 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -275,6 +275,28 @@ class TestResource(unittest.TestCase): ), want={"foo": {"bar": "baz"}}, ), + TestCase( + reason="Convert a nested struct containing ListValues to a dictionary.", + s=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"), + ] + ) + ) + } + ) + ) + } + ), + want={"foo": {"bar": ["baz", "qux"]}}, + ), ] for case in cases: