-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for logical types #21
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,16 @@ | ||
module Avromatic | ||
module Model | ||
module LogicalTypes | ||
|
||
LOGICAL_TYPE_MAP = { | ||
'date' => Date, | ||
'timestamp-micros' => Time, | ||
'timestamp-millis' => Time | ||
}.freeze | ||
|
||
def self.value_class(logical_type) | ||
LOGICAL_TYPE_MAP.fetch(logical_type, nil) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Avromatic | ||
VERSION = '0.7.1'.freeze | ||
VERSION = '0.8.0'.freeze | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
record :logical_types, namespace: :test do | ||
required :date, :int, logical_type: 'date' | ||
required :ts_msec, :long, logical_type: 'timestamp-millis' | ||
required :ts_usec, :long, logical_type: 'timestamp-micros' | ||
required :unknown, :int, logical_type: 'foobar' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"type": "record", | ||
"name": "logical_types", | ||
"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": "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
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 |
---|---|---|
@@ -1,11 +1,18 @@ | ||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
require 'simplecov' | ||
|
||
SimpleCov.start | ||
SimpleCov.start do | ||
add_filter 'spec' | ||
minimum_coverage 99 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cheater ;) |
||
end | ||
|
||
require 'avromatic' | ||
|
||
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } | ||
|
||
RSpec.configure do |config| | ||
config.extend LogicalTypesHelper | ||
|
||
config.before do | ||
Avromatic.logger = Logger.new('log/test.log') | ||
Avromatic.registry_url = 'http://registry.example.com' | ||
|
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,45 @@ | ||
# This examples expects let-variables to be defined for: | ||
# decoded: a model instance based on the encoded_value | ||
shared_examples_for "logical type encoding and decoding" do | ||
context "logical types" do | ||
let(:schema_name) { 'test.logical_types' } | ||
let(:test_class) do | ||
Avromatic::Model.model(schema_name: schema_name) | ||
end | ||
let(:now) { Time.now } | ||
|
||
with_logical_types do | ||
context "supported" do | ||
let(:values) do | ||
{ | ||
date: Date.today, | ||
ts_msec: Time.at(now.to_i, now.usec / 1000 * 1000), | ||
ts_usec: now, | ||
unknown: 42 | ||
} | ||
end | ||
|
||
it "encodes and decodes instances" do | ||
expect(decoded).to eq(instance) | ||
end | ||
end | ||
end | ||
|
||
without_logical_types do | ||
context "unsupported" do | ||
let(:values) do | ||
{ | ||
date: (Date.today - Date.new(1970, 1, 1)).to_i, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth a |
||
ts_msec: now.to_i + now.usec / 1000 * 1000, | ||
ts_usec: now.to_i * 1_000_000 + now.usec, | ||
unknown: 42 | ||
} | ||
end | ||
|
||
it "encodes and decodes instances" do | ||
expect(decoded).to eq(instance) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module LogicalTypesHelper | ||
|
||
def with_logical_types | ||
yield if logical_types? | ||
end | ||
|
||
def without_logical_types | ||
yield unless logical_types? | ||
end | ||
|
||
private | ||
|
||
def logical_types? | ||
Avro::Schema.instance_methods.include?(:logical_type) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably just be
LOGICAL_TYPE_MAP[logical_type]
since you're falling back tonil
anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason when I wrote this this felt like it captured more explicitly that its okay for a logical type to not be present, and we're expecting to handle nil. But I don't feel that strongly about it.