grpc_typechecker 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 643cfc11b7ddbc6ff80cdb3e52add070e6d5832e7bfecafd5abafde411c3248d
4
+ data.tar.gz: 6505ceb3c61e0a319c83646b8dfea0b4d3d97223eb5000ab18f4558e9b737e17
5
+ SHA512:
6
+ metadata.gz: aa01d8087578c31ffe19e97342b567162f277efc164da0a28847f0555811402f0edd9f63b1b9fae57c31687c47851c7b590862373bde8cccbdcb7e5e42357ba7
7
+ data.tar.gz: aeda9191c06cdb71d85d0cae8f2603adaacebd1968a802f33c40539318d2b58ffed787bb85097ac240f39cb519111be74d072293678b20085bf07eae5d8fbc3f
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in grpc_typechecker.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Masayuki Mizuno
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # GrpcTypechecker [![Build Status](https://travis-ci.com/wantedly/grpc_typechecker.svg?branch=master)](https://travis-ci.com/wantedly/grpc_typechecker)
2
+
3
+ A dynamic type checker for gRPC methods. This gem consists of two features:
4
+
5
+ - a client interceptor for the run-time type check of gRPC requests, and
6
+ - a monkey patch for type checking gRPC responses during execution.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'grpc_typechecker'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install grpc_typechecker
21
+
22
+ ## Usage
23
+
24
+ If you would like to type check the gRPC requsts, please set the instance of `GrpcTypechecker::ClientInterceptor` as an interceptor of your gRPC application. For instance:
25
+
26
+ ```ruby
27
+ server = Grpc::Testing::TestService::Stub.new(
28
+ "localhost:12345",
29
+ :this_port_is_insecure,
30
+ interceptors: [
31
+ GrpcTypechecker::ClientInterceptor(service_class: Grpc::Testing::TestService)
32
+ ]
33
+ )
34
+ ```
35
+
36
+ On the other hand, the dynamic type checker for gRPC responses will be automatically introduced only by requiring `grpc_typechecker`
37
+
38
+ ```ruby
39
+ require 'grpc_typechecker'
40
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "grpc_typechecker"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/grpc_typechecker/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "grpc_typechecker"
5
+ spec.version = GrpcTypechecker::VERSION
6
+ spec.authors = ["Masayuki Mizuno"]
7
+ spec.email = ["mizuno@wantedly.com"]
8
+
9
+ spec.summary = "A dynamic type checker for gRPC methods"
10
+ spec.description = "A dynamic type checker for gRPC methods"
11
+ spec.homepage = "https://github.com/wantedly/grpc_typechecker"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/wantedly/grpc_typechecker"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency 'grpc'
28
+ spec.add_dependency 'activesupport'
29
+ spec.add_development_dependency 'rspec'
30
+ end
@@ -0,0 +1,3 @@
1
+ require "grpc_typechecker/version"
2
+ require "grpc_typechecker/grpc_ext"
3
+ require "grpc_typechecker/client_interceptor"
@@ -0,0 +1,40 @@
1
+ module GrpcTypechecker
2
+ class ClientInterceptor < GRPC::ClientInterceptor
3
+ def initialize(service_class: nil)
4
+ @service = service_class::Service
5
+ end
6
+
7
+ def request_response(request: nil, call: nil, method: nil, metadata: nil)
8
+ # Raise InvalidArgument if the argument of gRPC method is ill-typed
9
+ unless request.is_a?(request_class(method))
10
+ raise GRPC::InvalidArgument, "#{method}: expected an instance of #{request_class(method)}, got an instance of #{request.class}"
11
+ end
12
+ yield
13
+ end
14
+
15
+ def client_streamer(requests: nil, call: nil, method: nil, metadata: nil)
16
+ # no-op
17
+ yield
18
+ end
19
+
20
+ def server_streamer(request: nil, call: nil, method: nil, metadata: nil)
21
+ # no-op
22
+ yield
23
+ end
24
+
25
+ def bidi_streamer(requests: nil, call: nil, method: nil, metadata: nil)
26
+ # no-op
27
+ yield
28
+ end
29
+
30
+ private
31
+
32
+ # @param [String] method A string which represents a full path of a gRPC
33
+ # method. e.g. "/wantedly.users.UserService/GetUser"
34
+ # @return [Class]
35
+ def request_class(method)
36
+ rpc_method = method.split('/')[2].to_sym
37
+ @service.rpc_descs[rpc_method][:input]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,59 @@
1
+ require "grpc"
2
+ require "active_support/core_ext/string/inflections"
3
+
4
+ module GRPC::GenericService
5
+ # The usual way to supplement class methods is use of `Module#included'.
6
+ # `GRPC::GenericService' already contains `included', thus we wrap `self.included' by `Module#prepend'.
7
+
8
+ module IncludedForClassMethods
9
+ def included(base)
10
+ super
11
+ base.extend(GRPC::GenericService::ClassMethods)
12
+ end
13
+ end
14
+
15
+ class << self
16
+ prepend IncludedForClassMethods
17
+ end
18
+
19
+ module ClassMethods
20
+ def inherited(subclass)
21
+ super if defined? super
22
+
23
+ mod = Module.new do
24
+ singleton_class.class_eval do
25
+ # Some server interceptors try to obtain the service name by `method.owner.service_name`
26
+ # despite `method` is overwritten by the method in this module.
27
+ # We thus redirect class method calls of `service_name` for compatibility.
28
+ define_method :service_name do
29
+ subclass.service_name
30
+ end
31
+ end
32
+ end
33
+
34
+ subclass.class_eval do
35
+ prepend mod
36
+ @_prepended_module_for_type_check_ = mod
37
+ end
38
+ end
39
+
40
+ def method_added(method)
41
+ super if defined? super
42
+
43
+ rpc_method = rpc_descs[method.to_s.camelize.to_sym]
44
+ return unless rpc_method && @_prepended_module_for_type_check_
45
+
46
+ # Overwrite the gRPC method
47
+ @_prepended_module_for_type_check_.class_eval do
48
+ define_method method do |*args, **kwargs|
49
+ response = super(*args, **kwargs)
50
+ # Raise Internal if the response of gRPC method is ill-typed
51
+ unless response.is_a?(rpc_method[:output])
52
+ raise GRPC::Internal, "the response of #{method} is expected to be an instance of #{rpc_method[:output]}, but the response is an instance of #{response.class}"
53
+ end
54
+ response
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module GrpcTypechecker
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grpc_typechecker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masayuki Mizuno
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grpc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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
+ description: A dynamic type checker for gRPC methods
56
+ email:
57
+ - mizuno@wantedly.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - grpc_typechecker.gemspec
72
+ - lib/grpc_typechecker.rb
73
+ - lib/grpc_typechecker/client_interceptor.rb
74
+ - lib/grpc_typechecker/grpc_ext.rb
75
+ - lib/grpc_typechecker/version.rb
76
+ homepage: https://github.com/wantedly/grpc_typechecker
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/wantedly/grpc_typechecker
81
+ source_code_uri: https://github.com/wantedly/grpc_typechecker
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.3.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.1.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A dynamic type checker for gRPC methods
101
+ test_files: []