Skip to content

Commit

Permalink
Merge pull request #5138 from tchaikov/kafka/proto/defaulted-operator-eq
Browse files Browse the repository at this point in the history
kafka/protocol: use default generated operator==()
  • Loading branch information
Rob Blafford committed Jun 17, 2022
2 parents cb8dad2 + 82fc206 commit afb991d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
32 changes: 2 additions & 30 deletions src/v/kafka/protocol/api_versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,8 @@ struct api_versions_response final {

private:
friend bool operator==(
const kafka::api_versions_response& a,
const kafka::api_versions_response& b) {
return a.data.error_code == b.data.error_code
&& a.data.throttle_time_ms == b.data.throttle_time_ms
&& a.data.finalized_features_epoch
== b.data.finalized_features_epoch
&& a.data.api_keys == b.data.api_keys
&& a.data.supported_features == b.data.supported_features
&& a.data.finalized_features == b.data.finalized_features;
}
const kafka::api_versions_response&, const kafka::api_versions_response&)
= default;
};

inline bool operator==(
const api_versions_response_key& a, const api_versions_response_key& b) {
return a.api_key == b.api_key && a.min_version == b.min_version
&& a.max_version == b.max_version;
}

inline bool operator==(
const kafka::supported_feature_key& a,
const kafka::supported_feature_key& b) {
return a.name == b.name && a.min_version == b.min_version
&& a.max_version == b.max_version;
}

inline bool operator==(
const kafka::finalized_feature_key& a,
const kafka::finalized_feature_key& b) {
return a.name == b.name && a.max_version_level == b.max_version_level
&& a.min_version_level == b.min_version_level;
}

} // namespace kafka
20 changes: 19 additions & 1 deletion src/v/kafka/protocol/schemata/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
# make it easier to audit where sensitive information is used.
import io
import json
import functools
import pathlib
import re
import sys
Expand Down Expand Up @@ -437,6 +436,13 @@ def make_context_field(path):
"FinalizedFeatureKey",
]

# a list of struct types which are ineligible to have default-generated
# `operator==()`, because one or more of its member variables are not
# comparable
WITHOUT_DEFAULT_EQUALITY_OPERATOR = {
'kafka::batch_reader', 'kafka::produce_request_record_data'
}

# The following is a list of tag types which contain fields where their
# respective types are not prefixed with []. The generator special cases these
# as ArrayTypes
Expand Down Expand Up @@ -634,6 +640,10 @@ def structs(self):
res.append(t)
return res

@property
def is_default_comparable(self):
return all(field.is_default_comparable for field in self.fields)


class ArrayType(FieldType):
def __init__(self, value_type):
Expand Down Expand Up @@ -845,6 +855,11 @@ def value_type(self):
def name(self):
return snake_case(self._field["name"])

@property
def is_default_comparable(self):
type_name, _ = self._redpanda_type()
return type_name not in WITHOUT_DEFAULT_EQUALITY_OPERATOR


HEADER_TEMPLATE = """
#pragma once
Expand Down Expand Up @@ -894,6 +909,9 @@ def name(self):
// added by redpanda. see generator.py:make_context_field.
{{ struct.context_field[0] }} {{ struct.context_field[1] -}};
{%- endif %}
{%- if struct.is_default_comparable %}
friend bool operator==(const {{ struct.name }}&, const {{ struct.name }}&) = default;
{%- endif %}
{% endmacro %}
namespace kafka {
Expand Down

0 comments on commit afb991d

Please sign in to comment.