TypeError: Expected Map instance
In the conversion of google-cloud-logging from GRPC to GAPIC using GAX, we ran into the following problem dealing with hash objects not being converted to Map objects.
The generated write_log_entries method is as follows:
# Writes log entries to Stackdriver Logging. All log entries are
# written by this method.
#
# @param log_name [String]
# Optional. A default log resource name that is assigned to all log entries
# in +entries+ that do not specify a value for +log_name+. Example:
# +"projects/my-project/logs/syslog"+. See
# LogEntry.
# @param resource [Google::Api::MonitoredResource]
# Optional. A default monitored resource object that is assigned to all log
# entries in +entries+ that do not specify a value for +resource+. Example:
#
# { "type": "gce_instance",
# "labels": {
# "zone": "us-central1-a", "instance_id": "00000000000000000000" }}
#
# See LogEntry.
# @param labels [Hash{String => String}]
# Optional. Default labels that are added to the +labels+ field of all log
# entries in +entries+. If a log entry already has a label with the same key
# as a label in this parameter, then the log entry's label is not changed.
# See LogEntry.
# @param entries [Array<Google::Logging::V2::LogEntry>]
# Required. The log entries to write. Values supplied for the fields
# +log_name+, +resource+, and +labels+ in this +entries.write+ request are
# added to those log entries that do not provide their own values for the
# fields.
#
# To improve throughput and to avoid exceeding the
# {quota limit}[https://cloud.google.com/logging/quota-policy] for calls to +entries.write+,
# you should write multiple log entries at once rather than
# calling this method for each individual log entry.
# @param partial_success [true, false]
# Optional. Whether valid entries should be written even if some other
# entries fail due to INVALID_ARGUMENT or PERMISSION_DENIED errors. If any
# entry is not written, the response status will be the error associated
# with one of the failed entries and include error details in the form of
# WriteLogEntriesPartialErrors.
# @param options [Google::Gax::CallOptions]
# Overrides the default settings for this call, e.g, timeout,
# retries, etc.
# @return [Google::Logging::V2::WriteLogEntriesResponse]
# @raise [Google::Gax::GaxError] if the RPC is aborted.
# @example
# require "google/cloud/logging/v2/logging_service_v2_api"
#
# LoggingServiceV2Api = Google::Cloud::Logging::V2::LoggingServiceV2Api
#
# logging_service_v2_api = LoggingServiceV2Api.new
# entries = []
# response = logging_service_v2_api.write_log_entries(entries)
def write_log_entries \
entries,
log_name: nil,
resource: nil,
labels: nil,
partial_success: nil,
options: nil
req = Google::Logging::V2::WriteLogEntriesRequest.new(
entries: entries
)
req.log_name = log_name unless log_name.nil?
req.resource = resource unless resource.nil?
req.labels = labels unless labels.nil?
req.partial_success = partial_success unless partial_success.nil?
@write_log_entries.call(req, options)
end
The labels argument is clearly documented as a hash of strings ([Hash{String => String}]). However, when providing a hash of strings, TypeError: Expected Map instance is raised. This is because the protobuf object is inconsistent in how it automatically boxes Hash to Map when setting the value using an accessor method. One option is to track which arguments are going to be Maps and convert them in the GAPIC method:
def write_log_entries \
entries,
log_name: nil,
resource: nil,
labels: nil,
partial_success: nil,
options: nil
req = Google::Logging::V2::WriteLogEntriesRequest.new(
entries: entries
)
req.log_name = log_name unless log_name.nil?
req.resource = resource unless resource.nil?
unless labels.nil?
labels_map = Google::Protobuf::Map.new(:string, :string)
labels.each { |k, v| labels_map[String(k)] = String(v) }
req.labels = labels_map
end
req.partial_success = partial_success unless partial_success.nil?
@write_log_entries.call(req, options)
end
Another option is to skip the use of accessor methods and set all values in the constructor. The protobuf class correctly boxes Hash to Map in the constructor. This is how google-cloud-ruby has chosen to avoid this bug before converting to GAPIC and GAX:
def write_log_entries \
entries,
log_name: nil,
resource: nil,
labels: nil,
partial_success: nil,
options: nil
req = Google::Logging::V2::WriteLogEntriesRequest.new(
{
entries: entries,
log_name: log_name,
resource: resource,
labels: labels,
partial_success: partial_success
}.delete_if { |_, v| v.nil? }
)
@write_log_entries.call(req, options)
end
This should be in the fix of the code generator. I prefer the latter one, more concise and simpler.
I would like the actual bug to be fixed in protobuf as well, but baby steps. :)
FWIW, this issue is also somewhat blocking GoogleCloudPlatform/google-cloud-ruby#971. I have changes for that issue implemented, but get TypeError: Expected repeated field array when running the acceptance tests. Basically, the GAPIC layer's list_log_entries method it tries to set the resource_names attribute on the protobuf object and it raises an error.
@blowmage If you use the constructor then it should work, right?
If the generated Ruby code was setting values in the constructor instead of using the method setters, then yes it would work for how we use the generated code.