Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert listValues to lists in nested structs #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crossplane/function/proto/v1/run_function_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

Expand Down
2 changes: 1 addition & 1 deletion crossplane/function/proto/v1beta1/run_function_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

Expand Down
20 changes: 16 additions & 4 deletions crossplane/function/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import dataclasses
import datetime
from typing import Any

import pydantic
from google.protobuf import struct_pb2 as structpb
Expand Down Expand Up @@ -62,17 +63,28 @@ def dict_to_struct(d: dict) -> structpb.Struct:
return s


def __to_python_native(v: structpb.Value) -> Any:
"""Convert a protobuf Value to a Python native type."""
if isinstance(v, structpb.ListValue):
return __listvalue_to_list(v)
elif isinstance(v, structpb.Struct):
return struct_to_dict(v)
return v


def __listvalue_to_list(lv: structpb.ListValue) -> list:
"""Convert a protobuf ListValue to a list."""
return [__to_python_native(v) for v in lv]


def struct_to_dict(s: structpb.Struct) -> dict:
"""Create a dict from the supplied Struct well-known type.

Crossplane sends observed and desired resources to a function encoded as a
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 {k: __to_python_native(v) for k, v in s.items()}


@dataclasses.dataclass
Expand Down
22 changes: 22 additions & 0 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ class 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:
Expand Down