avro_turf 0.0.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9673b4898c8bc5f75fbbdd90b2f4ac3f235c8a07
4
- data.tar.gz: 21bb1f430e089ba31e488cd813e5343c961a2325
3
+ metadata.gz: 221a8cec2332df0667003b732bbec51a9e181596
4
+ data.tar.gz: 6be83bca9496d4e901984c156ec62b2f2110d0ce
5
5
  SHA512:
6
- metadata.gz: 377dcdbd55e9789edc4a38f8324fe0ac2a52f34f06cfdc291a499e67bc4ab6fa9069a5f7fa3a2ff4e057625a5236694832f4c55fd8491bea8558b476c641170e
7
- data.tar.gz: 6644b4bcb1cb6b9bf4d3e1030f814897fff44a7b134ad038987b3c52e9f469686055d3416b1b4c144d69010ef3a2576d73625d45e6f366dc3030591c531accc7
6
+ metadata.gz: 398d8a49c7997faac0fd77a1c47875346c3a5e4b2227280020888abaf5aafdcbf8621560f62b1b7af15638a529517b79845180a50244a498aee2e53bec30ce6d
7
+ data.tar.gz: 51eceeb5904ffc58ea3770b1743507682c476e76db3aba158348bb134680b5f1df4ca3e3018581946a2964eb74c1acc1d269e17245ee132c9c166ad9c4945bb6
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.0
data/lib/avro_turf.rb CHANGED
@@ -2,6 +2,9 @@ require 'avro_turf/version'
2
2
  require 'avro'
3
3
 
4
4
  class AvroTurf
5
+ class Error < StandardError; end
6
+ class SchemaError < Error; end
7
+
5
8
  def initialize(schemas_path:)
6
9
  @schemas_path = schemas_path
7
10
  end
@@ -48,18 +51,26 @@ class AvroTurf
48
51
  # when referencing custom types.
49
52
  #
50
53
  # Returns an Avro::Schema.
51
- def resolve_schema(schema_name, names = {})
52
- schema_path = File.join(@schemas_path, schema_name + ".avsc")
53
- Avro::Schema.real_parse(JSON.parse(File.read(schema_path)), names)
54
+ def resolve_schema(fullname, names = {})
55
+ *namespace, schema_name = fullname.split(".")
56
+ schema_path = File.join(@schemas_path, *namespace, schema_name + ".avsc")
57
+ schema_json = JSON.parse(File.read(schema_path))
58
+ schema = Avro::Schema.real_parse(schema_json, names)
59
+
60
+ if schema.respond_to?(:fullname) && schema.fullname != fullname
61
+ raise SchemaError, "expected schema `#{schema_path}' to define type `#{fullname}'"
62
+ end
63
+
64
+ schema
54
65
  rescue ::Avro::SchemaParseError => e
55
66
  # This is a hack in order to figure out exactly which type was missing. The
56
67
  # Avro gem ought to provide this data directly.
57
- if e.to_s =~ /"(\w+)" is not a schema we know about/
68
+ if e.to_s =~ /"([\w\.]+)" is not a schema we know about/
58
69
  resolve_schema($1, names)
59
70
 
60
71
  # Re-resolve the original schema now that the dependency has been resolved.
61
- names.delete(schema_name)
62
- resolve_schema(schema_name, names)
72
+ names.delete(fullname)
73
+ resolve_schema(fullname, names)
63
74
  else
64
75
  raise
65
76
  end
@@ -1,3 +1,3 @@
1
1
  class AvroTurf
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/spec/avro_spec.rb CHANGED
@@ -10,7 +10,7 @@ describe AvroTurf do
10
10
  end
11
11
 
12
12
  it "encodes and decodes data using a named schema" do
13
- schema = <<-AVSC
13
+ define_schema "person.avsc", <<-AVSC
14
14
  {
15
15
  "name": "person",
16
16
  "type": "record",
@@ -40,12 +40,6 @@ describe AvroTurf do
40
40
  }
41
41
  AVSC
42
42
 
43
- FileUtils.mkdir_p("spec/schemas")
44
-
45
- File.open("spec/schemas/person.avsc", "w") do |f|
46
- f.write(schema)
47
- end
48
-
49
43
  data = {
50
44
  "full_name" => "John Doe",
51
45
  "address" => {
@@ -60,43 +54,39 @@ describe AvroTurf do
60
54
  end
61
55
 
62
56
  it "resolves named types" do
63
- File.open("spec/schemas/person.avsc", "w") do |f|
64
- f.write <<-AVSC
65
- {
66
- "name": "person",
67
- "type": "record",
68
- "fields": [
69
- {
70
- "type": "string",
71
- "name": "full_name"
72
- },
73
- {
74
- "name": "address",
75
- "type": "address"
76
- }
77
- ]
78
- }
79
- AVSC
80
- end
57
+ define_schema "person.avsc", <<-AVSC
58
+ {
59
+ "name": "person",
60
+ "type": "record",
61
+ "fields": [
62
+ {
63
+ "type": "string",
64
+ "name": "full_name"
65
+ },
66
+ {
67
+ "name": "address",
68
+ "type": "address"
69
+ }
70
+ ]
71
+ }
72
+ AVSC
81
73
 
82
- File.open("spec/schemas/address.avsc", "w") do |f|
83
- f.write <<-AVSC
84
- {
85
- "type": "record",
86
- "name": "address",
87
- "fields": [
88
- {
89
- "type": "string",
90
- "name": "street"
91
- },
92
- {
93
- "type": "string",
94
- "name": "city"
95
- }
96
- ]
97
- }
98
- AVSC
99
- end
74
+ define_schema "address.avsc", <<-AVSC
75
+ {
76
+ "type": "record",
77
+ "name": "address",
78
+ "fields": [
79
+ {
80
+ "type": "string",
81
+ "name": "street"
82
+ },
83
+ {
84
+ "type": "string",
85
+ "name": "city"
86
+ }
87
+ ]
88
+ }
89
+ AVSC
100
90
 
101
91
  data = {
102
92
  "full_name" => "John Doe",
@@ -112,17 +102,97 @@ describe AvroTurf do
112
102
  end
113
103
 
114
104
  it "allows decoding without specifying a reader schema" do
115
- File.open("spec/schemas/message.avsc", "w") do |f|
116
- f.write <<-AVSC
117
- {
118
- "name": "message",
119
- "type": "string"
120
- }
121
- AVSC
122
- end
105
+ define_schema "message.avsc", <<-AVSC
106
+ {
107
+ "name": "message",
108
+ "type": "string"
109
+ }
110
+ AVSC
123
111
 
124
112
  encoded_data = avro.encode("hello, world", schema_name: "message")
125
113
 
126
114
  expect(avro.decode(encoded_data)).to eq "hello, world"
127
115
  end
116
+
117
+ it "allows using namespaces in schemas" do
118
+ FileUtils.mkdir_p("spec/schemas/test/people")
119
+
120
+ define_schema "test/people/person.avsc", <<-AVSC
121
+ {
122
+ "name": "person",
123
+ "namespace": "test.people",
124
+ "type": "record",
125
+ "fields": [
126
+ {
127
+ "type": "string",
128
+ "name": "full_name"
129
+ },
130
+ {
131
+ "name": "address",
132
+ "type": "test.people.address"
133
+ }
134
+ ]
135
+ }
136
+ AVSC
137
+
138
+ define_schema "test/people/address.avsc", <<-AVSC
139
+ {
140
+ "name": "address",
141
+ "namespace": "test.people",
142
+ "type": "record",
143
+ "fields": [
144
+ {
145
+ "type": "string",
146
+ "name": "street"
147
+ },
148
+ {
149
+ "type": "string",
150
+ "name": "city"
151
+ }
152
+ ]
153
+ }
154
+ AVSC
155
+
156
+ data = {
157
+ "full_name" => "John Doe",
158
+ "address" => {
159
+ "street" => "Market st. 989",
160
+ "city" => "San Francisco"
161
+ }
162
+ }
163
+
164
+ encoded_data = avro.encode(data, schema_name: "test.people.person")
165
+
166
+ expect(avro.decode(encoded_data, schema_name: "test.people.person")).to eq(data)
167
+ end
168
+
169
+ it "raises AvroTurf::SchemaError if the schema's namespace doesn't match the file location" do
170
+ FileUtils.mkdir_p("spec/schemas/test/people")
171
+
172
+ define_schema "test/people/person.avsc", <<-AVSC
173
+ {
174
+ "name": "person",
175
+ "namespace": "yoyoyo.nanana",
176
+ "type": "record",
177
+ "fields": [
178
+ {
179
+ "type": "string",
180
+ "name": "full_name"
181
+ }
182
+ ]
183
+ }
184
+ AVSC
185
+
186
+ data = { "full_name" => "John Doe" }
187
+
188
+ expect {
189
+ avro.encode(data, schema_name: "test.people.person")
190
+ }.to raise_error(AvroTurf::SchemaError, "expected schema `spec/schemas/test/people/person.avsc' to define type `test.people.person'")
191
+ end
192
+
193
+ def define_schema(path, content)
194
+ File.open(File.join("spec/schemas", path), "w") do |f|
195
+ f.write(content)
196
+ end
197
+ end
128
198
  end
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.0.1
4
+ version: 0.1.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-04 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -94,6 +94,7 @@ files:
94
94
  - README.md
95
95
  - Rakefile
96
96
  - avro_turf.gemspec
97
+ - circle.yml
97
98
  - lib/avro_turf.rb
98
99
  - lib/avro_turf/version.rb
99
100
  - spec/avro_spec.rb