Skip to content
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 2 commits into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ appraise 'rails4_2' do
end

appraise 'avro-salsify-fork' do
gem 'avro-salsify-fork', '1.9.0.0', require: 'avro'
gem 'avro-salsify-fork', '1.9.0.1', require: 'avro'
gem 'activesupport', '~> 4.2.6'
gem 'activemodel', '~> 4.2.6'
end
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# avromatic changelog

## v0.8.0
- Add support for logical types. Currently this requires using the
`avro-salsify-fork` gem for logical support with Ruby.

## v0.7.1
- Raise a more descriptive error when attempting to generate a model for a
non-record Avro type.
Expand Down
2 changes: 1 addition & 1 deletion avromatic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'webmock'
spec.add_development_dependency 'avro-builder', '>= 0.7.0'
spec.add_development_dependency 'avro-builder', '>= 0.11.0'
# For FakeSchemaRegistryServer
spec.add_development_dependency 'sinatra'
spec.add_development_dependency 'salsify_rubocop', '~> 0.42.0'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/avro_salsify_fork.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source "https://rubygems.org"

gem "avro-salsify-fork", "1.9.0.0", :require => "avro"
gem "avro-salsify-fork", "1.9.0.1", :require => "avro"
gem "activesupport", "~> 4.2.6"
gem "activemodel", "~> 4.2.6"

Expand Down
1 change: 1 addition & 0 deletions lib/avromatic/model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'avromatic/model/builder'
require 'avromatic/model/message_decoder'
require 'avromatic/model/type_registry'
require 'avromatic/model/logical_types'

module Avromatic
module Model
Expand Down
5 changes: 5 additions & 0 deletions lib/avromatic/model/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def avro_field_class(field_type)
custom_type = Avromatic.type_registry.fetch(field_type)
return custom_type.value_class if custom_type.value_class

if field_type.respond_to?(:logical_type)
value_class = Avromatic::Model::LogicalTypes.value_class(field_type.logical_type)
return value_class if value_class
end

case field_type.type_sym
when :string, :bytes, :fixed
String
Expand Down
16 changes: 16 additions & 0 deletions lib/avromatic/model/logical_types.rb
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)
Copy link
Member

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 to nil anyway.

Copy link
Collaborator Author

@tjwp tjwp Aug 29, 2016

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.

end
end
end
end
2 changes: 1 addition & 1 deletion lib/avromatic/version.rb
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
6 changes: 6 additions & 0 deletions spec/avro/dsl/test/logical_types.rb
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
35 changes: 35 additions & 0 deletions spec/avro/schema/test/logical_types.avsc
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"
}
}
]
}
6 changes: 6 additions & 0 deletions spec/avromatic/model/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@
end
end
end

context "logical types" do
let(:schema_name) { 'test.logical_types' }

it_behaves_like "a generated model"
end
end

context "validation" do
Expand Down
5 changes: 5 additions & 0 deletions spec/avromatic/model/messaging_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
end
end

it_behaves_like "logical type encoding and decoding" do
let(:encoded_value) { instance.avro_message_value }
let(:decoded) { test_class.avro_message_decode(encoded_value) }
end

context "custom types" do
let(:schema_name) { 'test.named_type' }
let(:test_class) do
Expand Down
5 changes: 5 additions & 0 deletions spec/avromatic/model/raw_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
end
end

it_behaves_like "logical type encoding and decoding" do
let(:encoded_value) { instance.avro_raw_value }
let(:decoded) { test_class.avro_raw_decode(value: encoded_value) }
end

context "custom types" do
let(:schema_name) { 'test.named_type' }
let(:test_class) do
Expand Down
9 changes: 8 additions & 1 deletion spec/spec_helper.rb
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
Copy link
Member

Choose a reason for hiding this comment

The 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'
Expand Down
45 changes: 45 additions & 0 deletions spec/support/contexts/logical_types_serialization.rb
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth a let that describes the intent of Date.new(1970, 1, 1)

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
16 changes: 16 additions & 0 deletions spec/support/helpers/logical_types_helper.rb
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