thrift-local_type_checking 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9772b1b6974910c8f5c15b94179fe86b3cd14faf
4
+ data.tar.gz: 1a7aa6a10d187b27edc0691d1697f890c4388796
5
+ SHA512:
6
+ metadata.gz: a47d81c474861b9400c9397750ec06cc9b4747a9026335ffc39a0df2ac6ecbc7e3a060983da2aba2e8f6548f9b286a9e20ff747fb4ee747b0427be66b10bf398
7
+ data.tar.gz: b566f8ea4fa8ef8bcc1826943cbad275bfdfb54a963b2e0760c09f281ac4fb8d9058358e6077458d68552af43367186ba3cfc7e25b2bd4b7d42f98f21405f570
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ spec/support/thrift/*.rb
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thrift-local_type_checking.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Vicente Reig Rincon de Arellano
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Thrift::LocalTypeChecking
2
+
3
+ [![Circle CI](https://circleci.com/gh/vicentereig/thrift-local_type_checking.svg?style=svg)](https://circleci.com/gh/vicentereig/thrift-local_type_checking)
4
+
5
+ Enables local type checking per client, as opposed to `Thrift.type_checking = true` flag
6
+ which affects every client running on the client application.
7
+
8
+ It also recursively validates structs.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'thrift-local_type_checking'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install thrift-local_type_checking
25
+
26
+ ## Usage
27
+
28
+ ```thrift
29
+ namespace rb Accounts.V1
30
+
31
+ struct EmailAddress {
32
+ 1:i64 id
33
+ 2:string email
34
+ }
35
+
36
+ struct User {
37
+ 1:i64 id
38
+ 2:required list<EmailAddress> emails
39
+ }
40
+
41
+ struct Account {
42
+ 1:i64 id
43
+ 3:list<User> users
44
+ }
45
+
46
+ service AccountsService {
47
+ void create(1:Account account)
48
+ }
49
+
50
+ ```
51
+
52
+ ```ruby
53
+ client = Accounts::V1::AccountsService::Client.new.extend(Thrift::LocalTypeChecking)
54
+ account = Accounts::V1::Account.new(id: 'not a number! =D', users: nil)
55
+ client.create(account)
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/thrift-local_type_checking/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require './spec/support/rake/thrift.rb'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ test:
2
+ override:
3
+ - bundle exec rake spec:thrift:generate spec
@@ -0,0 +1,5 @@
1
+ module Thrift
2
+ module LocalTypeChecking
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,45 @@
1
+ require 'thrift/local_type_checking/version'
2
+ require 'thrift'
3
+
4
+ module Thrift
5
+ module LocalTypeChecking
6
+ def self.extended(*)
7
+ ::Thrift::Struct.extend(ClassMethods)
8
+ ::Thrift::Struct.overwrite_initialize
9
+ ::Thrift::Struct.instance_eval do
10
+ def method_added(name)
11
+ return if name != :initialize
12
+ overwrite_initialize
13
+ end
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ def overwrite_initialize
19
+ class_eval do
20
+ unless method_defined?(:custom_initialize)
21
+ define_method(:custom_initialize) do |d={}|
22
+ original_initialize(d)
23
+
24
+ unless d.empty?
25
+ d.each do |name, value|
26
+ unless name_to_id(name.to_s)
27
+ raise Exception, "Unknown key given to #{self.class}.new: #{name}"
28
+ end
29
+ Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name)
30
+ instance_variable_set("@#{name}", value)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ if instance_method(:initialize) != instance_method(:custom_initialize)
37
+ alias_method :original_initialize, :initialize
38
+ alias_method :initialize, :custom_initialize
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,9 @@
1
+ $:.unshift(File.expand_path('../lib', __FILE__))
2
+ $:.unshift(File.expand_path('../support/thrift', __FILE__))
3
+
4
+ require 'rspec'
5
+ require 'byebug'
6
+
7
+ require 'thrift/local_type_checking'
8
+ require 'accounts_service'
9
+
@@ -0,0 +1,22 @@
1
+ namespace rb Accounts.V1
2
+
3
+ struct EmailAddress {
4
+ 1:i64 id
5
+ 2:string email
6
+ }
7
+
8
+ struct User {
9
+ 1:i64 id
10
+ 2:required list<EmailAddress> emails
11
+ }
12
+
13
+ struct Account {
14
+ 1:i64 id
15
+ 3:list<User> users
16
+ }
17
+
18
+ service AccountsService {
19
+ void create(1:Account account)
20
+ void set_account_attribute(1:i64 attribute_value)
21
+ oneway void update(1:Account account)
22
+ }
@@ -0,0 +1,8 @@
1
+ namespace :spec do
2
+ namespace :thrift do
3
+ desc 'Generate Thrift client and service files'
4
+ task :generate do
5
+ sh 'bundle exec thrift --out spec/support/thrift --gen rb spec/support/accounts.thrift'
6
+ end
7
+ end
8
+ end
File without changes
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Thrift::LocalTypeChecking do
4
+
5
+ it 'has a version number' do
6
+ expect(Thrift::LocalTypeChecking::VERSION).not_to be nil
7
+ end
8
+
9
+ let(:base_client) { Accounts::V1::AccountsService::Client.new(protocol) }
10
+
11
+ describe 'when invoking the default Thrift client with invalid arguments' do
12
+ let(:protocol) { Thrift::CompactProtocol.new Thrift::MemoryBufferTransport.new }
13
+
14
+ describe 'scalars' do
15
+ let(:invalid_id) { 'should be a number :)'}
16
+ let(:invalid_users) { nil }
17
+ let(:invalid_account) { Accounts::V1::Account.new(id: invalid_id, users: invalid_users)}
18
+
19
+ it 'should raise a TypeError' do
20
+ expect {
21
+ base_client.create(invalid_account)
22
+ }.to raise_error
23
+ end
24
+ end
25
+
26
+ describe 'lists' do
27
+ let(:id) { 1 }
28
+ let(:invalid_users) { nil }
29
+ let(:invalid_account) { Accounts::V1::Account.new(id: id, users: invalid_users)}
30
+
31
+ it 'should not raise a TypeError' do
32
+ expect {
33
+ base_client.create(invalid_account)
34
+ }.not_to raise_error
35
+ end
36
+ end
37
+ end
38
+
39
+ describe 'when invoking the enhaced Thrift client with invalid arguments' do
40
+ let(:protocol) { Thrift::CompactProtocol.new Thrift::MemoryBufferTransport.new }
41
+ let(:client) { Accounts::V1::AccountsService::Client.new(protocol).extend(Thrift::LocalTypeChecking)}
42
+
43
+ describe 'scalars' do
44
+ let(:invalid_id) { 'should be a number :)'}
45
+ let(:invalid_users) { nil }
46
+ let(:invalid_account) { Accounts::V1::Account.new(id: invalid_id, users: invalid_users)}
47
+
48
+ it 'should raise a TypeError' do
49
+ expect {
50
+ client.create(invalid_account)
51
+ }.to raise_error
52
+ end
53
+ end
54
+
55
+ describe 'lists' do
56
+ let(:id) { 1 }
57
+ let(:invalid_users) { nil }
58
+ let(:invalid_account) { Accounts::V1::Account.new(id: id, users: invalid_users)}
59
+
60
+ it 'should not raise a TypeError' do
61
+ expect {
62
+ client.create(invalid_account)
63
+ }.not_to raise_error
64
+ end
65
+ end
66
+
67
+ describe 'nested structures' do
68
+ let(:id) { 1 }
69
+ let(:invalid_emails) { [Accounts::V1::EmailAddress.new(id: 'not a number', email: 'vicente@trolo.com')] }
70
+ let(:invalid_users) { [Accounts::V1::User.new(emails: invalid_emails)] }
71
+ let(:invalid_account) { Accounts::V1::Account.new(id: id, users: invalid_users)}
72
+
73
+ it 'should not raise a TypeError' do
74
+ expect {
75
+ client.create(invalid_account)
76
+ }.to raise_error Thrift::TypeError, 'Expected Types::I64, received String for field id'
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thrift/local_type_checking/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'thrift-local_type_checking'
8
+ spec.version = Thrift::LocalTypeChecking::VERSION
9
+ spec.authors = ['Vicente Reig Rincón de Arellano']
10
+ spec.email = ['vicente.reig@gmail.com']
11
+ spec.summary = %q{Enables Type Checking per client}
12
+ spec.description = %q{Enables Type Checking per client}
13
+ spec.homepage = 'https://github.com/vicentereig/thrift-local_type_checking'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6.5'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'byebug'
25
+ spec.add_dependency 'thrift'
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thrift-local_type_checking
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vicente Reig Rincón de Arellano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.5
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thrift
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Enables Type Checking per client
84
+ email:
85
+ - vicente.reig@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-version"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - circle.yml
98
+ - lib/thrift/local_type_checking.rb
99
+ - lib/thrift/local_type_checking/version.rb
100
+ - spec/spec_helper.rb
101
+ - spec/support/accounts.thrift
102
+ - spec/support/rake/thrift.rb
103
+ - spec/support/thrift/.gitkeep
104
+ - spec/thrift/local_type_checking_spec.rb
105
+ - thrift-local_type_checking.gemspec
106
+ homepage: https://github.com/vicentereig/thrift-local_type_checking
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Enables Type Checking per client
130
+ test_files:
131
+ - spec/spec_helper.rb
132
+ - spec/support/accounts.thrift
133
+ - spec/support/rake/thrift.rb
134
+ - spec/support/thrift/.gitkeep
135
+ - spec/thrift/local_type_checking_spec.rb