avro_turf 0.1.1 → 0.2.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: bc097e71b4fea08649c5e36f5506e2cf46d3b224
4
- data.tar.gz: 0da17e13eee520f66bbcf9b4fbee3731a6719787
3
+ metadata.gz: 64620bcf8c0a04337ecf571abe2d0e51098fce4a
4
+ data.tar.gz: cc45ee47d74ea7ee364463b105f8cfc92345058b
5
5
  SHA512:
6
- metadata.gz: de50ca27fa010d2f0f0401398dd2d3eb97c449817cec3ebb0a84d1026a845f082ce2d2e69c7a0241faea1c84c04c1b4d43c1d04bf105ca6e00a8e8aaab0ee779
7
- data.tar.gz: 7d10349cd766901ad1b74a74e89931907bb1c6ea17f882ac7d775f0635154fc2427460baed98f4f213e763eeb40d91352da0a247cce67e1646fc147607fd63e6
6
+ metadata.gz: 090cf294b97ea232c5ec8372eced0244d87f87a6fd3d9cdffdfb2ad73e467e52be0ae1ab3108e53a3e5aabfdd7a3bd2a35aae70c89ca1b448a74305d61d48964
7
+ data.tar.gz: 657f8b32c84d5b6e01a614bab06a4d17987e3c1832641e62bac8548c139810e60febfc72684983b1342e4839c889b667d037fcbb09932c940366636e32198c5f
@@ -1,3 +1,3 @@
1
1
  class AvroTurf
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/avro_turf.rb CHANGED
@@ -6,8 +6,10 @@ class AvroTurf
6
6
  class Error < StandardError; end
7
7
  class SchemaError < Error; end
8
8
 
9
- def initialize(schemas_path:)
9
+ def initialize(schemas_path:, namespace: nil)
10
10
  @schemas_path = schemas_path or raise "Please specify a schema path"
11
+ @schemas = Hash.new
12
+ @namespace = namespace
11
13
  end
12
14
 
13
15
  # Encodes data to Avro using the specified schema.
@@ -16,8 +18,8 @@ class AvroTurf
16
18
  # schema_name - The name of a schema in the `schemas_path`.
17
19
  #
18
20
  # Returns a String containing the encoded data.
19
- def encode(data, schema_name:)
20
- schema = resolve_schema(schema_name)
21
+ def encode(data, schema_name:, namespace: @namespace)
22
+ schema = resolve_schema(schema_name, namespace)
21
23
  writer = Avro::IO::DatumWriter.new(schema)
22
24
 
23
25
  io = StringIO.new
@@ -35,9 +37,9 @@ class AvroTurf
35
37
  # the data. If nil, the writer schema will be used.
36
38
  #
37
39
  # Returns whatever is encoded in the data.
38
- def decode(encoded_data, schema_name: nil)
40
+ def decode(encoded_data, schema_name: nil, namespace: @namespace)
39
41
  io = StringIO.new(encoded_data)
40
- schema = schema_name && resolve_schema(schema_name)
42
+ schema = schema_name && resolve_schema(schema_name, namespace)
41
43
  reader = Avro::IO::DatumReader.new(nil, schema)
42
44
  dr = Avro::DataFile::Reader.new(io, reader)
43
45
  dr.first
@@ -48,15 +50,17 @@ class AvroTurf
48
50
  # Resolves and returns a schema.
49
51
  #
50
52
  # schema_name - The String name of the schema to resolve.
51
- # names - A Hash mapping schema names to Avro::Schema instances. Used
52
- # when referencing custom types.
53
53
  #
54
54
  # Returns an Avro::Schema.
55
- def resolve_schema(fullname, names = {})
55
+ def resolve_schema(name, namespace = nil)
56
+ fullname = Avro::Name.make_fullname(name, namespace)
57
+
58
+ return @schemas[fullname] if @schemas.key?(fullname)
59
+
56
60
  *namespace, schema_name = fullname.split(".")
57
61
  schema_path = File.join(@schemas_path, *namespace, schema_name + ".avsc")
58
62
  schema_json = JSON.parse(File.read(schema_path))
59
- schema = Avro::Schema.real_parse(schema_json, names)
63
+ schema = Avro::Schema.real_parse(schema_json, @schemas)
60
64
 
61
65
  if schema.respond_to?(:fullname) && schema.fullname != fullname
62
66
  raise SchemaError, "expected schema `#{schema_path}' to define type `#{fullname}'"
@@ -67,11 +71,11 @@ class AvroTurf
67
71
  # This is a hack in order to figure out exactly which type was missing. The
68
72
  # Avro gem ought to provide this data directly.
69
73
  if e.to_s =~ /"([\w\.]+)" is not a schema we know about/
70
- resolve_schema($1, names)
74
+ resolve_schema($1)
71
75
 
72
76
  # Re-resolve the original schema now that the dependency has been resolved.
73
- names.delete(fullname)
74
- resolve_schema(fullname, names)
77
+ @schemas.delete(fullname)
78
+ resolve_schema(fullname)
75
79
  else
76
80
  raise
77
81
  end
data/spec/avro_spec.rb CHANGED
@@ -161,9 +161,9 @@ describe AvroTurf do
161
161
  }
162
162
  }
163
163
 
164
- encoded_data = avro.encode(data, schema_name: "test.people.person")
164
+ encoded_data = avro.encode(data, schema_name: "person", namespace: "test.people")
165
165
 
166
- expect(avro.decode(encoded_data, schema_name: "test.people.person")).to eq(data)
166
+ expect(avro.decode(encoded_data, schema_name: "person", namespace: "test.people")).to eq(data)
167
167
  end
168
168
 
169
169
  it "raises AvroTurf::SchemaError if the schema's namespace doesn't match the file location" do
@@ -190,6 +190,35 @@ describe AvroTurf do
190
190
  }.to raise_error(AvroTurf::SchemaError, "expected schema `spec/schemas/test/people/person.avsc' to define type `test.people.person'")
191
191
  end
192
192
 
193
+ it "caches schemas in memory" do
194
+ define_schema "person.avsc", <<-AVSC
195
+ {
196
+ "name": "person",
197
+ "type": "record",
198
+ "fields": [
199
+ {
200
+ "type": "string",
201
+ "name": "full_name"
202
+ }
203
+ ]
204
+ }
205
+ AVSC
206
+
207
+ data = {
208
+ "full_name" => "John Doe"
209
+ }
210
+
211
+ # Warm the schema cache.
212
+ avro.encode(data, schema_name: "person")
213
+
214
+ # Force a failure if the schema file is read again.
215
+ FileUtils.rm("spec/schemas/person.avsc")
216
+
217
+ encoded_data = avro.encode(data, schema_name: "person")
218
+
219
+ expect(avro.decode(encoded_data, schema_name: "person")).to eq(data)
220
+ end
221
+
193
222
  def define_schema(path, content)
194
223
  File.open(File.join("spec/schemas", path), "w") do |f|
195
224
  f.write(content)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avro_turf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-06 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro