avromatic 0.24.0 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c482b56c1e0e8d3f9271d7a7e61e763a9dffdd83
4
- data.tar.gz: 0005f4ef6ff0abdc4a484b67873de53fab595244
3
+ metadata.gz: c9b90e592c9479ce33ee18c69e7015e9f6f1a555
4
+ data.tar.gz: 681a4f77f781c1c005425f949f385cd51bf666d3
5
5
  SHA512:
6
- metadata.gz: 3ad6910fa3bda579cc807aec5b2e374d7821f7fb8c47b43de99b58c738b822b4f95d884594ec89a6f083baac97b018621d6ff70e9795caf8c050b96f3c4cc974
7
- data.tar.gz: d11cb1499b84dc7b4dfea172f0391eeee61e4f161d35a5e8e44c8181c775693b86902e2b0825e31fe4829058fbef51299a65da1d262c1aacfd689d74cacc9b43
6
+ metadata.gz: c8b9458f552eba32b660d9e40a35ec5f7c0d528dc5a516b0ec4b18d671a299da550233afce32701cec063f1bfae16de0c4f8015aba80acc29e2269776d88b769
7
+ data.tar.gz: 7d8b0347c8db2c7a59c17b34fc9ba96f8956f037db7c108dbaded473ea2fe6f66e6fa503ebd989f55c174fe202679dd58cd139787168e4773276a75d2e4c9fca
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # avromatic changelog
2
2
 
3
+ ## v0.25.0
4
+ - Disallow optional fields in schemas used for keys by default.
5
+
3
6
  ## v0.24.0
4
7
  - Add `Avromatic::IO::DatumWriter` to optimize the encoding of Avro unions
5
8
  from Avromatic models.
data/README.md CHANGED
@@ -150,6 +150,21 @@ end
150
150
  When key and value schemas are both specified, attributes are added to the model
151
151
  for the union of the fields in the two schemas.
152
152
 
153
+ By default, optional fields are not allowed in key schemas since their values may
154
+ be accidentally omitted leading to problems if data is partitioned based on the
155
+ key values.
156
+
157
+ This behavior can be overridden by specifying the `:allow_optional_key_fields`
158
+ option for the model:
159
+
160
+ ```ruby
161
+ class MyTopic
162
+ include Avromatic::Model.build(value_schema_name: :topic_value,
163
+ key_schema_name: :topic_key,
164
+ allow_optional_key_fields: true)
165
+ end
166
+ ```
167
+
153
168
  A model can also be generated as an anonymous class that can be assigned to a
154
169
  constant:
155
170
 
@@ -9,6 +9,15 @@ module Avromatic
9
9
  module Attributes
10
10
  extend ActiveSupport::Concern
11
11
 
12
+ class OptionalFieldError < StandardError
13
+ attr_reader :field
14
+
15
+ def initialize(field)
16
+ @field = field
17
+ super("Optional field not allowed: #{field}")
18
+ end
19
+ end
20
+
12
21
  def self.first_union_schema(field_type)
13
22
  # TODO: This is a hack until I find a better solution for unions with
14
23
  # Virtus. This only handles a union for an optional field with :null
@@ -27,7 +36,12 @@ module Avromatic
27
36
 
28
37
  if key_avro_schema
29
38
  check_for_field_conflicts!
30
- define_avro_attributes(key_avro_schema)
39
+ begin
40
+ define_avro_attributes(key_avro_schema,
41
+ allow_optional: config.allow_optional_key_fields)
42
+ rescue OptionalFieldError => ex
43
+ raise "Optional field '#{ex.field.name}' not allowed in key schema."
44
+ end
31
45
  end
32
46
  define_avro_attributes(avro_schema)
33
47
  end
@@ -55,12 +69,14 @@ module Avromatic
55
69
  value_avro_fields_by_name[name].to_avro
56
70
  end
57
71
 
58
- def define_avro_attributes(schema)
72
+ def define_avro_attributes(schema, allow_optional: true)
59
73
  if schema.type_sym != :record
60
74
  raise "Unsupported schema type '#{schema.type_sym}', only 'record' schemas are supported."
61
75
  end
62
76
 
63
77
  schema.fields.each do |field|
78
+ raise OptionalFieldError.new(field) if !allow_optional && optional?(field)
79
+
64
80
  field_class = avro_field_class(field.type)
65
81
 
66
82
  attribute(field.name,
@@ -4,7 +4,8 @@ module Avromatic
4
4
  # This class holds configuration for a model built from Avro schema(s).
5
5
  class Configuration
6
6
 
7
- attr_reader :avro_schema, :key_avro_schema, :nested_models, :mutable
7
+ attr_reader :avro_schema, :key_avro_schema, :nested_models, :mutable,
8
+ :allow_optional_key_fields
8
9
  delegate :schema_store, to: Avromatic
9
10
 
10
11
  # Either schema(_name) or value_schema(_name), but not both, must be
@@ -19,12 +20,14 @@ module Avromatic
19
20
  # @option options [String, Symbol] :key_schema_name
20
21
  # @option options [Avromatic::ModelRegistry] :nested_models
21
22
  # @option options [Boolean] :mutable, default false
23
+ # @option options [Boolean] :allow_optional_key_fields, default false
22
24
  def initialize(**options)
23
25
  @avro_schema = find_avro_schema(**options)
24
26
  raise ArgumentError.new('value_schema(_name) or schema(_name) must be specified') unless avro_schema
25
27
  @key_avro_schema = find_schema_by_option(:key_schema, **options)
26
28
  @nested_models = options[:nested_models]
27
29
  @mutable = options.fetch(:mutable, false)
30
+ @allow_optional_key_fields = options.fetch(:allow_optional_key_fields, false)
28
31
  end
29
32
 
30
33
  alias_method :value_avro_schema, :avro_schema
@@ -1,3 +1,3 @@
1
1
  module Avromatic
2
- VERSION = '0.24.0'.freeze
2
+ VERSION = '0.25.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avromatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.24.0
4
+ version: 0.25.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify Engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-08 00:00:00.000000000 Z
11
+ date: 2017-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro