Skip to content

Commit

Permalink
fix: Updated protobuf JSON formatting to support nested protobufs (#797)
Browse files Browse the repository at this point in the history
* fix: Updated protobuf JSON formatting to support nested protobufs

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Cleaner way to differentiate between proto objects and dict objects in
to_api_repr

* Fixed unused import.

* Fixed failing unit test.

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
gkevinzheng and gcf-owl-bot[bot] authored Oct 18, 2023
1 parent 7ae3d59 commit a00c261
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
12 changes: 6 additions & 6 deletions google/cloud/logging_v2/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import json
import re

from google.protobuf.any_pb2 import Any
from google.protobuf.json_format import MessageToDict
from google.protobuf.json_format import Parse
from google.protobuf.message import Message

from google.cloud.logging_v2.resource import Resource
from google.cloud._helpers import _name_from_project_path
Expand Down Expand Up @@ -325,7 +325,7 @@ def _extract_payload(cls, resource):

@property
def payload_pb(self):
if isinstance(self.payload, Any):
if isinstance(self.payload, Message):
return self.payload

@property
Expand All @@ -337,10 +337,10 @@ def to_api_repr(self):
"""API repr (JSON format) for entry."""
info = super(ProtobufEntry, self).to_api_repr()
proto_payload = None
if self.payload_json:
proto_payload = dict(self.payload_json)
elif self.payload_pb:
proto_payload = MessageToDict(self.payload_pb)
if self.payload_pb:
proto_payload = MessageToDict(self.payload)
elif self.payload_json:
proto_payload = dict(self.payload)
info["protoPayload"] = proto_payload
return info

Expand Down
7 changes: 7 additions & 0 deletions tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def test_list_entry_with_auditlog(self):
"methodName": "test",
"resourceName": "test",
"serviceName": "test",
"requestMetadata": {"callerIp": "127.0.0.1"},
}
audit_struct = self._dict_to_struct(audit_dict)

Expand Down Expand Up @@ -223,6 +224,12 @@ def test_list_entry_with_auditlog(self):
protobuf_entry.to_api_repr()["protoPayload"]["methodName"],
audit_dict["methodName"],
)
self.assertEqual(
protobuf_entry.to_api_repr()["protoPayload"]["requestMetadata"][
"callerIp"
],
audit_dict["requestMetadata"]["callerIp"],
)

def test_list_entry_with_requestlog(self):
"""
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/test_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,45 @@ def test_to_api_repr_proto_defaults(self):
}
self.assertEqual(entry.to_api_repr(), expected)

def test_to_api_repr_proto_inner_struct_field(self):
from google.protobuf.json_format import MessageToDict
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
from google.protobuf.struct_pb2 import Struct
from google.protobuf.struct_pb2 import Value

LOG_NAME = "test.log"
inner_struct = Struct(fields={"foo": Value(string_value="bar")})
message = Struct(fields={"inner": Value(struct_value=inner_struct)})

entry = self._make_one(log_name=LOG_NAME, payload=message)
expected = {
"logName": LOG_NAME,
"protoPayload": MessageToDict(message),
"resource": _GLOBAL_RESOURCE._to_dict(),
}
self.assertEqual(entry.to_api_repr(), expected)

def test_to_api_repr_proto_inner_list_field(self):
from google.protobuf.json_format import MessageToDict
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
from google.protobuf.struct_pb2 import ListValue
from google.protobuf.struct_pb2 import Struct
from google.protobuf.struct_pb2 import Value

LOG_NAME = "test.log"
lines = ListValue(
values=[Value(string_value="line1"), Value(string_value="line2")]
)
message = Struct(fields={"lines": Value(list_value=lines)})

entry = self._make_one(log_name=LOG_NAME, payload=message)
expected = {
"logName": LOG_NAME,
"protoPayload": MessageToDict(message),
"resource": _GLOBAL_RESOURCE._to_dict(),
}
self.assertEqual(entry.to_api_repr(), expected)

def test_to_api_repr_proto_explicit(self):
import datetime
from google.protobuf.json_format import MessageToDict
Expand Down

0 comments on commit a00c261

Please sign in to comment.