-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for decimal logical type (#151)
* Add support for decimal logical type https://avro.apache.org/docs/1.11.1/specification/#decimal * DecimalType to use precision and scale from schema * Make decimal logical type conditional by Avro::VERSION * Set value class to Numeric * Exclude strings from supported inputs * Remove support for Rational * Better method naming to follow convention for boolean methods * Update README.md with decimal type * Restrict decimal type to BigDecimal, Float and Integer * Fix input and value classes for DecimalType
- Loading branch information
Showing
9 changed files
with
183 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bigdecimal' | ||
require 'bigdecimal/util' | ||
require 'avromatic/model/types/abstract_type' | ||
|
||
module Avromatic | ||
module Model | ||
module Types | ||
class DecimalType < AbstractType | ||
VALUE_CLASSES = [::BigDecimal].freeze | ||
INPUT_CLASSES = [::BigDecimal, ::Float, ::Integer].freeze | ||
|
||
attr_reader :precision, :scale | ||
|
||
def initialize(precision:, scale: 0) | ||
super() | ||
@precision = precision | ||
@scale = scale | ||
end | ||
|
||
def value_classes | ||
VALUE_CLASSES | ||
end | ||
|
||
def input_classes | ||
INPUT_CLASSES | ||
end | ||
|
||
def name | ||
"decimal(#{precision}, #{scale})" | ||
end | ||
|
||
def coerce(input) | ||
case input | ||
when ::NilClass, ::BigDecimal | ||
input | ||
when ::Float, ::Integer | ||
input.to_d | ||
else | ||
raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}") | ||
end | ||
end | ||
|
||
def coercible?(input) | ||
input.nil? || input_classes.any? { |input_class| input.is_a?(input_class) } | ||
end | ||
|
||
def coerced?(value) | ||
value.nil? || value_classes.any? { |value_class| value.is_a?(value_class) } | ||
end | ||
|
||
def serialize(value, _strict) | ||
value | ||
end | ||
|
||
def referenced_model_classes | ||
EMPTY_ARRAY | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"type": "record", | ||
"name": "logical_types_with_decimal", | ||
"namespace": "test", | ||
"fields": [ | ||
{ | ||
"name": "date", | ||
"type": { | ||
"type": "int", | ||
"logicalType": "date" | ||
} | ||
}, | ||
{ | ||
"name": "ts_msec", | ||
"type": { | ||
"type": "long", | ||
"logicalType": "timestamp-millis" | ||
} | ||
}, | ||
{ | ||
"name": "ts_usec", | ||
"type": { | ||
"type": "long", | ||
"logicalType": "timestamp-micros" | ||
} | ||
}, | ||
{ | ||
"name": "decimal", | ||
"type": { | ||
"type": "bytes", | ||
"logicalType": "decimal", | ||
"precision": 4, | ||
"scale": 2 | ||
} | ||
}, | ||
{ | ||
"name": "unknown", | ||
"type": { | ||
"type": "int", | ||
"logicalType": "foobar" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters