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 +4 -4
- data/circle.yml +3 -0
- data/lib/avro_turf.rb +17 -6
- data/lib/avro_turf/version.rb +1 -1
- data/spec/avro_spec.rb +121 -51
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 221a8cec2332df0667003b732bbec51a9e181596
|
4
|
+
data.tar.gz: 6be83bca9496d4e901984c156ec62b2f2110d0ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 398d8a49c7997faac0fd77a1c47875346c3a5e4b2227280020888abaf5aafdcbf8621560f62b1b7af15638a529517b79845180a50244a498aee2e53bec30ce6d
|
7
|
+
data.tar.gz: 51eceeb5904ffc58ea3770b1743507682c476e76db3aba158348bb134680b5f1df4ca3e3018581946a2964eb74c1acc1d269e17245ee132c9c166ad9c4945bb6
|
data/circle.yml
ADDED
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(
|
52
|
-
|
53
|
-
|
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(
|
62
|
-
resolve_schema(
|
72
|
+
names.delete(fullname)
|
73
|
+
resolve_schema(fullname, names)
|
63
74
|
else
|
64
75
|
raise
|
65
76
|
end
|
data/lib/avro_turf/version.rb
CHANGED
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
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
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
|
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-
|
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
|