Skip to content

Commit c69ef17

Browse files
stenlarssonkou
authored andcommitted
Add GArrowPadOptions
1 parent 1e057e5 commit c69ef17

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ G_BEGIN_DECLS
287287
*
288288
* #GArrowModeOptions is a class to customize the `mode` function.
289289
*
290+
* #GArrowPadOptions is a class to customize the padding functions such as
291+
* `utf8_lpad`, `utf8_rpad`, `utf8_center`, `ascii_lpad`, `ascii_rpad`, and
292+
* `ascii_center`.
293+
*
290294
* There are many functions to compute data on an array.
291295
*/
292296

@@ -8016,6 +8020,147 @@ garrow_mode_options_new(void)
80168020
return GARROW_MODE_OPTIONS(g_object_new(GARROW_TYPE_MODE_OPTIONS, NULL));
80178021
}
80188022

8023+
enum {
8024+
PROP_PAD_OPTIONS_WIDTH = 1,
8025+
PROP_PAD_OPTIONS_PADDING,
8026+
PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING,
8027+
};
8028+
8029+
G_DEFINE_TYPE(GArrowPadOptions, garrow_pad_options, GARROW_TYPE_FUNCTION_OPTIONS)
8030+
8031+
static void
8032+
garrow_pad_options_set_property(GObject *object,
8033+
guint prop_id,
8034+
const GValue *value,
8035+
GParamSpec *pspec)
8036+
{
8037+
auto options = garrow_pad_options_get_raw(GARROW_PAD_OPTIONS(object));
8038+
8039+
switch (prop_id) {
8040+
case PROP_PAD_OPTIONS_WIDTH:
8041+
options->width = g_value_get_int64(value);
8042+
break;
8043+
case PROP_PAD_OPTIONS_PADDING:
8044+
options->padding = g_value_get_string(value);
8045+
break;
8046+
case PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING:
8047+
options->lean_left_on_odd_padding = g_value_get_boolean(value);
8048+
break;
8049+
default:
8050+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8051+
break;
8052+
}
8053+
}
8054+
8055+
static void
8056+
garrow_pad_options_get_property(GObject *object,
8057+
guint prop_id,
8058+
GValue *value,
8059+
GParamSpec *pspec)
8060+
{
8061+
auto options = garrow_pad_options_get_raw(GARROW_PAD_OPTIONS(object));
8062+
8063+
switch (prop_id) {
8064+
case PROP_PAD_OPTIONS_WIDTH:
8065+
g_value_set_int64(value, options->width);
8066+
break;
8067+
case PROP_PAD_OPTIONS_PADDING:
8068+
g_value_set_string(value, options->padding.c_str());
8069+
break;
8070+
case PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING:
8071+
g_value_set_boolean(value, options->lean_left_on_odd_padding);
8072+
break;
8073+
default:
8074+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8075+
break;
8076+
}
8077+
}
8078+
8079+
static void
8080+
garrow_pad_options_init(GArrowPadOptions *object)
8081+
{
8082+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
8083+
priv->options =
8084+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::PadOptions());
8085+
}
8086+
8087+
static void
8088+
garrow_pad_options_class_init(GArrowPadOptionsClass *klass)
8089+
{
8090+
auto gobject_class = G_OBJECT_CLASS(klass);
8091+
8092+
gobject_class->set_property = garrow_pad_options_set_property;
8093+
gobject_class->get_property = garrow_pad_options_get_property;
8094+
8095+
arrow::compute::PadOptions options;
8096+
8097+
GParamSpec *spec;
8098+
/**
8099+
* GArrowPadOptions:width:
8100+
*
8101+
* The desired string length.
8102+
*
8103+
* Since: 23.0.0
8104+
*/
8105+
spec = g_param_spec_int64("width",
8106+
"Width",
8107+
"The desired string length",
8108+
0,
8109+
G_MAXINT64,
8110+
options.width,
8111+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8112+
g_object_class_install_property(gobject_class, PROP_PAD_OPTIONS_WIDTH, spec);
8113+
8114+
/**
8115+
* GArrowPadOptions:padding:
8116+
*
8117+
* What to pad the string with. Should be one codepoint (Unicode)/byte (ASCII).
8118+
*
8119+
* Since: 23.0.0
8120+
*/
8121+
spec = g_param_spec_string(
8122+
"padding",
8123+
"Padding",
8124+
"What to pad the string with. Should be one codepoint (Unicode)/byte (ASCII)",
8125+
options.padding.c_str(),
8126+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8127+
g_object_class_install_property(gobject_class, PROP_PAD_OPTIONS_PADDING, spec);
8128+
8129+
/**
8130+
* GArrowPadOptions:lean-left-on-odd-padding:
8131+
*
8132+
* What to do if there is an odd number of padding characters (in case of centered
8133+
* padding). Defaults to aligning on the left (i.e. adding the extra padding character
8134+
* on the right).
8135+
*
8136+
* Since: 23.0.0
8137+
*/
8138+
spec =
8139+
g_param_spec_boolean("lean-left-on-odd-padding",
8140+
"Lean left on odd padding",
8141+
"What to do if there is an odd number of padding characters (in "
8142+
"case of centered padding). Defaults to aligning on the left "
8143+
"(i.e. adding the extra padding character on the right)",
8144+
options.lean_left_on_odd_padding,
8145+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8146+
g_object_class_install_property(gobject_class,
8147+
PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING,
8148+
spec);
8149+
}
8150+
8151+
/**
8152+
* garrow_pad_options_new:
8153+
*
8154+
* Returns: A newly created #GArrowPadOptions.
8155+
*
8156+
* Since: 23.0.0
8157+
*/
8158+
GArrowPadOptions *
8159+
garrow_pad_options_new(void)
8160+
{
8161+
return GARROW_PAD_OPTIONS(g_object_new(GARROW_TYPE_PAD_OPTIONS, NULL));
8162+
}
8163+
80198164
G_END_DECLS
80208165

80218166
arrow::Result<arrow::FieldRef>
@@ -8205,6 +8350,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
82058350
static_cast<const arrow::compute::ModeOptions *>(arrow_options);
82068351
auto options = garrow_mode_options_new_raw(arrow_mode_options);
82078352
return GARROW_FUNCTION_OPTIONS(options);
8353+
} else if (arrow_type_name == "PadOptions") {
8354+
const auto arrow_pad_options =
8355+
static_cast<const arrow::compute::PadOptions *>(arrow_options);
8356+
auto options = garrow_pad_options_new_raw(arrow_pad_options);
8357+
return GARROW_FUNCTION_OPTIONS(options);
82088358
} else {
82098359
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
82108360
return GARROW_FUNCTION_OPTIONS(options);
@@ -8979,3 +9129,23 @@ garrow_mode_options_get_raw(GArrowModeOptions *options)
89799129
return static_cast<arrow::compute::ModeOptions *>(
89809130
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
89819131
}
9132+
9133+
GArrowPadOptions *
9134+
garrow_pad_options_new_raw(const arrow::compute::PadOptions *arrow_options)
9135+
{
9136+
return GARROW_PAD_OPTIONS(g_object_new(GARROW_TYPE_PAD_OPTIONS,
9137+
"width",
9138+
arrow_options->width,
9139+
"padding",
9140+
arrow_options->padding.c_str(),
9141+
"lean-left-on-odd-padding",
9142+
arrow_options->lean_left_on_odd_padding,
9143+
NULL));
9144+
}
9145+
9146+
arrow::compute::PadOptions *
9147+
garrow_pad_options_get_raw(GArrowPadOptions *options)
9148+
{
9149+
return static_cast<arrow::compute::PadOptions *>(
9150+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
9151+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,4 +1433,17 @@ GARROW_AVAILABLE_IN_23_0
14331433
GArrowModeOptions *
14341434
garrow_mode_options_new(void);
14351435

1436+
#define GARROW_TYPE_PAD_OPTIONS (garrow_pad_options_get_type())
1437+
GARROW_AVAILABLE_IN_23_0
1438+
G_DECLARE_DERIVABLE_TYPE(
1439+
GArrowPadOptions, garrow_pad_options, GARROW, PAD_OPTIONS, GArrowFunctionOptions)
1440+
struct _GArrowPadOptionsClass
1441+
{
1442+
GArrowFunctionOptionsClass parent_class;
1443+
};
1444+
1445+
GARROW_AVAILABLE_IN_23_0
1446+
GArrowPadOptions *
1447+
garrow_pad_options_new(void);
1448+
14361449
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,8 @@ GArrowModeOptions *
241241
garrow_mode_options_new_raw(const arrow::compute::ModeOptions *arrow_options);
242242
arrow::compute::ModeOptions *
243243
garrow_mode_options_get_raw(GArrowModeOptions *options);
244+
245+
GArrowPadOptions *
246+
garrow_pad_options_new_raw(const arrow::compute::PadOptions *arrow_options);
247+
arrow::compute::PadOptions *
248+
garrow_pad_options_get_raw(GArrowPadOptions *options);

c_glib/test/test-pad-options.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestPadOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::PadOptions.new
23+
end
24+
25+
def test_width_property
26+
assert_equal(0, @options.width)
27+
@options.width = 5
28+
assert_equal(5, @options.width)
29+
end
30+
31+
def test_padding_property
32+
assert_equal(" ", @options.padding)
33+
@options.padding = "0"
34+
assert_equal("0", @options.padding)
35+
end
36+
37+
def test_lean_left_on_odd_padding_property
38+
assert do
39+
@options.lean_left_on_odd_padding?
40+
end
41+
@options.lean_left_on_odd_padding = false
42+
assert do
43+
not @options.lean_left_on_odd_padding?
44+
end
45+
end
46+
47+
def test_utf8_center_function
48+
args = [
49+
Arrow::ArrayDatum.new(build_string_array(["a", "ab", "abc"])),
50+
]
51+
utf8_center_function = Arrow::Function.find("utf8_center")
52+
53+
@options.width = 5
54+
@options.padding = " "
55+
result = utf8_center_function.execute(args, @options).value
56+
assert_equal(build_string_array([" a ", " ab ", " abc "]), result)
57+
58+
@options.lean_left_on_odd_padding = false
59+
result = utf8_center_function.execute(args, @options).value
60+
assert_equal(build_string_array([" a ", " ab ", " abc "]), result)
61+
end
62+
end

0 commit comments

Comments
 (0)