sparsam 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sparsam.rb +1 -0
- data/lib/sparsam/application_exception.rb +10 -0
- data/lib/sparsam/base_struct.rb +35 -0
- data/lib/sparsam/base_type.rb +89 -0
- data/lib/sparsam/struct.rb +5 -28
- data/lib/sparsam/union.rb +4 -2
- data/spec/gen-ruby/user_constants.rb +1 -1
- data/spec/gen-ruby/user_types.rb +18 -1
- data/spec/sparsam_spec.rb +22 -0
- data/spec/user.thrift +9 -0
- metadata +19 -14
- data/lib/sparsam/base_class.rb +0 -83
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b9246d1a569e7fa72c9f97015e0f9031b31249a
|
4
|
+
data.tar.gz: ba92cae518e5606f8c7273fb32519f307163b72c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 268c577839d6ba4ff96b19dfdf9d09e6b903a72ef298a4cba763ed13dd4723181b85bf213e0a974ffa0abdf0a5ac131b6ef51f3fe8b42ae753ac4f3d48378afe
|
7
|
+
data.tar.gz: 83b7f8c97f0feea55491958a67ae0db8805a080a0e874c27e7046211f49f74cd6b39e0f67ac1014b27a70734247ca38173dd1ece94b6dd11bf41ca0b435c9646
|
data/lib/sparsam.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Sparsam
|
2
|
+
module BaseStruct
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def field_accessor(klass, field_key, field_info)
|
9
|
+
field_name = field_info[:name]
|
10
|
+
klass.class_eval(<<-EOF, __FILE__, __LINE__)
|
11
|
+
attr_accessor :'#{field_name}'
|
12
|
+
EOF
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def assign_defaults(defaults)
|
19
|
+
defaults.each do |name, default_value|
|
20
|
+
accessors = name_to_accessors(name)
|
21
|
+
send(accessors.writer, default_value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def assign_from_arg(d)
|
26
|
+
d.each do |name, value|
|
27
|
+
accessors = name_to_accessors(name)
|
28
|
+
unless accessors
|
29
|
+
raise Sparsam::Exception, "Unknown key given to #{self.class}.new: #{name}"
|
30
|
+
end
|
31
|
+
send(accessors.writer, value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'sparsam/types'
|
3
|
+
|
4
|
+
module Sparsam
|
5
|
+
module BaseType
|
6
|
+
class AccessorNames < Struct.new(:reader, :writer); end
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def generate_field_syms(klass)
|
14
|
+
field_syms = {}
|
15
|
+
klass::FIELDS.each do |_, field_info|
|
16
|
+
name = field_info[:name].to_sym
|
17
|
+
accessors = AccessorNames.new(name, :"#{name}=")
|
18
|
+
field_syms[name] = accessors
|
19
|
+
field_syms[name.to_s] = accessors
|
20
|
+
end
|
21
|
+
|
22
|
+
klass.const_set(
|
23
|
+
:FIELD_SYMS,
|
24
|
+
field_syms
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_accessors(klass)
|
29
|
+
klass::FIELDS.each do |field_key, field_info|
|
30
|
+
field_accessor(klass, field_key, field_info)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def generate_default_values(klass)
|
35
|
+
fields_with_default_values = {}
|
36
|
+
klass::FIELDS.each do |fid, field_def|
|
37
|
+
unless field_def[:default].nil?
|
38
|
+
fields_with_default_values[field_def[:name]] = field_def[:default].freeze
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
if fields_with_default_values.empty?
|
43
|
+
klass.const_set(
|
44
|
+
:DEFAULT_VALUES,
|
45
|
+
nil
|
46
|
+
)
|
47
|
+
else
|
48
|
+
klass.const_set(
|
49
|
+
:DEFAULT_VALUES,
|
50
|
+
fields_with_default_values
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def init_thrift_struct(klass)
|
56
|
+
generate_accessors(klass)
|
57
|
+
generate_field_syms(klass)
|
58
|
+
Sparsam.cache_fields(klass)
|
59
|
+
generate_default_values(klass)
|
60
|
+
klass.class_eval(<<-EOF, __FILE__, __LINE__)
|
61
|
+
def struct_fields
|
62
|
+
FIELDS
|
63
|
+
end
|
64
|
+
EOF
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def serialize(prot = Sparsam::CompactProtocol)
|
69
|
+
s = Sparsam::Serializer.new(prot, "")
|
70
|
+
s.serialize(self.class, self)
|
71
|
+
end
|
72
|
+
|
73
|
+
def validate(mode = Sparsam::NORMAL)
|
74
|
+
Sparsam.validate(self.class, self, mode)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def name_to_accessors(name)
|
80
|
+
self.class::FIELD_SYMS[name]
|
81
|
+
end
|
82
|
+
|
83
|
+
def each_field
|
84
|
+
struct_fields.each do |fid, data|
|
85
|
+
yield fid, data
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/sparsam/struct.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
# -*- coding: UTF-8 -*-
|
2
|
-
require 'sparsam/
|
2
|
+
require 'sparsam/base_type'
|
3
|
+
require 'sparsam/base_struct'
|
3
4
|
|
4
5
|
module Sparsam
|
5
|
-
class Struct
|
6
|
+
class Struct
|
7
|
+
include ::Sparsam::BaseType
|
6
8
|
include ::Sparsam::StructInitialization
|
9
|
+
include ::Sparsam::BaseStruct
|
7
10
|
|
8
11
|
def ==(other)
|
9
12
|
return true if other.equal?(self)
|
@@ -15,31 +18,5 @@ module Sparsam
|
|
15
18
|
end
|
16
19
|
end
|
17
20
|
alias_method :eql?, :==
|
18
|
-
|
19
|
-
def self.field_accessor(klass, field_key, field_info)
|
20
|
-
field_name = field_info[:name]
|
21
|
-
klass.class_eval(<<-EOF, __FILE__, __LINE__)
|
22
|
-
attr_accessor :'#{field_name}'
|
23
|
-
EOF
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def assign_defaults(defaults)
|
29
|
-
defaults.each do |name, default_value|
|
30
|
-
accessors = name_to_accessors(name)
|
31
|
-
send(accessors.writer, default_value)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def assign_from_arg(d)
|
36
|
-
d.each do |name, value|
|
37
|
-
accessors = name_to_accessors(name)
|
38
|
-
unless accessors
|
39
|
-
raise Sparsam::Exception, "Unknown key given to #{self.class}.new: #{name}"
|
40
|
-
end
|
41
|
-
send(accessors.writer, value)
|
42
|
-
end
|
43
|
-
end
|
44
21
|
end
|
45
22
|
end
|
data/lib/sparsam/union.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# -*- coding: UTF-8 -*-
|
2
|
-
require 'sparsam/
|
2
|
+
require 'sparsam/base_type'
|
3
3
|
|
4
4
|
module Sparsam
|
5
|
-
class Union
|
5
|
+
class Union
|
6
|
+
include ::Sparsam::BaseType
|
7
|
+
|
6
8
|
def initialize(name = nil, value = nil)
|
7
9
|
if name
|
8
10
|
if name.is_a? Hash
|
data/spec/gen-ruby/user_types.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# Autogenerated by Thrift Compiler (0.1.
|
2
|
+
# Autogenerated by Thrift Compiler (0.1.1)
|
3
3
|
#
|
4
4
|
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
|
5
5
|
#
|
@@ -126,3 +126,20 @@ class EveryType < ::Sparsam::Struct
|
|
126
126
|
init_thrift_struct(self)
|
127
127
|
end
|
128
128
|
|
129
|
+
class SimpleException < ::Sparsam::ApplicationException
|
130
|
+
FIELDS = {
|
131
|
+
1 => {:type => ::Sparsam::Types::STRING, :name => 'message', :optional => true}
|
132
|
+
}
|
133
|
+
|
134
|
+
init_thrift_struct(self)
|
135
|
+
end
|
136
|
+
|
137
|
+
class WithFieldsException < ::Sparsam::ApplicationException
|
138
|
+
FIELDS = {
|
139
|
+
1 => {:type => ::Sparsam::Types::I64, :name => 'an_i64', :optional => true},
|
140
|
+
2 => {:type => ::Sparsam::Types::STRING, :name => 'message', :optional => true}
|
141
|
+
}
|
142
|
+
|
143
|
+
init_thrift_struct(self)
|
144
|
+
end
|
145
|
+
|
data/spec/sparsam_spec.rb
CHANGED
@@ -460,5 +460,27 @@ describe 'Sparsam' do
|
|
460
460
|
|
461
461
|
expect { data.serialize }.not_to raise_error
|
462
462
|
end
|
463
|
+
|
464
|
+
describe 'ApplicationException' do
|
465
|
+
it 'creates exceptions that can be raised' do
|
466
|
+
e = SimpleException.new(message: "Oops")
|
467
|
+
|
468
|
+
e.message.should eq("Oops")
|
469
|
+
|
470
|
+
expect { raise e }.to raise_error(SimpleException, "Oops")
|
471
|
+
end
|
472
|
+
|
473
|
+
it 'serializes and reads exceptions' do
|
474
|
+
e = SimpleException.new(message: "Oops")
|
475
|
+
|
476
|
+
data = e.serialize
|
477
|
+
|
478
|
+
e2 = Sparsam::Deserializer.deserialize(SimpleException, data)
|
479
|
+
|
480
|
+
e2.message.should eq("Oops")
|
481
|
+
|
482
|
+
expect { raise e2 }.to raise_error(SimpleException, "Oops")
|
483
|
+
end
|
484
|
+
end
|
463
485
|
end
|
464
486
|
end
|
data/spec/user.thrift
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sparsam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Airbnb Thrift Developers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -90,24 +90,26 @@ extra_rdoc_files:
|
|
90
90
|
- README.md
|
91
91
|
- ext/ruby_hooks.c
|
92
92
|
- ext/serializer.h
|
93
|
-
- ext/third-party/sparsepp/sparsepp/spp.h
|
94
|
-
- ext/third-party/sparsepp/sparsepp/spp_config.h
|
95
|
-
- ext/third-party/sparsepp/sparsepp/spp_dlalloc.h
|
96
93
|
- ext/third-party/sparsepp/sparsepp/spp_memory.h
|
94
|
+
- ext/third-party/sparsepp/sparsepp/spp_utils.h
|
97
95
|
- ext/third-party/sparsepp/sparsepp/spp_smartptr.h
|
96
|
+
- ext/third-party/sparsepp/sparsepp/spp.h
|
98
97
|
- ext/third-party/sparsepp/sparsepp/spp_stdint.h
|
99
98
|
- ext/third-party/sparsepp/sparsepp/spp_timer.h
|
99
|
+
- ext/third-party/sparsepp/sparsepp/spp_config.h
|
100
100
|
- ext/third-party/sparsepp/sparsepp/spp_traits.h
|
101
|
-
- ext/third-party/sparsepp/sparsepp/
|
101
|
+
- ext/third-party/sparsepp/sparsepp/spp_dlalloc.h
|
102
102
|
- ext/extconf.rb
|
103
103
|
- ext/serializer.cpp
|
104
|
-
- lib/sparsam
|
105
|
-
- lib/sparsam/deserializer.rb
|
106
|
-
- lib/sparsam/exceptions.rb
|
104
|
+
- lib/sparsam.rb
|
107
105
|
- lib/sparsam/struct.rb
|
108
|
-
- lib/sparsam/
|
106
|
+
- lib/sparsam/base_struct.rb
|
109
107
|
- lib/sparsam/union.rb
|
110
|
-
- lib/sparsam.rb
|
108
|
+
- lib/sparsam/application_exception.rb
|
109
|
+
- lib/sparsam/exceptions.rb
|
110
|
+
- lib/sparsam/types.rb
|
111
|
+
- lib/sparsam/deserializer.rb
|
112
|
+
- lib/sparsam/base_type.rb
|
111
113
|
files:
|
112
114
|
- README.md
|
113
115
|
- ext/extconf.rb
|
@@ -124,7 +126,9 @@ files:
|
|
124
126
|
- ext/third-party/sparsepp/sparsepp/spp_traits.h
|
125
127
|
- ext/third-party/sparsepp/sparsepp/spp_utils.h
|
126
128
|
- lib/sparsam.rb
|
127
|
-
- lib/sparsam/
|
129
|
+
- lib/sparsam/application_exception.rb
|
130
|
+
- lib/sparsam/base_struct.rb
|
131
|
+
- lib/sparsam/base_type.rb
|
128
132
|
- lib/sparsam/deserializer.rb
|
129
133
|
- lib/sparsam/exceptions.rb
|
130
134
|
- lib/sparsam/struct.rb
|
@@ -166,7 +170,8 @@ signing_key:
|
|
166
170
|
specification_version: 4
|
167
171
|
summary: Ruby bindings for Apache Thrift
|
168
172
|
test_files:
|
169
|
-
- spec/gen-ruby/user_constants.rb
|
170
|
-
- spec/gen-ruby/user_types.rb
|
171
173
|
- spec/sparsam_spec.rb
|
174
|
+
- spec/gen-ruby/user_types.rb
|
175
|
+
- spec/gen-ruby/user_constants.rb
|
172
176
|
- spec/user.thrift
|
177
|
+
has_rdoc: true
|
data/lib/sparsam/base_class.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
require 'sparsam/types'
|
3
|
-
|
4
|
-
module Sparsam
|
5
|
-
class BaseClass
|
6
|
-
class AccessorNames < Struct.new(:reader, :writer); end
|
7
|
-
|
8
|
-
def self.generate_field_syms(klass)
|
9
|
-
field_syms = {}
|
10
|
-
klass::FIELDS.each do |_, field_info|
|
11
|
-
name = field_info[:name].to_sym
|
12
|
-
accessors = AccessorNames.new(name, :"#{name}=")
|
13
|
-
field_syms[name] = accessors
|
14
|
-
field_syms[name.to_s] = accessors
|
15
|
-
end
|
16
|
-
|
17
|
-
klass.const_set(
|
18
|
-
:FIELD_SYMS,
|
19
|
-
field_syms
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.generate_accessors(klass)
|
24
|
-
klass::FIELDS.each do |field_key, field_info|
|
25
|
-
field_accessor(klass, field_key, field_info)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.generate_default_values(klass)
|
30
|
-
fields_with_default_values = {}
|
31
|
-
klass::FIELDS.each do |fid, field_def|
|
32
|
-
unless field_def[:default].nil?
|
33
|
-
fields_with_default_values[field_def[:name]] = field_def[:default].freeze
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
if fields_with_default_values.empty?
|
38
|
-
klass.const_set(
|
39
|
-
:DEFAULT_VALUES,
|
40
|
-
nil
|
41
|
-
)
|
42
|
-
else
|
43
|
-
klass.const_set(
|
44
|
-
:DEFAULT_VALUES,
|
45
|
-
fields_with_default_values
|
46
|
-
)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.init_thrift_struct(klass)
|
51
|
-
generate_accessors(klass)
|
52
|
-
generate_field_syms(klass)
|
53
|
-
Sparsam.cache_fields(klass)
|
54
|
-
generate_default_values(klass)
|
55
|
-
klass.class_eval(<<-EOF, __FILE__, __LINE__)
|
56
|
-
def struct_fields
|
57
|
-
FIELDS
|
58
|
-
end
|
59
|
-
EOF
|
60
|
-
end
|
61
|
-
|
62
|
-
def serialize(prot = Sparsam::CompactProtocol)
|
63
|
-
s = Sparsam::Serializer.new(prot, "")
|
64
|
-
s.serialize(self.class, self)
|
65
|
-
end
|
66
|
-
|
67
|
-
def validate(mode = Sparsam::NORMAL)
|
68
|
-
Sparsam.validate(self.class, self, mode)
|
69
|
-
end
|
70
|
-
|
71
|
-
private
|
72
|
-
|
73
|
-
def name_to_accessors(name)
|
74
|
-
self.class::FIELD_SYMS[name]
|
75
|
-
end
|
76
|
-
|
77
|
-
def each_field
|
78
|
-
struct_fields.each do |fid, data|
|
79
|
-
yield fid, data
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|