deimos-ruby 2.4.0.pre.beta8 → 2.4.0.pre.beta9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1b2dfa779652664bad07fb938a1f4472bca69b7c61c21b4cb6fe028c86061a9
4
- data.tar.gz: 0bf9e789a83563e10b3bd885de8736c5b4487268e4b9cc1225ac8f8f3ee2a866
3
+ metadata.gz: 63128b3ded679a1adf4a45cf2ba2cd18e5734f493d707a60d720df1d2112b249
4
+ data.tar.gz: 829675d8502e3229f79ea5a57f417f181e825c15f0d87f60ff4d1779580b458e
5
5
  SHA512:
6
- metadata.gz: b089bcb150fabaabf23b3d4431f8de937080961b298c3160c7b02db5e41468f524b9d0f8b634f3b69ae99dadf12e2c67b06d420c0821f24ff42eb4c7f6222b2e
7
- data.tar.gz: 41bf155c6813975fdda7818d9199a1334572e70601e7a16076430325f0e1bbe4a9983c0169eaac0e91ede2081947c762a1e6a618f379ad1ade21c78711cfcd49
6
+ metadata.gz: b6fe15786e0c5ec7bc173354ace9763ae911b922b4e3f6312e09e5cdf56071076a72d0cb561658826a98ac1214bf58c97ae3124d690408a42a6530d6fe33d7eb
7
+ data.tar.gz: fa6ff30280e567c252c7d763d25c2726669ce2809efcd52bf76ac8f5d8d93538bd93f5d98ba5858e97d57217c794eccb2f8983338db9cd8259218ed1f87715e4
data/README.md CHANGED
@@ -281,9 +281,15 @@ MyProducer.publish({
281
281
  })
282
282
  ```
283
283
 
284
+ ### Protobuf and Key Schemas
285
+
284
286
  > [!IMPORTANT]
285
287
  > Protobuf should *not* be used as a key schema, since the binary encoding is [unstable](https://protobuf.dev/programming-guides/encoding/#implications) and may break partitioning. Deimos will automatically convert keys to sorted JSON, and will use JSON Schema in the schema registry.
286
288
 
289
+ To enable integration with [buf](https://buf.build/), Deimos provides a Rake task that will auto-generate `.proto` files. This task can be run in CI before running `buf push`. This way, downstream systems that want to use the generated key schemas can do so using their own tech stack.
290
+
291
+ bundle exec rake deimos:generate_key_protos
292
+
287
293
  ## Instrumentation
288
294
 
289
295
  Deimos will send events through the [Karafka instrumentation monitor](https://karafka.io/docs/Monitoring-and-Logging/#subscribing-to-the-instrumentation-events).
@@ -10,9 +10,10 @@ module Deimos
10
10
  end
11
11
 
12
12
  def producer_class
13
- self.producer_classes.first
13
+ self.producer_classes&.first
14
14
  end
15
15
  end
16
+
16
17
  module Topic
17
18
  (FIELDS + [:producer_class]).each do |field|
18
19
  define_method(field) do |*args|
@@ -56,6 +56,29 @@ module Deimos
56
56
  )
57
57
  end
58
58
 
59
+ # @param file [String]
60
+ # @param field_name [String]
61
+ def write_key_proto(file, field_name)
62
+ return if field_name.nil?
63
+
64
+ proto = proto_schema
65
+ package = proto.file_descriptor.to_proto.package
66
+ writer = SchemaRegistry::Output::ProtoText::Writer.new
67
+ info = SchemaRegistry::Output::ProtoText::ParseInfo.new(writer, package)
68
+ writer.write_line(%(syntax = "proto3";))
69
+ writer.write_line("package #{package};")
70
+ writer.writenl
71
+
72
+ field = proto.to_proto.field.find { |f| f.name == field_name.to_s }
73
+ writer.write_line("message #{proto.to_proto.name}Key {")
74
+ writer.indent
75
+ SchemaRegistry::Output::ProtoText.write_field(info, field)
76
+ writer.dedent
77
+ writer.write_line("}")
78
+ path = "#{file}/#{package.gsub('.', '/')}/#{proto.to_proto.name.underscore}_key.proto"
79
+ File.write(path, writer.string)
80
+ end
81
+
59
82
  end
60
83
  end
61
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Deimos
4
- VERSION = '2.4.0-beta8'
4
+ VERSION = '2.4.0-beta9'
5
5
  end
data/lib/deimos.rb CHANGED
@@ -72,9 +72,18 @@ module Deimos
72
72
  klass
73
73
  end
74
74
 
75
+ # @param topic_name [String]
76
+ # @return [SchemaBackends::Base]
77
+ def schema_backend_for(topic_name)
78
+ config = Deimos.karafka_config_for(topic: topic_name)
79
+ self.schema_backend(schema: config.schema,
80
+ namespace: config.namespace,
81
+ backend: config.schema_backend || Deimos.config.schema.backend)
82
+ end
83
+
75
84
  # @param schema [String, Symbol]
76
85
  # @param namespace [String]
77
- # @return [Class<Deimos::SchemaBackends::Base>]
86
+ # @return [Deimos::SchemaBackends::Base]
78
87
  def schema_backend(schema:, namespace:, backend: Deimos.config.schema.backend)
79
88
  if config.schema.use_schema_classes
80
89
  # Initialize an instance of the provided schema
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'generators/deimos/schema_class_generator'
4
4
  require 'optparse'
5
+ require 'deimos/schema_backends/proto_schema_registry'
5
6
 
6
7
  namespace :deimos do
7
8
  desc 'Starts Deimos in the rails environment'
@@ -40,4 +41,17 @@ namespace :deimos do
40
41
  Deimos::Generators::SchemaClassGenerator.start
41
42
  end
42
43
 
44
+ desc 'Output Protobuf key schemas'
45
+ task generate_key_protos: :environment do
46
+ puts "Generating Protobuf key schemas"
47
+ Deimos.karafka_configs.each do |config|
48
+ next if config.try(:producer_class).blank? ||
49
+ !Deimos.schema_backend_for(config.name).is_a?(Deimos::SchemaBackends::ProtoSchemaRegistry) ||
50
+ config.deserializers[:key].try(:key_field).blank?
51
+
52
+ puts "Writing key proto for #{config.name}"
53
+ backend = Deimos.schema_backend_for(config.name)
54
+ backend.write_key_proto('protos', config.deserializers[:key].key_field)
55
+ end
56
+ end
43
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deimos-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0.pre.beta8
4
+ version: 2.4.0.pre.beta9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner