lib/arrow/field.rb in red-arrow-8.0.0 vs lib/arrow/field.rb in red-arrow-9.0.0
- old
+ new
@@ -15,16 +15,32 @@
# specific language governing permissions and limitations
# under the License.
module Arrow
class Field
+ class << self
+ # @api private
+ def try_convert(value)
+ case value
+ when Hash
+ begin
+ new(value)
+ rescue ArgumentError
+ nil
+ end
+ else
+ nil
+ end
+ end
+ end
+
alias_method :initialize_raw, :initialize
private :initialize_raw
# Creates a new {Arrow::Field}.
#
- # @overload initialize(name, data_type)
+ # @overload initialize(name, data_type, nullable=false)
#
# @param name [String, Symbol] The name of the field.
#
# @param data_type [Arrow::DataType, Hash, String, Symbol] The
# data type of the field.
@@ -32,10 +48,15 @@
# You can specify data type as a description by `Hash`.
#
# See {Arrow::DataType.resolve} how to specify data type
# description.
#
+ # @param nullable [Boolean, nil] (false) Whether the field may contain
+ # zero or more nulls.
+ #
+ # `nil` is processed as `true` (null may be contained).
+ #
# @example Create a field with {Arrow::DataType}s
# Arrow::Field.new("visible", Arrow::BooleanDataType.new)
#
# @example Create a field with data type description
# Arrow::Field.new("visible", :boolean)
@@ -73,10 +94,15 @@
# by `Hash`.
#
# See {Arrow::DataType.resolve} how to specify data type
# description.
#
+ # @option description [Boolean, nil] :nullable Whether the field
+ # may contain zero or more nulls.
+ #
+ # `nil` is processed as `true` (null may be contained).
+ #
# @example Create a field with {Arrow::DataType}s
# Arrow::Field.new(name: "visible",
# data_type: Arrow::BooleanDataType.new)
#
# @example Create a field with data type description
@@ -90,29 +116,44 @@
when 1
description = args[0]
name = nil
data_type = nil
data_type_description = {}
+ nullable = nil
description.each do |key, value|
key = key.to_sym
case key
when :name
name = value
when :data_type
data_type = DataType.resolve(value)
+ when :nullable
+ nullable = value
else
data_type_description[key] = value
end
end
data_type ||= DataType.resolve(data_type_description)
when 2
name = args[0]
+ data_type = args[1]
+ if data_type.is_a?(Hash)
+ data_type = data_type.dup
+ nullable = data_type.delete(:nullable)
+ else
+ nullable = nil
+ end
+ data_type = DataType.resolve(data_type)
+ when 3
+ name = args[0]
data_type = DataType.resolve(args[1])
+ nullable = args[2]
else
- message = "wrong number of arguments (given #{n_args}, expected 1..2)"
+ message = "wrong number of arguments (given #{n_args}, expected 1..3)"
raise ArgumentError, message
end
+ nullable = true if nullable.nil?
- initialize_raw(name, data_type)
+ initialize_raw(name, data_type, nullable)
end
end
end