Skip to content

Commit d665a82

Browse files
committed
Replace getters and setters with an add_field method
1 parent 730c87a commit d665a82

File tree

3 files changed

+33
-132
lines changed

3 files changed

+33
-132
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 17 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7617,112 +7617,34 @@ garrow_make_struct_options_new(void)
76177617
}
76187618

76197619
/**
7620-
* garrow_make_struct_options_set_field_nullability:
7620+
* garrow_make_struct_options_add_field:
76217621
* @options: A #GArrowMakeStructOptions.
7622-
* @nullability: (element-type gboolean) (nullable) (transfer none): A #GList
7623-
* of nullability values, or %NULL.
7622+
* @name: The name of the field to add.
7623+
* @nullability: Whether the field is nullable.
7624+
* @metadata: (nullable) (element-type utf8 utf8): A #GHashTable for the field's
7625+
* metadata, or %NULL.
76247626
*
7625-
* Sets the nullability information for each struct field.
7627+
* Adds a field to the struct options with the specified name, nullability,
7628+
* and optional metadata.
76267629
*
76277630
* Since: 23.0.0
76287631
*/
76297632
void
7630-
garrow_make_struct_options_set_field_nullability(GArrowMakeStructOptions *options,
7631-
GList *nullability)
7633+
garrow_make_struct_options_add_field(GArrowMakeStructOptions *options,
7634+
const char *name,
7635+
gboolean nullability,
7636+
GHashTable *metadata)
76327637
{
76337638
auto arrow_options = static_cast<arrow::compute::MakeStructOptions *>(
76347639
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
7635-
arrow_options->field_nullability.clear();
7636-
if (nullability) {
7637-
for (GList *node = nullability; node; node = node->next) {
7638-
gboolean value = GPOINTER_TO_INT(node->data);
7639-
arrow_options->field_nullability.push_back(value != FALSE);
7640-
}
7641-
}
7642-
}
7643-
7644-
/**
7645-
* garrow_make_struct_options_get_field_nullability:
7646-
* @options: A #GArrowMakeStructOptions.
7647-
*
7648-
* Returns: (element-type gboolean) (transfer full): A #GList of nullability
7649-
* values. It should be freed with g_list_free() when no longer needed.
7650-
*
7651-
* Gets the nullability information for each struct field.
7652-
*
7653-
* Since: 23.0.0
7654-
*/
7655-
GList *
7656-
garrow_make_struct_options_get_field_nullability(GArrowMakeStructOptions *options)
7657-
{
7658-
auto arrow_options = static_cast<arrow::compute::MakeStructOptions *>(
7659-
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
7660-
const auto &arrow_nullability = arrow_options->field_nullability;
7661-
GList *list = NULL;
7662-
for (gsize i = 0; i < arrow_nullability.size(); ++i) {
7663-
gboolean value = arrow_nullability[i] ? TRUE : FALSE;
7664-
list = g_list_prepend(list, GINT_TO_POINTER(value));
7665-
}
7666-
return g_list_reverse(list);
7667-
}
7668-
7669-
/**
7670-
* garrow_make_struct_options_set_field_metadata:
7671-
* @options: A #GArrowMakeStructOptions.
7672-
* @metadata: (element-type GHashTable) (nullable) (transfer none): A #GList of
7673-
* #GHashTable for each field's metadata, or %NULL.
7674-
*
7675-
* Sets the metadata for each struct field.
7676-
*
7677-
* Since: 23.0.0
7678-
*/
7679-
void
7680-
garrow_make_struct_options_set_field_metadata(GArrowMakeStructOptions *options,
7681-
GList *metadata)
7682-
{
7683-
auto arrow_options = static_cast<arrow::compute::MakeStructOptions *>(
7684-
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
7685-
arrow_options->field_metadata.clear();
7640+
arrow_options->field_names.emplace_back(name);
7641+
arrow_options->field_nullability.push_back(nullability != FALSE);
76867642
if (metadata) {
7687-
for (GList *node = metadata; node; node = node->next) {
7688-
auto hash_table = static_cast<GHashTable *>(node->data);
7689-
if (hash_table) {
7690-
arrow_options->field_metadata.push_back(
7691-
garrow_internal_hash_table_to_metadata(hash_table));
7692-
} else {
7693-
arrow_options->field_metadata.push_back(NULLPTR);
7694-
}
7695-
}
7696-
}
7697-
}
7698-
7699-
/**
7700-
* garrow_make_struct_options_get_field_metadata:
7701-
* @options: A #GArrowMakeStructOptions.
7702-
*
7703-
* Returns: (element-type GHashTable) (transfer full): A #GList of #GHashTable
7704-
* for each field's metadata. It should be freed with g_list_free_full() and
7705-
* g_hash_table_unref() when no longer needed.
7706-
*
7707-
* Gets the metadata for each struct field.
7708-
*
7709-
* Since: 23.0.0
7710-
*/
7711-
GList *
7712-
garrow_make_struct_options_get_field_metadata(GArrowMakeStructOptions *options)
7713-
{
7714-
auto arrow_options = static_cast<arrow::compute::MakeStructOptions *>(
7715-
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
7716-
const auto &arrow_metadata = arrow_options->field_metadata;
7717-
GList *list = NULL;
7718-
for (gsize i = 0; i < arrow_metadata.size(); ++i) {
7719-
GHashTable *hash_table = NULL;
7720-
if (arrow_metadata[i] && arrow_metadata[i].get()) {
7721-
hash_table = garrow_internal_hash_table_from_metadata(arrow_metadata[i]);
7722-
}
7723-
list = g_list_prepend(list, hash_table);
7643+
arrow_options->field_metadata.push_back(
7644+
garrow_internal_hash_table_to_metadata(metadata));
7645+
} else {
7646+
arrow_options->field_metadata.push_back(nullptr);
77247647
}
7725-
return g_list_reverse(list);
77267648
}
77277649

77287650
G_END_DECLS

c_glib/arrow-glib/compute.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,20 +1356,9 @@ garrow_make_struct_options_new(void);
13561356

13571357
GARROW_AVAILABLE_IN_23_0
13581358
void
1359-
garrow_make_struct_options_set_field_nullability(GArrowMakeStructOptions *options,
1360-
GList *nullability);
1361-
1362-
GARROW_AVAILABLE_IN_23_0
1363-
GList *
1364-
garrow_make_struct_options_get_field_nullability(GArrowMakeStructOptions *options);
1365-
1366-
GARROW_AVAILABLE_IN_23_0
1367-
void
1368-
garrow_make_struct_options_set_field_metadata(GArrowMakeStructOptions *options,
1369-
GList *metadata);
1370-
1371-
GARROW_AVAILABLE_IN_23_0
1372-
GList *
1373-
garrow_make_struct_options_get_field_metadata(GArrowMakeStructOptions *options);
1359+
garrow_make_struct_options_add_field(GArrowMakeStructOptions *options,
1360+
const char *name,
1361+
gboolean nullability,
1362+
GHashTable *metadata);
13741363

13751364
G_END_DECLS

c_glib/test/test-make-struct-options.rb

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,12 @@ def test_field_names_property
2828
assert_equal(["a", "b", "c"], @options.field_names)
2929
end
3030

31-
def test_field_nullability_property
32-
assert_equal([], @options.field_nullability)
33-
@options.field_names = ["a", "b", "c"]
34-
@options.field_nullability = [true, false, true]
35-
assert_equal([true, false, true], @options.field_nullability)
36-
end
37-
38-
def test_field_metadata_property
39-
assert_equal([], @options.field_metadata)
40-
@options.field_names = ["a", "b"]
41-
metadata1 = {"key1" => "value1", "key2" => "value2"}
42-
metadata2 = {"key3" => "value3"}
43-
@options.field_metadata = [metadata1, metadata2]
44-
result = @options.field_metadata
45-
assert_equal(2, result.size)
46-
assert_equal(metadata1, result[0])
47-
assert_equal(metadata2, result[1])
31+
def test_add_field
32+
@options.add_field("a", true, nil)
33+
@options.add_field("b", false, nil)
34+
metadata = {"key1" => "value1", "key2" => "value2"}
35+
@options.add_field("c", true, metadata)
36+
assert_equal(["a", "b", "c"], @options.field_names)
4837
end
4938

5039
def test_make_struct_function
@@ -54,7 +43,8 @@ def test_make_struct_function
5443
Arrow::ArrayDatum.new(a),
5544
Arrow::ArrayDatum.new(b),
5645
]
57-
@options.field_names = ["a", "b"]
46+
@options.add_field("a", true, nil)
47+
@options.add_field("b", true, nil)
5848
make_struct_function = Arrow::Function.find("make_struct")
5949
result = make_struct_function.execute(args, @options).value
6050

@@ -79,8 +69,8 @@ def test_make_struct_function_with_nullability
7969
Arrow::ArrayDatum.new(a),
8070
Arrow::ArrayDatum.new(b),
8171
]
82-
@options.field_names = ["a", "b"]
83-
@options.field_nullability = [false, true]
72+
@options.add_field("a", false, nil)
73+
@options.add_field("b", true, nil)
8474
make_struct_function = Arrow::Function.find("make_struct")
8575
result = make_struct_function.execute(args, @options).value
8676

@@ -105,10 +95,10 @@ def test_make_struct_function_with_metadata
10595
Arrow::ArrayDatum.new(a),
10696
Arrow::ArrayDatum.new(b),
10797
]
108-
@options.field_names = ["a", "b"]
10998
metadata1 = {"key1" => "value1"}
11099
metadata2 = {"key2" => "value2"}
111-
@options.field_metadata = [metadata1, metadata2]
100+
@options.add_field("a", true, metadata1)
101+
@options.add_field("b", true, metadata2)
112102
make_struct_function = Arrow::Function.find("make_struct")
113103
result = make_struct_function.execute(args, @options).value
114104

0 commit comments

Comments
 (0)