Skip to content

Commit 9e32f58

Browse files
author
Daniel Orner
committed
Code review fixes
1 parent 51418ab commit 9e32f58

File tree

9 files changed

+27
-20
lines changed

9 files changed

+27
-20
lines changed

lib/deimos/active_record_consumer.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ def record_attributes(payload)
8585
def _coerce_field(column, val)
8686
return nil if val.nil?
8787

88-
is_integer = begin
89-
val.is_a?(Integer) || (val.is_a?(String) && Integer(val))
90-
rescue StandardError
91-
false
92-
end
88+
if column.type == :datetime
89+
int_val = begin
90+
val.is_a?(Integer) ? val : (val.is_a?(String) && Integer(val))
91+
rescue StandardError
92+
nil
93+
end
9394

94-
if column.type == :datetime && is_integer
95-
return Time.zone.strptime(val.to_s, '%s')
95+
return Time.zone.at(int_val) if int_val
9696
end
9797

9898
val

lib/deimos/message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(payload, producer, topic: nil, key: nil, partition_key: nil)
1818

1919
# Add message_id and timestamp default values if they are in the
2020
# schema and don't already have values.
21-
# @param fields [Array<Name>] existing fields in the schema.
21+
# @param fields [Array<String>] existing name fields in the schema.
2222
def add_fields(fields)
2323
return if @payload.except(:payload_key, :partition_key).blank?
2424

lib/deimos/schema_backends/avro_base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def _generate_key_schema(key_id)
7575
'type' => 'record',
7676
'name' => name,
7777
'namespace' => @namespace,
78-
'doc' => "Key for #{@namespace}.#{@schema}",
78+
'doc' => "Key for #{@namespace}.#{@schema} - autogenerated by Deimos",
7979
'fields' => [
8080
{
8181
'name' => key_id,
@@ -101,7 +101,7 @@ def _field_name_from_schema(value_schema)
101101
# @param schema [String]
102102
# @return [String]
103103
def _key_schema_name(schema)
104-
"#{schema.gsub('-value', '')}_key"
104+
"#{schema}_key"
105105
end
106106
end
107107
end

lib/deimos/schema_backends/avro_schema_coercer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def _is_integer_string?(val)
6767
return false unless val.is_a?(String)
6868

6969
begin
70-
true if Integer(val)
70+
true if Integer(val)
7171
rescue StandardError
7272
false
73-
end
73+
end
7474
end
7575

7676
# @param val [String]
@@ -79,10 +79,10 @@ def _is_float_string?(val)
7979
return false unless val.is_a?(String)
8080

8181
begin
82-
true if Float(val)
82+
true if Float(val)
8383
rescue StandardError
8484
false
85-
end
85+
end
8686
end
8787

8888
# @param val [Object]

lib/deimos/schema_backends/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def coerce_field(_field, _value)
110110
end
111111

112112
# Encode a message key. To be defined by subclass.
113-
# @param key [String] the value to use as the key.
113+
# @param key [String|Hash] the value to use as the key.
114114
# @param key_id [Symbol|String] the field name of the key.
115115
# @param topic [String]
116116
# @return [String]

lib/deimos/test_helpers.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ def sent_messages
4444
end
4545
end
4646

47-
# :nodoc:
47+
# @deprecated
4848
def stub_producers_and_consumers!
4949
warn('stub_producers_and_consumers! is no longer necessary and this method will be removed in 3.0')
5050
end
5151

52-
# :nodoc:
52+
# @deprecated
5353
def stub_producer(_klass)
5454
warn('Stubbing producers is no longer necessary and this method will be removed in 3.0')
5555
end
5656

57-
# :nodoc:
57+
# @deprecated
5858
def stub_consumer(_klass)
5959
warn('Stubbing consumers is no longer necessary and this method will be removed in 3.0')
6060
end
6161

62-
# :nodoc:
62+
# @deprecated
6363
def stub_batch_consumer(_klass)
6464
warn('Stubbing batch consumers is no longer necessary and this method will be removed in 3.0')
6565
end

spec/active_record_consumer_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,12 @@ def fetch_record(klass, payload, _key)
119119
expect(Widget.find_by_test_id('id2').some_int).to eq(4)
120120
end
121121

122+
it 'should coerce int values to datetimes' do
123+
column = Widget.columns.find { |c| c.name == 'some_datetime_int' }
124+
expect(MyConsumer.new.send(:_coerce_field, column, 1_579_046_400)).to eq('2020-01-14 19:00:00 -0500')
125+
expect(MyConsumer.new.send(:_coerce_field, column, '1579046400')).to eq('2020-01-14 19:00:00 -0500')
126+
expect(MyConsumer.new.send(:_coerce_field, column, 'some-other-val')).to eq('some-other-val')
127+
end
128+
122129
end
123130
end
File renamed without changes.

spec/schema_backends/avro_base_shared.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
expect(backend.encode_key('test_id', 1, topic: 'topic')).to eq('itsme')
5454
expect(backend.schema_store.find('MySchema_key', 'com.my-namespace').to_avro).
5555
to eq(
56-
'doc' => 'Key for com.my-namespace.MySchema',
56+
'doc' => 'Key for com.my-namespace.MySchema - autogenerated by Deimos',
5757
'fields' => [
5858
{ 'name' => 'test_id', 'type' => 'string' }
5959
],

0 commit comments

Comments
 (0)