Skip to content

Commit c391ee1

Browse files
authored
get_field_kind is added. (#1150)
* get_field_kind is added. * Mistakenly added file is removed.
1 parent 628abe2 commit c391ee1

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

hazelcast/include/hazelcast/client/serialization/pimpl/compact/compact.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ class compact_serializer
9090
class HAZELCAST_API compact_reader
9191
{
9292
public:
93+
/**
94+
* Returns the kind of the field for the given field name.
95+
* <p>
96+
* If the field with the given name does not exist,
97+
* {@link field_kind#NOT_AVAILABLE} is returned.
98+
* <p>
99+
* This method can be used to check the existence of a field, which can be
100+
* useful when the class is evolved.
101+
*
102+
* @param field_name name of the field.
103+
* @return kind of the field
104+
*/
105+
field_kind get_field_kind(const std::string& field_name);
106+
93107
/**
94108
* Reads a boolean.
95109
*

hazelcast/src/hazelcast/client/compact.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,18 @@ compact_reader::read_var_size_position(
25222522
: offset + data_start_position;
25232523
}
25242524

2525+
field_kind
2526+
compact_reader::get_field_kind(const std::string& field_name)
2527+
{
2528+
auto descriptor = schema.get_field(field_name);
2529+
2530+
if (!descriptor) {
2531+
return field_kind::NOT_AVAILABLE;
2532+
}
2533+
2534+
return descriptor->kind;
2535+
}
2536+
25252537
bool
25262538
compact_reader::read_boolean(const std::string& fieldName)
25272539
{

hazelcast/src/hazelcast/client/compact.cpp'

Whitespace-only changes.

hazelcast/test/src/compact/compact_serialization_test.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ TEST_F(CompactSerializationTest, test_field_order_fixed_size)
116116
check_schema_field(schema, "isHired", 16, -1, 1);
117117
}
118118

119+
TEST_F(CompactSerializationTest, test_get_field_kind)
120+
{
121+
schema_writer schema_writer("typeName");
122+
auto writer = serialization::pimpl::create_compact_writer(&schema_writer);
123+
serialization::hz_serializer<employee_dto>::write(employee_dto{}, writer);
124+
auto schema = std::move(schema_writer).build();
125+
126+
auto data = serialization_service().to_data(employee_dto{});
127+
128+
serialization::object_data_input input{
129+
boost::endian::order::little,
130+
data.to_byte_array(),
131+
0,
132+
serialization_service().get_portable_serializer(),
133+
serialization_service().get_compact_serializer(),
134+
serialization_service().get_data_serializer(),
135+
nullptr
136+
};
137+
138+
auto reader = serialization::pimpl::create_compact_reader(
139+
serialization_service().get_compact_serializer(), input, schema);
140+
141+
EXPECT_EQ(reader.get_field_kind("age"), serialization::field_kind::INT32);
142+
EXPECT_EQ(reader.get_field_kind("non_existent_field"),
143+
serialization::field_kind::NOT_AVAILABLE);
144+
}
145+
119146
TEST_F(CompactSerializationTest, test_schema_writer_counts)
120147
{
121148
using serialization::field_kind;

hazelcast/test/src/compact/serialization/employee_dto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ struct hz_serializer<test::compact::employee_dto>
6666

6767
return test::compact::employee_dto{ age, rank, id, isHired, isFired };
6868
}
69+
70+
static std::string type_name() { return "employee_dto"; }
6971
};
7072

7173
} // namespace serialization

0 commit comments

Comments
 (0)