avro_turf 0.8.1 → 0.11.0
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 +4 -4
- data/.circleci/config.yml +36 -0
- data/.github/workflows/ruby.yml +20 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile +0 -3
- data/README.md +20 -0
- data/avro_turf.gemspec +6 -5
- data/lib/avro_turf.rb +10 -1
- data/lib/avro_turf/cached_confluent_schema_registry.rb +18 -6
- data/lib/avro_turf/confluent_schema_registry.rb +23 -4
- data/lib/avro_turf/disk_cache.rb +83 -0
- data/lib/avro_turf/in_memory_cache.rb +38 -0
- data/lib/avro_turf/messaging.rb +109 -16
- data/lib/avro_turf/schema_store.rb +35 -20
- data/lib/avro_turf/test/fake_confluent_schema_registry_server.rb +15 -3
- data/lib/avro_turf/version.rb +1 -1
- data/spec/cached_confluent_schema_registry_spec.rb +24 -2
- data/spec/confluent_schema_registry_spec.rb +13 -1
- data/spec/disk_cached_confluent_schema_registry_spec.rb +159 -0
- data/spec/messaging_spec.rb +205 -17
- data/spec/schema_store_spec.rb +36 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/confluent_schema_registry_context.rb +8 -5
- data/spec/test/fake_confluent_schema_registry_server_spec.rb +40 -0
- metadata +34 -13
- data/circle.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e2ee4d9598bcaa2ec5738a1130fae0b19be7b5e7250f27540313b7471f97e23
|
4
|
+
data.tar.gz: 0b441cb30a153958c2ea283300a1a05c26218e2a97cb807fce36f8ad9d0240da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12779eac5c325752cfa1be34da94ef5f332490cda4ff0aef29529a00557008cecf39592396a3f3525a2a12cb67a46744800781e0da69d4bf02511f5a2284e5e7
|
7
|
+
data.tar.gz: a2e4c84fb338d62296aefb8ae8c206c262d756475ec11bd9cead17bfe6015ea069ff36a891df3381385fa650cb4b5ea1584855fccc5294f5910ab34563ad973a
|
@@ -0,0 +1,36 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
build:
|
4
|
+
environment:
|
5
|
+
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
|
6
|
+
CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:2.6.2
|
9
|
+
steps:
|
10
|
+
- checkout
|
11
|
+
- run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
|
12
|
+
- restore_cache:
|
13
|
+
keys:
|
14
|
+
# This branch if available
|
15
|
+
- v1-dep-{{ .Branch }}-
|
16
|
+
# Default branch if not
|
17
|
+
- v1-dep-master-
|
18
|
+
# Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
|
19
|
+
- v1-dep-
|
20
|
+
- run: gem install bundler --no-document
|
21
|
+
- run: 'bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3'
|
22
|
+
# Save dependency cache
|
23
|
+
- save_cache:
|
24
|
+
key: v1-dep-{{ .Branch }}-{{ epoch }}
|
25
|
+
paths:
|
26
|
+
- vendor/bundle
|
27
|
+
- ~/.bundle
|
28
|
+
- run: mkdir -p $CIRCLE_TEST_REPORTS/rspec
|
29
|
+
- run:
|
30
|
+
command: bundle exec rspec --color --require spec_helper --format progress
|
31
|
+
- store_test_results:
|
32
|
+
path: /tmp/circleci-test-results
|
33
|
+
- store_artifacts:
|
34
|
+
path: /tmp/circleci-artifacts
|
35
|
+
- store_artifacts:
|
36
|
+
path: /tmp/circleci-test-results
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v1
|
12
|
+
- name: Set up Ruby 2.6
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.6.x
|
16
|
+
- name: Build and test with RSpec
|
17
|
+
run: |
|
18
|
+
gem install bundler
|
19
|
+
bundle install --jobs 4 --retry 3
|
20
|
+
bundle exec rspec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# avro_turf
|
2
2
|
|
3
|
+
## Unreleased
|
4
|
+
|
5
|
+
## v0.11.0
|
6
|
+
|
7
|
+
- Add proxy support (#107)
|
8
|
+
- Adding support for client certs (#109)
|
9
|
+
|
10
|
+
## v0.10.0
|
11
|
+
|
12
|
+
- Add more disk caching (#103)
|
13
|
+
- Include schema information when decoding (#100, #101, #104)
|
14
|
+
|
15
|
+
## v0.9.0
|
16
|
+
|
17
|
+
- Compatibility with Avro v1.9.0 (#94)
|
18
|
+
- Disable the auto registeration of schema (#95)
|
19
|
+
- abstracted caching from CachedConfluentSchemaRegistry (#74)
|
20
|
+
- Load avro-patches if installed to silence deprecation errors (#85)
|
21
|
+
- Make schema store to be thread safe (#92)
|
22
|
+
|
3
23
|
## v0.8.1
|
4
24
|
|
5
25
|
- Allow accessing schema store from outside AvroTurf (#68).
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -124,9 +124,29 @@ avro = AvroTurf::Messaging.new(registry_url: "http://my-registry:8081/")
|
|
124
124
|
# time a schema is used.
|
125
125
|
data = avro.encode({ "title" => "hello, world" }, schema_name: "greeting")
|
126
126
|
|
127
|
+
# If you don't want to automatically register new schemas, you can pass explicitly
|
128
|
+
# subject and version to specify which schema should be used for encoding.
|
129
|
+
# It will fetch that schema from the registry and cache it. Subsequent instances
|
130
|
+
# of the same schema version will be served by the cache.
|
131
|
+
data = avro.encode({ "title" => "hello, world" }, subject: 'greeting', version: 1)
|
132
|
+
|
133
|
+
# You can also pass explicitly schema_id to specify which schema
|
134
|
+
# should be used for encoding.
|
135
|
+
# It will fetch that schema from the registry and cache it. Subsequent instances
|
136
|
+
# of the same schema version will be served by the cache.
|
137
|
+
data = avro.encode({ "title" => "hello, world" }, schema_id: 2)
|
138
|
+
|
127
139
|
# When decoding, the schema will be fetched from the registry and cached. Subsequent
|
128
140
|
# instances of the same schema id will be served by the cache.
|
129
141
|
avro.decode(data) #=> { "title" => "hello, world" }
|
142
|
+
|
143
|
+
# If you want to get decoded message as well as the schema used to encode the message,
|
144
|
+
# you can use `#decode_message` method.
|
145
|
+
result = avro.decode_message(data)
|
146
|
+
result.message #=> { "title" => "hello, world" }
|
147
|
+
result.schema_id #=> 3
|
148
|
+
result.writer_schema #=> #<Avro::Schema: ...>
|
149
|
+
result.reader_schema #=> nil
|
130
150
|
```
|
131
151
|
|
132
152
|
### Confluent Schema Registry Client
|
data/avro_turf.gemspec
CHANGED
@@ -17,16 +17,17 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency "avro", ">= 1.7.7", "< 1.
|
20
|
+
spec.add_dependency "avro", ">= 1.7.7", "< 1.10"
|
21
21
|
spec.add_dependency "excon", "~> 0.45"
|
22
22
|
|
23
|
-
spec.add_development_dependency "bundler", "~>
|
24
|
-
spec.add_development_dependency "rake", "~>
|
25
|
-
spec.add_development_dependency "rspec", "~> 3.2
|
26
|
-
spec.add_development_dependency "fakefs", "~> 0.
|
23
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
24
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
26
|
+
spec.add_development_dependency "fakefs", "~> 0.20.0"
|
27
27
|
spec.add_development_dependency "webmock"
|
28
28
|
spec.add_development_dependency "sinatra"
|
29
29
|
spec.add_development_dependency "json_spec"
|
30
|
+
spec.add_development_dependency "rack-test"
|
30
31
|
|
31
32
|
spec.post_install_message = %{
|
32
33
|
avro_turf v0.8.0 deprecates the names AvroTurf::SchemaRegistry,
|
data/lib/avro_turf.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
+
begin
|
2
|
+
require 'avro-patches'
|
3
|
+
rescue LoadError
|
4
|
+
false
|
5
|
+
end
|
1
6
|
require 'avro_turf/version'
|
2
7
|
require 'avro'
|
3
8
|
require 'json'
|
4
9
|
require 'avro_turf/schema_store'
|
5
10
|
require 'avro_turf/core_ext'
|
6
|
-
|
11
|
+
|
12
|
+
# check for something that indicates Avro v1.9.0 or later
|
13
|
+
unless defined?(::Avro::LogicalTypes)
|
14
|
+
require 'avro_turf/schema_to_avro_patch'
|
15
|
+
end
|
7
16
|
|
8
17
|
class AvroTurf
|
9
18
|
class Error < StandardError; end
|
@@ -1,16 +1,23 @@
|
|
1
1
|
require 'avro_turf/confluent_schema_registry'
|
2
|
+
require 'avro_turf/in_memory_cache'
|
3
|
+
require 'avro_turf/disk_cache'
|
2
4
|
|
3
5
|
# Caches registrations and lookups to the schema registry in memory.
|
4
6
|
class AvroTurf::CachedConfluentSchemaRegistry
|
5
7
|
|
6
|
-
|
8
|
+
# Instantiate a new CachedConfluentSchemaRegistry instance with the given configuration.
|
9
|
+
# By default, uses a provided InMemoryCache to prevent repeated calls to the upstream registry.
|
10
|
+
#
|
11
|
+
# upstream - The upstream schema registry object that fully responds to all methods in the
|
12
|
+
# AvroTurf::ConfluentSchemaRegistry interface.
|
13
|
+
# cache - Optional user provided Cache object that responds to all methods in the AvroTurf::InMemoryCache interface.
|
14
|
+
def initialize(upstream, cache: nil)
|
7
15
|
@upstream = upstream
|
8
|
-
@
|
9
|
-
@ids_by_schema = {}
|
16
|
+
@cache = cache || AvroTurf::InMemoryCache.new()
|
10
17
|
end
|
11
18
|
|
12
19
|
# Delegate the following methods to the upstream
|
13
|
-
%i(subjects subject_versions
|
20
|
+
%i(subjects subject_versions check compatible?
|
14
21
|
global_config update_global_config subject_config update_subject_config).each do |name|
|
15
22
|
define_method(name) do |*args|
|
16
23
|
instance_variable_get(:@upstream).send(name, *args)
|
@@ -18,10 +25,15 @@ class AvroTurf::CachedConfluentSchemaRegistry
|
|
18
25
|
end
|
19
26
|
|
20
27
|
def fetch(id)
|
21
|
-
@
|
28
|
+
@cache.lookup_by_id(id) || @cache.store_by_id(id, @upstream.fetch(id))
|
22
29
|
end
|
23
30
|
|
24
31
|
def register(subject, schema)
|
25
|
-
@
|
32
|
+
@cache.lookup_by_schema(subject, schema) || @cache.store_by_schema(subject, schema, @upstream.register(subject, schema))
|
33
|
+
end
|
34
|
+
|
35
|
+
def subject_version(subject, version = 'latest')
|
36
|
+
@cache.lookup_by_version(subject, version) ||
|
37
|
+
@cache.store_by_version(subject, version, @upstream.subject_version(subject, version))
|
26
38
|
end
|
27
39
|
end
|
@@ -3,11 +3,30 @@ require 'excon'
|
|
3
3
|
class AvroTurf::ConfluentSchemaRegistry
|
4
4
|
CONTENT_TYPE = "application/vnd.schemaregistry.v1+json".freeze
|
5
5
|
|
6
|
-
def initialize(
|
6
|
+
def initialize(
|
7
|
+
url,
|
8
|
+
logger: Logger.new($stdout),
|
9
|
+
proxy: nil,
|
10
|
+
client_cert: nil,
|
11
|
+
client_key: nil,
|
12
|
+
client_key_pass: nil,
|
13
|
+
client_cert_data: nil,
|
14
|
+
client_key_data: nil
|
15
|
+
)
|
7
16
|
@logger = logger
|
8
|
-
|
9
|
-
"Content-Type" => CONTENT_TYPE
|
10
|
-
}
|
17
|
+
headers = {
|
18
|
+
"Content-Type" => CONTENT_TYPE
|
19
|
+
}
|
20
|
+
headers[:proxy] = proxy if proxy&.present?
|
21
|
+
@connection = Excon.new(
|
22
|
+
url,
|
23
|
+
headers: headers,
|
24
|
+
client_cert: client_cert,
|
25
|
+
client_key: client_key,
|
26
|
+
client_key_pass: client_key_pass,
|
27
|
+
client_cert_data: client_cert_data,
|
28
|
+
client_key_data: client_key_data
|
29
|
+
)
|
11
30
|
end
|
12
31
|
|
13
32
|
def fetch(id)
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# A cache for the CachedConfluentSchemaRegistry.
|
2
|
+
# Extends the InMemoryCache to provide a write-thru to disk for persistent cache.
|
3
|
+
class AvroTurf::DiskCache < AvroTurf::InMemoryCache
|
4
|
+
|
5
|
+
def initialize(disk_path)
|
6
|
+
super()
|
7
|
+
|
8
|
+
# load the write-thru cache on startup, if it exists
|
9
|
+
@schemas_by_id_path = File.join(disk_path, 'schemas_by_id.json')
|
10
|
+
@schemas_by_id = JSON.parse(File.read(@schemas_by_id_path)) if File.exist?(@schemas_by_id_path)
|
11
|
+
|
12
|
+
@ids_by_schema_path = File.join(disk_path, 'ids_by_schema.json')
|
13
|
+
@ids_by_schema = JSON.parse(File.read(@ids_by_schema_path)) if File.exist?(@ids_by_schema_path)
|
14
|
+
|
15
|
+
@schemas_by_subject_version_path = File.join(disk_path, 'schemas_by_subject_version.json')
|
16
|
+
@schemas_by_subject_version = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# override
|
20
|
+
# the write-thru cache (json) does not store keys in numeric format
|
21
|
+
# so, convert id to a string for caching purposes
|
22
|
+
def lookup_by_id(id)
|
23
|
+
super(id.to_s)
|
24
|
+
end
|
25
|
+
|
26
|
+
# override to include write-thru cache after storing result from upstream
|
27
|
+
def store_by_id(id, schema)
|
28
|
+
# must return the value from storing the result (i.e. do not return result from file write)
|
29
|
+
value = super(id.to_s, schema)
|
30
|
+
File.write(@schemas_by_id_path, JSON.pretty_generate(@schemas_by_id))
|
31
|
+
return value
|
32
|
+
end
|
33
|
+
|
34
|
+
# override to include write-thru cache after storing result from upstream
|
35
|
+
def store_by_schema(subject, schema, id)
|
36
|
+
# must return the value from storing the result (i.e. do not return result from file write)
|
37
|
+
value = super
|
38
|
+
File.write(@ids_by_schema_path, JSON.pretty_generate(@ids_by_schema))
|
39
|
+
return value
|
40
|
+
end
|
41
|
+
|
42
|
+
# checks instance var (in-memory cache) for schema
|
43
|
+
# checks disk cache if in-memory cache doesn't exists
|
44
|
+
# if file exists but no in-memory cache, read from file and sync in-memory cache
|
45
|
+
# finally, if file doesn't exist return nil
|
46
|
+
def lookup_by_version(subject, version)
|
47
|
+
key = "#{subject}#{version}"
|
48
|
+
schema = @schemas_by_subject_version[key]
|
49
|
+
|
50
|
+
return schema unless schema.nil?
|
51
|
+
|
52
|
+
hash = JSON.parse(File.read(@schemas_by_subject_version_path)) if File.exist?(@schemas_by_subject_version_path)
|
53
|
+
if hash
|
54
|
+
@schemas_by_subject_version = hash
|
55
|
+
@schemas_by_subject_version[key]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# check if file exists and parse json into a hash
|
60
|
+
# if file exists take json and overwite/insert schema at key
|
61
|
+
# if file doesn't exist create new hash
|
62
|
+
# write the new/updated hash to file
|
63
|
+
# update instance var (in memory-cache) to match
|
64
|
+
def store_by_version(subject, version, schema)
|
65
|
+
key = "#{subject}#{version}"
|
66
|
+
hash = JSON.parse(File.read(@schemas_by_subject_version_path)) if File.exist?(@schemas_by_subject_version_path)
|
67
|
+
hash = if hash
|
68
|
+
hash[key] = schema
|
69
|
+
hash
|
70
|
+
else
|
71
|
+
{key => schema}
|
72
|
+
end
|
73
|
+
|
74
|
+
write_to_disk_cache(@schemas_by_subject_version_path, hash)
|
75
|
+
|
76
|
+
@schemas_by_subject_version = hash
|
77
|
+
@schemas_by_subject_version[key]
|
78
|
+
end
|
79
|
+
|
80
|
+
private def write_to_disk_cache(path, hash)
|
81
|
+
File.write(path, JSON.pretty_generate(hash))
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# A cache for the CachedConfluentSchemaRegistry.
|
2
|
+
# Simply stores the schemas and ids in in-memory hashes.
|
3
|
+
class AvroTurf::InMemoryCache
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@schemas_by_id = {}
|
7
|
+
@ids_by_schema = {}
|
8
|
+
@schema_by_subject_version = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def lookup_by_id(id)
|
12
|
+
@schemas_by_id[id]
|
13
|
+
end
|
14
|
+
|
15
|
+
def store_by_id(id, schema)
|
16
|
+
@schemas_by_id[id] = schema
|
17
|
+
end
|
18
|
+
|
19
|
+
def lookup_by_schema(subject, schema)
|
20
|
+
key = subject + schema.to_s
|
21
|
+
@ids_by_schema[key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def store_by_schema(subject, schema, id)
|
25
|
+
key = subject + schema.to_s
|
26
|
+
@ids_by_schema[key] = id
|
27
|
+
end
|
28
|
+
|
29
|
+
def lookup_by_version(subject, version)
|
30
|
+
key = "#{subject}#{version}"
|
31
|
+
@schema_by_subject_version[key]
|
32
|
+
end
|
33
|
+
|
34
|
+
def store_by_version(subject, version, schema)
|
35
|
+
key = "#{subject}#{version}"
|
36
|
+
@schema_by_subject_version[key] = schema
|
37
|
+
end
|
38
|
+
end
|
data/lib/avro_turf/messaging.rb
CHANGED
@@ -21,21 +21,53 @@ class AvroTurf
|
|
21
21
|
# 1: https://github.com/confluentinc/schema-registry
|
22
22
|
class Messaging
|
23
23
|
MAGIC_BYTE = [0].pack("C").freeze
|
24
|
+
DecodedMessage = Struct.new(:schema_id, :writer_schema, :reader_schema, :message)
|
25
|
+
private_constant(:DecodedMessage)
|
24
26
|
|
25
27
|
# Instantiate a new Messaging instance with the given configuration.
|
26
28
|
#
|
27
|
-
# registry
|
28
|
-
#
|
29
|
-
# registry_url
|
30
|
-
# schema_store
|
31
|
-
# schemas_path
|
32
|
-
# namespace
|
33
|
-
# logger
|
34
|
-
|
29
|
+
# registry - A schema registry object that responds to all methods in the
|
30
|
+
# AvroTurf::ConfluentSchemaRegistry interface.
|
31
|
+
# registry_url - The String URL of the schema registry that should be used.
|
32
|
+
# schema_store - A schema store object that responds to #find(schema_name, namespace).
|
33
|
+
# schemas_path - The String file system path where local schemas are stored.
|
34
|
+
# namespace - The String default schema namespace.
|
35
|
+
# logger - The Logger that should be used to log information (optional).
|
36
|
+
# proxy - Forward the request via proxy (optional).
|
37
|
+
# client_cert - Name of file containing client certificate (optional).
|
38
|
+
# client_key - Name of file containing client private key to go with client_cert (optional).
|
39
|
+
# client_key_pass - Password to go with client_key (optional).
|
40
|
+
# client_cert_data - In-memory client certificate (optional).
|
41
|
+
# client_key_data - In-memory client private key to go with client_cert_data (optional).
|
42
|
+
def initialize(
|
43
|
+
registry: nil,
|
44
|
+
registry_url: nil,
|
45
|
+
schema_store: nil,
|
46
|
+
schemas_path: nil,
|
47
|
+
namespace: nil,
|
48
|
+
logger: nil,
|
49
|
+
proxy: nil,
|
50
|
+
client_cert: nil,
|
51
|
+
client_key: nil,
|
52
|
+
client_key_pass: nil,
|
53
|
+
client_cert_data: nil,
|
54
|
+
client_key_data: nil
|
55
|
+
)
|
35
56
|
@logger = logger || Logger.new($stderr)
|
36
57
|
@namespace = namespace
|
37
58
|
@schema_store = schema_store || SchemaStore.new(path: schemas_path || DEFAULT_SCHEMAS_PATH)
|
38
|
-
@registry = registry || CachedConfluentSchemaRegistry.new(
|
59
|
+
@registry = registry || CachedConfluentSchemaRegistry.new(
|
60
|
+
ConfluentSchemaRegistry.new(
|
61
|
+
registry_url,
|
62
|
+
logger: @logger,
|
63
|
+
proxy: proxy,
|
64
|
+
client_cert: client_cert,
|
65
|
+
client_key: client_key,
|
66
|
+
client_key_pass: client_key_pass,
|
67
|
+
client_cert_data: client_cert_data,
|
68
|
+
client_key_data: client_key_data
|
69
|
+
)
|
70
|
+
)
|
39
71
|
@schemas_by_id = {}
|
40
72
|
end
|
41
73
|
|
@@ -46,14 +78,24 @@ class AvroTurf
|
|
46
78
|
# schema_name - The String name of the schema that should be used to encode
|
47
79
|
# the data.
|
48
80
|
# namespace - The namespace of the schema (optional).
|
81
|
+
# subject - The subject name the schema should be registered under in
|
82
|
+
# the schema registry (optional).
|
83
|
+
# version - The integer version of the schema that should be used to decode
|
84
|
+
# the data. Must match the schema used when encoding (optional).
|
85
|
+
# schema_id - The integer id of the schema that should be used to encode
|
86
|
+
# the data.
|
49
87
|
#
|
50
88
|
# Returns the encoded data as a String.
|
51
|
-
def encode(message, schema_name: nil, namespace: @namespace, subject: nil)
|
52
|
-
schema =
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
89
|
+
def encode(message, schema_name: nil, namespace: @namespace, subject: nil, version: nil, schema_id: nil)
|
90
|
+
schema_id, schema = if schema_id
|
91
|
+
fetch_schema_by_id(schema_id)
|
92
|
+
elsif subject && version
|
93
|
+
fetch_schema(subject, version)
|
94
|
+
elsif schema_name
|
95
|
+
register_schema(subject, schema_name, namespace)
|
96
|
+
else
|
97
|
+
raise ArgumentError.new('Neither schema_name nor schema_id nor subject + version provided to determine the schema.')
|
98
|
+
end
|
57
99
|
|
58
100
|
stream = StringIO.new
|
59
101
|
writer = Avro::IO::DatumWriter.new(schema)
|
@@ -69,6 +111,12 @@ class AvroTurf
|
|
69
111
|
writer.write(message, encoder)
|
70
112
|
|
71
113
|
stream.string
|
114
|
+
rescue Excon::Error::NotFound
|
115
|
+
if schema_id
|
116
|
+
raise SchemaNotFoundError.new("Schema with id: #{schema_id} is not found on registry")
|
117
|
+
else
|
118
|
+
raise SchemaNotFoundError.new("Schema with subject: `#{subject}` version: `#{version}` is not found on registry")
|
119
|
+
end
|
72
120
|
end
|
73
121
|
|
74
122
|
# Decodes data into the original message.
|
@@ -80,6 +128,20 @@ class AvroTurf
|
|
80
128
|
#
|
81
129
|
# Returns the decoded message.
|
82
130
|
def decode(data, schema_name: nil, namespace: @namespace)
|
131
|
+
decode_message(data, schema_name: schema_name, namespace: namespace).message
|
132
|
+
end
|
133
|
+
|
134
|
+
# Decodes data into the original message.
|
135
|
+
#
|
136
|
+
# data - A String containing encoded data.
|
137
|
+
# schema_name - The String name of the schema that should be used to decode
|
138
|
+
# the data. Must match the schema used when encoding (optional).
|
139
|
+
# namespace - The namespace of the schema (optional).
|
140
|
+
#
|
141
|
+
# Returns Struct with the next attributes:
|
142
|
+
# schema_id - The integer id of schema used to encode the message
|
143
|
+
# message - The decoded message
|
144
|
+
def decode_message(data, schema_name: nil, namespace: @namespace)
|
83
145
|
readers_schema = schema_name && @schema_store.find(schema_name, namespace)
|
84
146
|
stream = StringIO.new(data)
|
85
147
|
decoder = Avro::IO::BinaryDecoder.new(stream)
|
@@ -100,7 +162,38 @@ class AvroTurf
|
|
100
162
|
end
|
101
163
|
|
102
164
|
reader = Avro::IO::DatumReader.new(writers_schema, readers_schema)
|
103
|
-
reader.read(decoder)
|
165
|
+
message = reader.read(decoder)
|
166
|
+
|
167
|
+
DecodedMessage.new(schema_id, writers_schema, readers_schema, message)
|
168
|
+
rescue Excon::Error::NotFound
|
169
|
+
raise SchemaNotFoundError.new("Schema with id: #{schema_id} is not found on registry")
|
170
|
+
end
|
171
|
+
|
172
|
+
private
|
173
|
+
|
174
|
+
# Providing subject and version to determine the schema,
|
175
|
+
# which skips the auto registeration of schema on the schema registry.
|
176
|
+
# Fetch the schema from registry with the provided subject name and version.
|
177
|
+
def fetch_schema(subject, version)
|
178
|
+
schema_data = @registry.subject_version(subject, version)
|
179
|
+
schema_id = schema_data.fetch('id')
|
180
|
+
schema = Avro::Schema.parse(schema_data.fetch('schema'))
|
181
|
+
[schema_id, schema]
|
182
|
+
end
|
183
|
+
|
184
|
+
# Fetch the schema from registry with the provided schema_id.
|
185
|
+
def fetch_schema_by_id(schema_id)
|
186
|
+
schema_json = @registry.fetch(schema_id)
|
187
|
+
schema = Avro::Schema.parse(schema_json)
|
188
|
+
[schema_id, schema]
|
189
|
+
end
|
190
|
+
|
191
|
+
# Schemas are registered under the full name of the top level Avro record
|
192
|
+
# type, or `subject` if it's provided.
|
193
|
+
def register_schema(subject, schema_name, namespace)
|
194
|
+
schema = @schema_store.find(schema_name, namespace)
|
195
|
+
schema_id = @registry.register(subject || schema.fullname, schema)
|
196
|
+
[schema_id, schema]
|
104
197
|
end
|
105
198
|
end
|
106
199
|
end
|