From caa655cb8a362e86c89d057e392b26df6bd5abf0 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 19:09:13 +1100 Subject: [PATCH 01/19] [Remove activesupport] Remove require for unused 'active_support/inflector' --- lib/protobuf.rb | 1 - lib/protobuf/cli.rb | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/protobuf.rb b/lib/protobuf.rb index 69139e2c..cb15b3a5 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -6,7 +6,6 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/try' -require 'active_support/inflector' require 'active_support/json' require 'active_support/notifications' # Under MRI, this optimizes proto decoding by around 15% in tests. diff --git a/lib/protobuf/cli.rb b/lib/protobuf/cli.rb index 35c6d936..3211f67d 100644 --- a/lib/protobuf/cli.rb +++ b/lib/protobuf/cli.rb @@ -1,5 +1,4 @@ require 'active_support/core_ext/hash/keys' -require 'active_support/inflector' require 'thor' require 'protobuf/version' From 98481bc7ad144e21f33df90632243aba20b994a4 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 19:28:16 +1100 Subject: [PATCH 02/19] [Remove activesupport] Replace 'active_support/core_ext/object/blank' with refinement --- lib/protobuf.rb | 1 - .../field/base_field_object_definitions.rb | 4 ++++ lib/protobuf/message/fields.rb | 3 +++ lib/protobuf/refinements/object/blank.rb | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lib/protobuf/refinements/object/blank.rb diff --git a/lib/protobuf.rb b/lib/protobuf.rb index cb15b3a5..9755ce69 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -4,7 +4,6 @@ require 'socket' require 'stringio' -require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/try' require 'active_support/json' require 'active_support/notifications' diff --git a/lib/protobuf/field/base_field_object_definitions.rb b/lib/protobuf/field/base_field_object_definitions.rb index 93a93226..b9a2343c 100644 --- a/lib/protobuf/field/base_field_object_definitions.rb +++ b/lib/protobuf/field/base_field_object_definitions.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/object/blank' + +using Protobuf::Refinements::Object::Blank + module Protobuf module Field module BaseFieldObjectDefinitions diff --git a/lib/protobuf/message/fields.rb b/lib/protobuf/message/fields.rb index d07e1eff..fe80439a 100644 --- a/lib/protobuf/message/fields.rb +++ b/lib/protobuf/message/fields.rb @@ -1,4 +1,7 @@ require "set" +require 'protobuf/refinements/object/blank' + +using Protobuf::Refinements::Object::Blank module Protobuf class Message diff --git a/lib/protobuf/refinements/object/blank.rb b/lib/protobuf/refinements/object/blank.rb new file mode 100644 index 00000000..b15f7d25 --- /dev/null +++ b/lib/protobuf/refinements/object/blank.rb @@ -0,0 +1,19 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/core_ext/object/blank.rb + +module Protobuf + module Refinements + module Object + module Blank + refine ::Object do + def blank? + respond_to?(:empty?) ? !!empty? : !self + end + + def present? + !blank? + end + end + end + end + end +end From 8b626515136ea329889c5838a3918c0603e3e112 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 19:49:26 +1100 Subject: [PATCH 03/19] [Remove activesupport] Replace 'active_support/core_ext/object/try' with refinement --- lib/protobuf.rb | 1 - lib/protobuf/enum.rb | 4 ++ lib/protobuf/generators/enum_generator.rb | 3 ++ lib/protobuf/generators/field_generator.rb | 3 ++ lib/protobuf/generators/group_generator.rb | 3 ++ lib/protobuf/message/fields.rb | 2 + lib/protobuf/optionable.rb | 4 ++ lib/protobuf/refinements/object/try.rb | 55 ++++++++++++++++++++++ lib/protobuf/rpc/connectors/zmq.rb | 3 ++ lib/protobuf/rpc/middleware/logger.rb | 4 ++ lib/protobuf/rpc/servers/zmq/broker.rb | 3 ++ lib/protobuf/rpc/servers/zmq/server.rb | 3 ++ lib/protobuf/rpc/servers/zmq/worker.rb | 3 ++ lib/protobuf/rpc/servers/zmq_runner.rb | 3 ++ lib/protobuf/rpc/service_directory.rb | 3 ++ lib/protobuf/rpc/stat.rb | 3 ++ spec/lib/protobuf/enum_spec.rb | 4 ++ 17 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 lib/protobuf/refinements/object/try.rb diff --git a/lib/protobuf.rb b/lib/protobuf.rb index 9755ce69..5a951295 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -4,7 +4,6 @@ require 'socket' require 'stringio' -require 'active_support/core_ext/object/try' require 'active_support/json' require 'active_support/notifications' # Under MRI, this optimizes proto decoding by around 15% in tests. diff --git a/lib/protobuf/enum.rb b/lib/protobuf/enum.rb index c0a2a809..beba312f 100644 --- a/lib/protobuf/enum.rb +++ b/lib/protobuf/enum.rb @@ -1,5 +1,9 @@ require 'delegate' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + ## # Adding extension to Numeric until # we can get people to stop calling #value diff --git a/lib/protobuf/generators/enum_generator.rb b/lib/protobuf/generators/enum_generator.rb index 1088e3ed..9b836ddc 100644 --- a/lib/protobuf/generators/enum_generator.rb +++ b/lib/protobuf/generators/enum_generator.rb @@ -1,5 +1,8 @@ require 'protobuf/generators/base' require 'protobuf/generators/option_generator' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Generators diff --git a/lib/protobuf/generators/field_generator.rb b/lib/protobuf/generators/field_generator.rb index 68652fcc..c718d90a 100644 --- a/lib/protobuf/generators/field_generator.rb +++ b/lib/protobuf/generators/field_generator.rb @@ -1,4 +1,7 @@ require 'protobuf/generators/base' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Generators diff --git a/lib/protobuf/generators/group_generator.rb b/lib/protobuf/generators/group_generator.rb index 33e7b5be..81b81683 100644 --- a/lib/protobuf/generators/group_generator.rb +++ b/lib/protobuf/generators/group_generator.rb @@ -4,6 +4,9 @@ require 'protobuf/generators/message_generator' require 'protobuf/generators/option_generator' require 'protobuf/generators/service_generator' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Generators diff --git a/lib/protobuf/message/fields.rb b/lib/protobuf/message/fields.rb index fe80439a..23bbf00a 100644 --- a/lib/protobuf/message/fields.rb +++ b/lib/protobuf/message/fields.rb @@ -1,7 +1,9 @@ require "set" require 'protobuf/refinements/object/blank' +require 'protobuf/refinements/object/try' using Protobuf::Refinements::Object::Blank +using Protobuf::Refinements::Object::Try module Protobuf class Message diff --git a/lib/protobuf/optionable.rb b/lib/protobuf/optionable.rb index 34b00c93..e6dc0a6f 100644 --- a/lib/protobuf/optionable.rb +++ b/lib/protobuf/optionable.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + module Protobuf module Optionable module ClassMethods diff --git a/lib/protobuf/refinements/object/try.rb b/lib/protobuf/refinements/object/try.rb new file mode 100644 index 00000000..f3e1a3d4 --- /dev/null +++ b/lib/protobuf/refinements/object/try.rb @@ -0,0 +1,55 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/core_ext/object/try.rb + +module Protobuf + module Refinements + module Object + module Try + module Tryable #:nodoc: + def try(method_name = nil, *args, &b) + if method_name.nil? && block_given? + if b.arity == 0 + instance_eval(&b) + else + yield self + end + elsif respond_to?(method_name) + public_send(method_name, *args, &b) + end + end + ruby2_keywords(:try) if respond_to?(:ruby2_keywords, true) + + def try!(method_name = nil, *args, &b) + if method_name.nil? && block_given? + if b.arity == 0 + instance_eval(&b) + else + yield self + end + else + public_send(method_name, *args, &b) + end + end + ruby2_keywords(:try!) if respond_to?(:ruby2_keywords, true) + end + + refine ::Object do + include Tryable + end + + refine ::Delegator do + include Tryable + end + + refine ::NilClass do + def try(method_name = nil, *args) + nil + end + + def try!(method_name = nil, *args) + nil + end + end + end + end + end +end diff --git a/lib/protobuf/rpc/connectors/zmq.rb b/lib/protobuf/rpc/connectors/zmq.rb index 3d99fb96..be517d61 100644 --- a/lib/protobuf/rpc/connectors/zmq.rb +++ b/lib/protobuf/rpc/connectors/zmq.rb @@ -2,6 +2,9 @@ require "protobuf/rpc/connectors/base" require "protobuf/rpc/connectors/ping" require "protobuf/rpc/service_directory" +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Rpc diff --git a/lib/protobuf/rpc/middleware/logger.rb b/lib/protobuf/rpc/middleware/logger.rb index 2c02b9a4..d833fa03 100644 --- a/lib/protobuf/rpc/middleware/logger.rb +++ b/lib/protobuf/rpc/middleware/logger.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc module Middleware diff --git a/lib/protobuf/rpc/servers/zmq/broker.rb b/lib/protobuf/rpc/servers/zmq/broker.rb index 2b12aee8..5f9e7278 100644 --- a/lib/protobuf/rpc/servers/zmq/broker.rb +++ b/lib/protobuf/rpc/servers/zmq/broker.rb @@ -1,4 +1,7 @@ require 'thread' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Rpc diff --git a/lib/protobuf/rpc/servers/zmq/server.rb b/lib/protobuf/rpc/servers/zmq/server.rb index b5dce25a..fb45e928 100644 --- a/lib/protobuf/rpc/servers/zmq/server.rb +++ b/lib/protobuf/rpc/servers/zmq/server.rb @@ -1,3 +1,4 @@ +require 'protobuf/refinements/object/try' require 'protobuf/rpc/servers/zmq/util' require 'protobuf/rpc/servers/zmq/worker' require 'protobuf/rpc/servers/zmq/broker' @@ -5,6 +6,8 @@ require 'securerandom' require 'thread' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc module Zmq diff --git a/lib/protobuf/rpc/servers/zmq/worker.rb b/lib/protobuf/rpc/servers/zmq/worker.rb index 85e4aa2a..82d20479 100644 --- a/lib/protobuf/rpc/servers/zmq/worker.rb +++ b/lib/protobuf/rpc/servers/zmq/worker.rb @@ -1,7 +1,10 @@ +require 'protobuf/refinements/object/try' require 'protobuf/rpc/server' require 'protobuf/rpc/servers/zmq/util' require 'thread' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc module Zmq diff --git a/lib/protobuf/rpc/servers/zmq_runner.rb b/lib/protobuf/rpc/servers/zmq_runner.rb index bfab6738..473d1893 100644 --- a/lib/protobuf/rpc/servers/zmq_runner.rb +++ b/lib/protobuf/rpc/servers/zmq_runner.rb @@ -1,5 +1,8 @@ require 'ostruct' require 'thread' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Rpc diff --git a/lib/protobuf/rpc/service_directory.rb b/lib/protobuf/rpc/service_directory.rb index 7c467358..35f31d5c 100644 --- a/lib/protobuf/rpc/service_directory.rb +++ b/lib/protobuf/rpc/service_directory.rb @@ -5,8 +5,11 @@ require 'thread' require 'timeout' +require 'protobuf/refinements/object/try' require 'protobuf/rpc/dynamic_discovery.pb' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc def self.service_directory diff --git a/lib/protobuf/rpc/stat.rb b/lib/protobuf/rpc/stat.rb index 841181a3..fe7dd127 100644 --- a/lib/protobuf/rpc/stat.rb +++ b/lib/protobuf/rpc/stat.rb @@ -1,8 +1,11 @@ require 'date' require 'time' require 'protobuf/logging' +require 'protobuf/refinements/object/try' require 'protobuf/rpc/rpc.pb' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc class Stat diff --git a/spec/lib/protobuf/enum_spec.rb b/spec/lib/protobuf/enum_spec.rb index f0ace1d6..511acb1c 100644 --- a/spec/lib/protobuf/enum_spec.rb +++ b/spec/lib/protobuf/enum_spec.rb @@ -1,6 +1,10 @@ require 'spec_helper' require PROTOS_PATH.join('enum.pb') +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + RSpec.describe Protobuf::Enum do describe 'class dsl' do From 4e4f028f26d680e435dc0132e2f9d70a72ee8b1e Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 19:59:11 +1100 Subject: [PATCH 04/19] [Remove activesupport] Replace 'active_support/core_ext/hash/keys' with refinement --- lib/protobuf/cli.rb | 5 +++-- lib/protobuf/refinements/hash/symbolize_keys.rb | 15 +++++++++++++++ lib/protobuf/rpc/servers/socket_runner.rb | 4 ++++ lib/protobuf/rpc/servers/zmq_runner.rb | 2 ++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 lib/protobuf/refinements/hash/symbolize_keys.rb diff --git a/lib/protobuf/cli.rb b/lib/protobuf/cli.rb index 3211f67d..d72a5a01 100644 --- a/lib/protobuf/cli.rb +++ b/lib/protobuf/cli.rb @@ -1,10 +1,11 @@ -require 'active_support/core_ext/hash/keys' - require 'thor' require 'protobuf/version' require 'protobuf/logging' require 'protobuf/rpc/servers/socket_runner' require 'protobuf/rpc/servers/zmq_runner' +require 'protobuf/refinements/hash/symbolize_keys' + +using Protobuf::Refinements::Hash::SymbolizeKeys module Protobuf class CLI < ::Thor diff --git a/lib/protobuf/refinements/hash/symbolize_keys.rb b/lib/protobuf/refinements/hash/symbolize_keys.rb new file mode 100644 index 00000000..a17f817f --- /dev/null +++ b/lib/protobuf/refinements/hash/symbolize_keys.rb @@ -0,0 +1,15 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/core_ext/hash/keys.rb + +module Protobuf + module Refinements + module Hash + module SymbolizeKeys + refine ::Hash do + def symbolize_keys + transform_keys { |key| key.to_sym rescue key } + end + end + end + end + end +end diff --git a/lib/protobuf/rpc/servers/socket_runner.rb b/lib/protobuf/rpc/servers/socket_runner.rb index 8d36182d..b866c124 100644 --- a/lib/protobuf/rpc/servers/socket_runner.rb +++ b/lib/protobuf/rpc/servers/socket_runner.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/hash/symbolize_keys' + +using Protobuf::Refinements::Hash::SymbolizeKeys + module Protobuf module Rpc class SocketRunner diff --git a/lib/protobuf/rpc/servers/zmq_runner.rb b/lib/protobuf/rpc/servers/zmq_runner.rb index 473d1893..1c392539 100644 --- a/lib/protobuf/rpc/servers/zmq_runner.rb +++ b/lib/protobuf/rpc/servers/zmq_runner.rb @@ -1,7 +1,9 @@ require 'ostruct' require 'thread' +require 'protobuf/refinements/hash/symbolize_keys' require 'protobuf/refinements/object/try' +using Protobuf::Refinements::Hash::SymbolizeKeys using Protobuf::Refinements::Object::Try module Protobuf From e5a4384803cd8515a98a9adc4bbf1ec890b75e2e Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 20:00:56 +1100 Subject: [PATCH 05/19] [Remove activesupport] Remove require for unused 'active_support/core_ext/module/aliasing' --- lib/protobuf/code_generator.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/protobuf/code_generator.rb b/lib/protobuf/code_generator.rb index 7b04a32c..8ef70ba2 100644 --- a/lib/protobuf/code_generator.rb +++ b/lib/protobuf/code_generator.rb @@ -1,4 +1,3 @@ -require 'active_support/core_ext/module/aliasing' require 'protobuf/generators/file_generator' module Protobuf From 2543ba92264bf6bea3383d7b8468a2dc60770a78 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 21:18:01 +1100 Subject: [PATCH 06/19] [Remove activesupport] Expand Blank refinement with code from facets --- .../field/base_field_object_definitions.rb | 4 +- lib/protobuf/message/fields.rb | 4 +- lib/protobuf/refinements/kernel/blank.rb | 62 +++++++++++++++++++ lib/protobuf/refinements/object/blank.rb | 19 ------ 4 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 lib/protobuf/refinements/kernel/blank.rb delete mode 100644 lib/protobuf/refinements/object/blank.rb diff --git a/lib/protobuf/field/base_field_object_definitions.rb b/lib/protobuf/field/base_field_object_definitions.rb index b9a2343c..8a3dba1d 100644 --- a/lib/protobuf/field/base_field_object_definitions.rb +++ b/lib/protobuf/field/base_field_object_definitions.rb @@ -1,6 +1,6 @@ -require 'protobuf/refinements/object/blank' +require 'protobuf/refinements/kernel/blank' -using Protobuf::Refinements::Object::Blank +using Protobuf::Refinements::Kernel::Blank module Protobuf module Field diff --git a/lib/protobuf/message/fields.rb b/lib/protobuf/message/fields.rb index 23bbf00a..f1c9d3a3 100644 --- a/lib/protobuf/message/fields.rb +++ b/lib/protobuf/message/fields.rb @@ -1,8 +1,8 @@ require "set" -require 'protobuf/refinements/object/blank' +require 'protobuf/refinements/kernel/blank' require 'protobuf/refinements/object/try' -using Protobuf::Refinements::Object::Blank +using Protobuf::Refinements::Kernel::Blank using Protobuf::Refinements::Object::Try module Protobuf diff --git a/lib/protobuf/refinements/kernel/blank.rb b/lib/protobuf/refinements/kernel/blank.rb new file mode 100644 index 00000000..42dd9627 --- /dev/null +++ b/lib/protobuf/refinements/kernel/blank.rb @@ -0,0 +1,62 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/kernel/blank.rb + +module Protobuf + module Refinements + module Kernel + module Blank + refine ::Kernel do + def blank? + return empty? if respond_to?(:empty?) + !self + end + + def present? + !blank? + end + + def presence + self if present? + end + end + + refine ::NilClass do + def blank? + true + end + end + + refine ::FalseClass do + def blank? + true + end + end + + refine ::TrueClass do + def blank? + false + end + end + + refine ::Array do + alias_method :blank?, :empty? + end + + refine ::Hash do + alias_method :blank?, :empty? + end + + refine ::String do + def blank? + /\S/ !~ self + end + end + + refine ::Numeric do + def blank? + false + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/object/blank.rb b/lib/protobuf/refinements/object/blank.rb deleted file mode 100644 index b15f7d25..00000000 --- a/lib/protobuf/refinements/object/blank.rb +++ /dev/null @@ -1,19 +0,0 @@ -# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/core_ext/object/blank.rb - -module Protobuf - module Refinements - module Object - module Blank - refine ::Object do - def blank? - respond_to?(:empty?) ? !!empty? : !self - end - - def present? - !blank? - end - end - end - end - end -end From c163185eb17c7573bf7138c91d21bb7c7d0db212 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 21:21:24 +1100 Subject: [PATCH 07/19] [Remove activesupport] Substitute sole usage of #titleize --- spec/bin/protoc-gen-ruby_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/bin/protoc-gen-ruby_spec.rb b/spec/bin/protoc-gen-ruby_spec.rb index 1df8b155..9991a8c5 100644 --- a/spec/bin/protoc-gen-ruby_spec.rb +++ b/spec/bin/protoc-gen-ruby_spec.rb @@ -17,7 +17,7 @@ pipe.close_write # needed so we can implicitly read until EOF response_bytes = pipe.read response = ::Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_bytes) - expect(response.file.first.content).to include("module #{package.titleize}") + expect(response.file.first.content).to include("module Test") end end end From c9f2b81fa2ead741f7ddbf007f975b82a32f8b80 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 21:23:11 +1100 Subject: [PATCH 08/19] [Remove activesupport] Add refinements for #camelize, #uderscore, #constantize, and #classify --- lib/protobuf.rb | 7 +++ lib/protobuf/cli.rb | 4 ++ lib/protobuf/generators/file_generator.rb | 3 ++ lib/protobuf/generators/service_generator.rb | 3 ++ lib/protobuf/message.rb | 5 ++ lib/protobuf/refinements/string/camelize.rb | 35 ++++++++++++ lib/protobuf/refinements/string/classify.rb | 19 +++++++ .../refinements/string/constantize.rb | 53 +++++++++++++++++++ lib/protobuf/refinements/string/underscore.rb | 20 +++++++ .../rpc/middleware/request_decoder.rb | 6 +++ spec/support/packed_field.rb | 4 ++ 11 files changed, 159 insertions(+) create mode 100644 lib/protobuf/refinements/string/camelize.rb create mode 100644 lib/protobuf/refinements/string/classify.rb create mode 100644 lib/protobuf/refinements/string/constantize.rb create mode 100644 lib/protobuf/refinements/string/underscore.rb diff --git a/lib/protobuf.rb b/lib/protobuf.rb index 5a951295..878c7998 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -6,6 +6,13 @@ require 'active_support/json' require 'active_support/notifications' + +require 'protobuf/refinements/string/classify' +require 'protobuf/refinements/string/constantize' + +using Protobuf::Refinements::String::Classify +using Protobuf::Refinements::String::Constantize + # Under MRI, this optimizes proto decoding by around 15% in tests. # When unavailable, we fall to pure Ruby. # rubocop:disable Lint/HandleExceptions diff --git a/lib/protobuf/cli.rb b/lib/protobuf/cli.rb index d72a5a01..10803ae2 100644 --- a/lib/protobuf/cli.rb +++ b/lib/protobuf/cli.rb @@ -4,8 +4,12 @@ require 'protobuf/rpc/servers/socket_runner' require 'protobuf/rpc/servers/zmq_runner' require 'protobuf/refinements/hash/symbolize_keys' +require 'protobuf/refinements/string/classify' +require 'protobuf/refinements/string/constantize' using Protobuf::Refinements::Hash::SymbolizeKeys +using Protobuf::Refinements::String::Classify +using Protobuf::Refinements::String::Constantize module Protobuf class CLI < ::Thor diff --git a/lib/protobuf/generators/file_generator.rb b/lib/protobuf/generators/file_generator.rb index f94d2264..18af88d5 100644 --- a/lib/protobuf/generators/file_generator.rb +++ b/lib/protobuf/generators/file_generator.rb @@ -1,6 +1,9 @@ require 'set' require 'protobuf/generators/base' require 'protobuf/generators/group_generator' +require 'protobuf/refinements/string/constantize' + +using Protobuf::Refinements::String::Constantize module Protobuf module Generators diff --git a/lib/protobuf/generators/service_generator.rb b/lib/protobuf/generators/service_generator.rb index f7e9febd..a65d991b 100644 --- a/lib/protobuf/generators/service_generator.rb +++ b/lib/protobuf/generators/service_generator.rb @@ -1,5 +1,8 @@ require 'protobuf/generators/base' require 'protobuf/generators/option_generator' +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Underscore module Protobuf module Generators diff --git a/lib/protobuf/message.rb b/lib/protobuf/message.rb index b5b723f5..8aba4cbc 100644 --- a/lib/protobuf/message.rb +++ b/lib/protobuf/message.rb @@ -1,6 +1,11 @@ require 'protobuf/message/fields' require 'protobuf/message/serialization' require 'protobuf/varint' +require 'protobuf/refinements/string/camelize' +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Camelize +using Protobuf::Refinements::String::Underscore module Protobuf class Message diff --git a/lib/protobuf/refinements/string/camelize.rb b/lib/protobuf/refinements/string/camelize.rb new file mode 100644 index 00000000..9415f5d7 --- /dev/null +++ b/lib/protobuf/refinements/string/camelize.rb @@ -0,0 +1,35 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/camelcase.rb + +module Protobuf + module Refinements + module String + module Camelize + refine ::String do + def camelize(*separators) + case separators.first + when Symbol, TrueClass, FalseClass, NilClass + first_letter = separators.shift + end + + separators = ['_', '\s'] if separators.empty? + + str = self.dup + + separators.each do |s| + str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase } + end + + case first_letter + when :upper, true + str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase } + when :lower, false + str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase } + end + + str + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/string/classify.rb b/lib/protobuf/refinements/string/classify.rb new file mode 100644 index 00000000..616efac6 --- /dev/null +++ b/lib/protobuf/refinements/string/classify.rb @@ -0,0 +1,19 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/inflector/methods.rb + +require 'protobuf/refinements/string/camelize' + +module Protobuf + module Refinements + module String + module Classify + refine ::String do + using Protobuf::Refinements::String::Camelize + + def classify + split('/').map { |c| c.camelize(:upper) }.join('::') + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/string/constantize.rb b/lib/protobuf/refinements/string/constantize.rb new file mode 100644 index 00000000..a9386184 --- /dev/null +++ b/lib/protobuf/refinements/string/constantize.rb @@ -0,0 +1,53 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/inflector/methods.rb + +module Protobuf + module Refinements + module String + module Constantize + refine ::String do + def constantize + names = split("::") + + # Trigger a built-in NameError exception including the ill-formed constant in the message. + Object.const_get(self) if names.empty? + + # Remove the first blank element in case of '::ClassName' notation. + names.shift if names.size > 1 && names.first.empty? + + names.inject(Object) do |constant, name| + if constant == Object + constant.const_get(name) + else + candidate = constant.const_get(name) + next candidate if constant.const_defined?(name, false) + next candidate unless Object.const_defined?(name) + + # Go down the ancestors to check if it is owned directly. The check + # stops when we reach Object or the end of ancestors tree. + constant = constant.ancestors.inject(constant) do |const, ancestor| + break const if ancestor == Object + break ancestor if ancestor.const_defined?(name, false) + const + end + + # owner is in Object, so raise + constant.const_get(name, false) + end + end + end + + def safe_constantize + constantize + rescue NameError => e + raise if e.name && !(to_s.split("::").include?(e.name.to_s) || + e.name.to_s == to_s) + rescue ArgumentError => e + raise unless /not missing constant #{const_regexp(self)}!$/.match?(e.message) + rescue LoadError => e + raise unless /Unable to autoload constant #{const_regexp(self)}/.match?(e.message) + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/string/underscore.rb b/lib/protobuf/refinements/string/underscore.rb new file mode 100644 index 00000000..25a48d88 --- /dev/null +++ b/lib/protobuf/refinements/string/underscore.rb @@ -0,0 +1,20 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb + +module Protobuf + module Refinements + module String + module Underscore + refine ::String do + def underscore(*separators) + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + tr('-', '_'). + gsub(/\s/, '_'). + gsub(/__+/, '_'). + downcase + end + end + end + end + end +end diff --git a/lib/protobuf/rpc/middleware/request_decoder.rb b/lib/protobuf/rpc/middleware/request_decoder.rb index c4efe823..5d8795ed 100644 --- a/lib/protobuf/rpc/middleware/request_decoder.rb +++ b/lib/protobuf/rpc/middleware/request_decoder.rb @@ -1,3 +1,9 @@ +require 'protobuf/refinements/string/constantize' +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Constantize +using Protobuf::Refinements::String::Underscore + module Protobuf module Rpc module Middleware diff --git a/spec/support/packed_field.rb b/spec/support/packed_field.rb index 9b18efee..aba61357 100644 --- a/spec/support/packed_field.rb +++ b/spec/support/packed_field.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Underscore + if defined?(RSpec) shared_examples_for :packable_field do |field_klass| From 5c1d57292d956997802fe520746506596a4c8838 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 20:05:25 +1100 Subject: [PATCH 09/19] [Remove activesupport] Replace 'active_support/core_ext/numeric/time' with simple Time.now maths --- spec/lib/protobuf/rpc/stat_spec.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spec/lib/protobuf/rpc/stat_spec.rb b/spec/lib/protobuf/rpc/stat_spec.rb index 9abd627f..e629914b 100644 --- a/spec/lib/protobuf/rpc/stat_spec.rb +++ b/spec/lib/protobuf/rpc/stat_spec.rb @@ -1,6 +1,5 @@ require 'spec_helper' require 'timecop' -require 'active_support/core_ext/numeric/time' RSpec.describe ::Protobuf::Rpc::Stat do @@ -24,14 +23,14 @@ describe 'server mode' do it 'describes a server response to a client' do - ::Timecop.freeze(10.minutes.ago) do + ::Timecop.freeze(Time.now - 10 * 60) do stats = ::Protobuf::Rpc::Stat.new(:SERVER) stats.client = 'myserver1' stats.dispatcher = double('dispatcher', :service => BarService.new(:find_bars)) stats.request_size = 43 stats.response_size = 1302 - ::Timecop.freeze(1.62.seconds.from_now) do + ::Timecop.freeze(Time.now + 1.62) do stats.stop expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/1302B - 1.62s - OK - #{::Time.now.iso8601}" end @@ -51,7 +50,7 @@ describe 'client mode' do it 'describes a client request to a server' do - ::Timecop.freeze(10.minutes.ago) do + ::Timecop.freeze(Time.now - 10 * 60) do stats = ::Protobuf::Rpc::Stat.new(:CLIENT) stats.server = ['30000', 'myserver1.myhost.com'] stats.service = 'Foo::BarService' @@ -59,7 +58,7 @@ stats.request_size = 37 stats.response_size = 12345 - ::Timecop.freeze(0.832.seconds.from_now) do + ::Timecop.freeze(Time.now + 0.832) do stats.stop expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - OK - #{::Time.now.iso8601}" end @@ -69,7 +68,7 @@ describe 'error log' do it 'resolves error to a string' do - ::Timecop.freeze(10.minutes.ago) do + ::Timecop.freeze(Time.now - 10 * 60) do stats = ::Protobuf::Rpc::Stat.new(:CLIENT) stats.server = ['30000', 'myserver1.myhost.com'] stats.service = 'Foo::BarService' @@ -78,7 +77,7 @@ stats.request_size = 37 stats.response_size = 12345 - ::Timecop.freeze(0.832.seconds.from_now) do + ::Timecop.freeze(Time.now + 0.832) do stats.stop expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - RPC_ERROR - #{::Time.now.iso8601}" end From b785a2fa5802515a87d48ec7bbb326b268564e58 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 21:45:42 +1100 Subject: [PATCH 10/19] [Remove activesupport] Fix Blank refinement for Enum (SimpleDelegator) --- lib/protobuf/refinements/kernel/blank.rb | 6 ++++++ spec/lib/protobuf/message_spec.rb | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/lib/protobuf/refinements/kernel/blank.rb b/lib/protobuf/refinements/kernel/blank.rb index 42dd9627..b39cedd3 100644 --- a/lib/protobuf/refinements/kernel/blank.rb +++ b/lib/protobuf/refinements/kernel/blank.rb @@ -56,6 +56,12 @@ def blank? false end end + + refine ::Delegator do + def present? + __getobj__.present? + end + end end end end diff --git a/spec/lib/protobuf/message_spec.rb b/spec/lib/protobuf/message_spec.rb index 6a3f6d9c..47fe9890 100644 --- a/spec/lib/protobuf/message_spec.rb +++ b/spec/lib/protobuf/message_spec.rb @@ -5,6 +5,10 @@ require PROTOS_PATH.join('resource.pb') require PROTOS_PATH.join('enum.pb') +require 'protobuf/refinements/kernel/blank' + +using Protobuf::Refinements::Kernel::Blank + RSpec.describe Protobuf::Message do describe '.decode' do From 8f0d5957855f67f3b74794d4b1f2948676313f2b Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 21:56:47 +1100 Subject: [PATCH 11/19] [Remove activesupport] Replace 'active_support/core_ext/hash/slice' with refinement --- lib/protobuf/field/base_field.rb | 4 ++- lib/protobuf/refinements/hash/slice.rb | 36 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/protobuf/refinements/hash/slice.rb diff --git a/lib/protobuf/field/base_field.rb b/lib/protobuf/field/base_field.rb index 6f2f4f7f..6341ae88 100644 --- a/lib/protobuf/field/base_field.rb +++ b/lib/protobuf/field/base_field.rb @@ -1,7 +1,9 @@ -require 'active_support/core_ext/hash/slice' require 'protobuf/field/field_array' require 'protobuf/field/field_hash' require 'protobuf/field/base_field_object_definitions' +require 'protobuf/refinements/hash/slice' + +using Protobuf::Refinements::Hash::Slice module Protobuf module Field diff --git a/lib/protobuf/refinements/hash/slice.rb b/lib/protobuf/refinements/hash/slice.rb new file mode 100644 index 00000000..52858012 --- /dev/null +++ b/lib/protobuf/refinements/hash/slice.rb @@ -0,0 +1,36 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/hash/slice.rb + +module Protobuf + module Refinements + module Hash + module Slice + refine ::Hash do + def slice(*keep_keys) + if block_given? + each do |k, v| + keep_keys << k if yield(k, v) + end + end + + keep_keys.each_with_object({}) do |key, hash| + hash[key] = fetch(key) if key?(key) + end + end + + def slice!(*keep_keys) + if block_given? + each do |k, v| + keep_keys << k if yield(k, v) + end + end + + rejected = keys - keep_keys + removed = {} + rejected.each{ |k| removed[k] = delete(k) } + removed + end + end + end + end + end +end From cfe46f91da9249f52de0405897d3a034ab31f8ce Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 22:00:01 +1100 Subject: [PATCH 12/19] [Remove activesupport] Reorder merge and remove 'active_support/core_ext/hash/reverse_merge' --- spec/support/server.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/support/server.rb b/spec/support/server.rb index 8a0203b6..9c5b7635 100644 --- a/spec/support/server.rb +++ b/spec/support/server.rb @@ -1,7 +1,5 @@ require 'ostruct' -require 'active_support/core_ext/hash/reverse_merge' - require 'spec_helper' require 'protobuf/logging' require 'protobuf/rpc/server' @@ -25,13 +23,13 @@ class StubServer def initialize(options = {}) self.options = OpenStruct.new( - options.reverse_merge( + { :host => '127.0.0.1', :port => 9399, :worker_port => 9400, :delay => 0, :server => Protobuf::Rpc::Socket::Server, - ), + }.merge(options), ) start From 014fa396eb0b7c42775ef7d295c2ed1cac30be71 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 22:17:21 +1100 Subject: [PATCH 13/19] [Remove activesupport] Expose current dependency on 'active_support/lazy_load_hooks' --- lib/protobuf.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/protobuf.rb b/lib/protobuf.rb index 878c7998..9e870b22 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -5,6 +5,7 @@ require 'stringio' require 'active_support/json' +require 'active_support/lazy_load_hooks' require 'active_support/notifications' require 'protobuf/refinements/string/classify' From 55df098c63784ee19a68ad9cd44832c89767f13b Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 22:19:26 +1100 Subject: [PATCH 14/19] [Remove activesupport] Switch to inbuilt json from 'active_support/json' --- lib/protobuf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/protobuf.rb b/lib/protobuf.rb index 9e870b22..41e2ef9f 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -1,10 +1,10 @@ require 'base64' +require 'json' require 'logger' require 'pp' require 'socket' require 'stringio' -require 'active_support/json' require 'active_support/lazy_load_hooks' require 'active_support/notifications' From 704f0d1b1fb907e0b40c5ab144b0bb7496eaded6 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 23:28:31 +1100 Subject: [PATCH 15/19] [Remove activesupport] Only use ActiveSupport::Notifications and ActiveSupport::Deprecation when present --- lib/protobuf.rb | 28 +++++++++++++++++-- lib/protobuf/cli.rb | 4 +-- lib/protobuf/deprecation.rb | 17 +++++++++-- lib/protobuf/lifecycle.rb | 22 +++++++++------ lib/protobuf/rpc/client.rb | 2 +- lib/protobuf/rpc/middleware.rb | 2 +- lib/protobuf/rpc/service.rb | 2 +- lib/protobuf/rpc/service_directory.rb | 1 + spec/lib/protobuf/cli_spec.rb | 1 + spec/lib/protobuf/lifecycle_spec.rb | 1 + .../protobuf/rpc/service_directory_spec.rb | 1 + 11 files changed, 63 insertions(+), 18 deletions(-) diff --git a/lib/protobuf.rb b/lib/protobuf.rb index 41e2ef9f..ccd22ccf 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -5,8 +5,16 @@ require 'socket' require 'stringio' -require 'active_support/lazy_load_hooks' -require 'active_support/notifications' +# rubocop:disable Lint/HandleExceptions +begin + require 'active_support/lazy_load_hooks' +rescue LoadError +end +begin + require 'active_support/notifications' +rescue LoadError +end +# rubocop:enable Lint/HandleExceptions require 'protobuf/refinements/string/classify' require 'protobuf/refinements/string/constantize' @@ -60,17 +68,33 @@ class << self end def self.after_server_bind(&block) + unless Object.const_defined?('::ActiveSupport::Notifications') + warn_no_active_support_notifications('after_server_bind') + return + end + ::ActiveSupport::Notifications.subscribe('after_server_bind') do |*args| block.call(*args) end end def self.before_server_bind(&block) + unless Object.const_defined?('::ActiveSupport::Notifications') + warn_no_active_support_notifications('before_server_bind') + return + end + ::ActiveSupport::Notifications.subscribe('before_server_bind') do |*args| block.call(*args) end end + def self.warn_no_active_support_notifications(method) + return if ENV['PROTOBUF_SUPPRESS_NO_ACTIVE_SUPPORT_NOTIFICATIONS_WARNING'] == '1' + + STDERR.puts("[WARN] ActiveSupport::Notifications is not loaded. To use `Protobuf.#{method}`, you need to require 'active_support/notifications'") + end + def self.client_host @client_host ||= Socket.gethostname end diff --git a/lib/protobuf/cli.rb b/lib/protobuf/cli.rb index 10803ae2..c1bb6c11 100644 --- a/lib/protobuf/cli.rb +++ b/lib/protobuf/cli.rb @@ -244,14 +244,14 @@ def shutdown_server def start_server debug_say('Running server') - ::ActiveSupport::Notifications.instrument("before_server_bind") + ::ActiveSupport::Notifications.instrument("before_server_bind") if Object.const_defined?('::ActiveSupport::Notifications') runner.run do logger.info do "pid #{::Process.pid} -- #{mode} RPC Server listening at #{options.host}:#{options.port}" end - ::ActiveSupport::Notifications.instrument("after_server_bind") + ::ActiveSupport::Notifications.instrument("after_server_bind") if Object.const_defined?('::ActiveSupport::Notifications') end logger.info { 'Shutdown complete' } diff --git a/lib/protobuf/deprecation.rb b/lib/protobuf/deprecation.rb index 8c0d415e..428db92c 100644 --- a/lib/protobuf/deprecation.rb +++ b/lib/protobuf/deprecation.rb @@ -1,7 +1,20 @@ -require 'active_support/deprecation' +begin + require 'active_support/deprecation' +rescue LoadError +end module Protobuf - if ::ActiveSupport::Deprecation.is_a?(Class) + if !Object.const_defined?('::ActiveSupport::Deprecation') + class NullDeprecator + def initialize(*); end + def define_deprecated_methods(*); end + def deprecate_methods(*); end + attr_accessor :silenced, :behavior + end + + Deprecation = NullDeprecator.clone + FieldDeprecation = NullDeprecator.clone + elsif ::ActiveSupport::Deprecation.is_a?(Class) class DeprecationBase < ::ActiveSupport::Deprecation def deprecate_methods(*args) deprecation_options = { :deprecator => self } diff --git a/lib/protobuf/lifecycle.rb b/lib/protobuf/lifecycle.rb index a17aac5c..96ee5501 100644 --- a/lib/protobuf/lifecycle.rb +++ b/lib/protobuf/lifecycle.rb @@ -5,8 +5,10 @@ def register(event_name) fail "Lifecycle register must have a block" unless block_given? event_name = normalized_event_name(event_name) - ::ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, args| - yield(*args) + if Object.const_defined?('::ActiveSupport::Notifications') + ::ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, args| + yield(*args) + end end end alias :on register @@ -14,16 +16,18 @@ def register(event_name) def trigger(event_name, *args) event_name = normalized_event_name(event_name) - ::ActiveSupport::Notifications.instrument(event_name, args) + ::ActiveSupport::Notifications.instrument(event_name, args) if Object.const_defined?('::ActiveSupport::Notifications') end - replacement = ::ActiveSupport::Notifications + if Object.const_defined?('::ActiveSupport::Notifications') + replacement = ::ActiveSupport::Notifications - ::Protobuf.deprecator.deprecate_methods( - self, - :register => "#{replacement}.#{replacement.method(:subscribe).name}".to_sym, - :trigger => "#{replacement}.#{replacement.method(:instrument).name}".to_sym, - ) + ::Protobuf.deprecator.deprecate_methods( + self, + :register => "#{replacement}.#{replacement.method(:subscribe).name}".to_sym, + :trigger => "#{replacement}.#{replacement.method(:instrument).name}".to_sym, + ) + end def normalized_event_name(event_name) event_name.to_s.downcase diff --git a/lib/protobuf/rpc/client.rb b/lib/protobuf/rpc/client.rb index 74791d23..3e6c1cb5 100644 --- a/lib/protobuf/rpc/client.rb +++ b/lib/protobuf/rpc/client.rb @@ -135,6 +135,6 @@ def method_missing(method_name, *params) end - ActiveSupport.run_load_hooks(:protobuf_rpc_client, Client) + ActiveSupport.run_load_hooks(:protobuf_rpc_client, Client) if Object.const_defined?('::ActiveSupport::LazyLoadHooks') end end diff --git a/lib/protobuf/rpc/middleware.rb b/lib/protobuf/rpc/middleware.rb index 3757b646..668f8830 100644 --- a/lib/protobuf/rpc/middleware.rb +++ b/lib/protobuf/rpc/middleware.rb @@ -21,5 +21,5 @@ def self.middleware Rpc.middleware.use(Rpc::Middleware::Logger) Rpc.middleware.use(Rpc::Middleware::ResponseEncoder) - ActiveSupport.run_load_hooks(:protobuf_rpc_middleware, Rpc) + ActiveSupport.run_load_hooks(:protobuf_rpc_middleware, Rpc) if Object.const_defined?('::ActiveSupport::LazyLoadHooks') end diff --git a/lib/protobuf/rpc/service.rb b/lib/protobuf/rpc/service.rb index cc1974ad..142537e1 100644 --- a/lib/protobuf/rpc/service.rb +++ b/lib/protobuf/rpc/service.rb @@ -167,6 +167,6 @@ def rpc_failed(message) end end - ActiveSupport.run_load_hooks(:protobuf_rpc_service, Service) + ActiveSupport.run_load_hooks(:protobuf_rpc_service, Service) if Object.const_defined?('::ActiveSupport::LazyLoadHooks') end end diff --git a/lib/protobuf/rpc/service_directory.rb b/lib/protobuf/rpc/service_directory.rb index 35f31d5c..af1e812f 100644 --- a/lib/protobuf/rpc/service_directory.rb +++ b/lib/protobuf/rpc/service_directory.rb @@ -257,6 +257,7 @@ def run end def trigger(action, listing) + return unless Object.const_defined?('::ActiveSupport::Notifications') ::ActiveSupport::Notifications.instrument("directory.listing.#{action}", :listing => listing) end end diff --git a/spec/lib/protobuf/cli_spec.rb b/spec/lib/protobuf/cli_spec.rb index ea78ed0d..911a8f98 100644 --- a/spec/lib/protobuf/cli_spec.rb +++ b/spec/lib/protobuf/cli_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'active_support/notifications' require 'protobuf/cli' RSpec.describe ::Protobuf::CLI do diff --git a/spec/lib/protobuf/lifecycle_spec.rb b/spec/lib/protobuf/lifecycle_spec.rb index d1414477..d48d3020 100644 --- a/spec/lib/protobuf/lifecycle_spec.rb +++ b/spec/lib/protobuf/lifecycle_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'active_support/notifications' require 'protobuf/lifecycle' RSpec.describe ::Protobuf::Lifecycle do diff --git a/spec/lib/protobuf/rpc/service_directory_spec.rb b/spec/lib/protobuf/rpc/service_directory_spec.rb index 268bb021..05cfefcb 100644 --- a/spec/lib/protobuf/rpc/service_directory_spec.rb +++ b/spec/lib/protobuf/rpc/service_directory_spec.rb @@ -1,5 +1,6 @@ require 'spec_helper' +require 'active_support/notifications' require 'protobuf/rpc/service_directory' RSpec.describe ::Protobuf::Rpc::ServiceDirectory do From cf9851e947a3a7707ef1d02dc03616ef99652352 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 23:28:59 +1100 Subject: [PATCH 16/19] [Remove activesupport] Demote activesupport to a development dependency --- protobuf-cucumber.gemspec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/protobuf-cucumber.gemspec b/protobuf-cucumber.gemspec index a38aa0cd..020a3fc0 100644 --- a/protobuf-cucumber.gemspec +++ b/protobuf-cucumber.gemspec @@ -21,7 +21,8 @@ require "protobuf/version" # Hack, as Rails 5 requires Ruby version >= 2.2.2. active_support_max_version = "< 5" if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.2.2") - s.add_dependency "activesupport", '>= 3.2', active_support_max_version + s.add_development_dependency "activesupport", '>= 3.2', active_support_max_version + s.add_dependency 'middleware' s.add_dependency 'thor' s.add_dependency 'thread_safe' From 488c1f5c8b2a9e63fd7c0b60eb0a1f1de7d8c9c0 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 23:36:43 +1100 Subject: [PATCH 17/19] [Remove activesupport] Fix Delegator refining failing when it's not been required --- lib/protobuf/refinements/kernel/blank.rb | 8 +++++--- lib/protobuf/refinements/object/try.rb | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/protobuf/refinements/kernel/blank.rb b/lib/protobuf/refinements/kernel/blank.rb index b39cedd3..e9dd480b 100644 --- a/lib/protobuf/refinements/kernel/blank.rb +++ b/lib/protobuf/refinements/kernel/blank.rb @@ -57,9 +57,11 @@ def blank? end end - refine ::Delegator do - def present? - __getobj__.present? + if Object.const_defined?('::Delegator') + refine ::Delegator do + def present? + __getobj__.present? + end end end end diff --git a/lib/protobuf/refinements/object/try.rb b/lib/protobuf/refinements/object/try.rb index f3e1a3d4..7074c54b 100644 --- a/lib/protobuf/refinements/object/try.rb +++ b/lib/protobuf/refinements/object/try.rb @@ -36,8 +36,10 @@ def try!(method_name = nil, *args, &b) include Tryable end - refine ::Delegator do - include Tryable + if Object.const_defined?('::Delegator') + refine ::Delegator do + include Tryable + end end refine ::NilClass do From a576f2f723914a65d293c89d224e24139466ed58 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 23:55:02 +1100 Subject: [PATCH 18/19] Make existing code satisfy RuboCop --- .rubocop.yml | 2 +- lib/protobuf/field/bytes_field.rb | 2 +- lib/protobuf/field/enum_field.rb | 2 +- lib/protobuf/field/string_field.rb | 2 +- lib/protobuf/message.rb | 12 +++++++++++- lib/protobuf/refinements/hash/slice.rb | 2 +- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 310aa0c8..9544655c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -44,7 +44,7 @@ Style/GuardClause: MinBodyLength: 4 Style/HashSyntax: - EnforcedStyle: hash_rockets + Enabled: False Style/IndentHash: EnforcedStyle: consistent diff --git a/lib/protobuf/field/bytes_field.rb b/lib/protobuf/field/bytes_field.rb index e120835c..eb687e89 100644 --- a/lib/protobuf/field/bytes_field.rb +++ b/lib/protobuf/field/bytes_field.rb @@ -70,7 +70,7 @@ def coerce!(value) end end - def json_encode(value, options={}) + def json_encode(value, _options = {}) Base64.strict_encode64(value) end end diff --git a/lib/protobuf/field/enum_field.rb b/lib/protobuf/field/enum_field.rb index 12867adf..add8bf06 100644 --- a/lib/protobuf/field/enum_field.rb +++ b/lib/protobuf/field/enum_field.rb @@ -37,7 +37,7 @@ def coerce!(value) type_class.fetch(value) || fail(TypeError, "Invalid Enum value: #{value.inspect} for #{name}") end - def json_encode(value, options={}) + def json_encode(value, _options = {}) enum = type_class.enums.find { |e| e.to_i == value } enum.to_s(:name) end diff --git a/lib/protobuf/field/string_field.rb b/lib/protobuf/field/string_field.rb index 551bdd23..08dc1136 100644 --- a/lib/protobuf/field/string_field.rb +++ b/lib/protobuf/field/string_field.rb @@ -43,7 +43,7 @@ def encode(value) "#{::Protobuf::Field::VarintField.encode(value_to_encode.bytesize)}#{value_to_encode}" end - def json_encode(value, options={}) + def json_encode(value, _options = {}) value end end diff --git a/lib/protobuf/message.rb b/lib/protobuf/message.rb index 8aba4cbc..80eda29a 100644 --- a/lib/protobuf/message.rb +++ b/lib/protobuf/message.rb @@ -179,7 +179,17 @@ def to_json_hash(options = {}) value end - if proto3 && (hashed_value.nil? || value == field.class.default rescue field.default rescue nil) + if proto3 && ( + begin + begin + hashed_value.nil? || value == field.class.default + rescue + field.default + end + rescue + nil + end + ) result.delete(field.name) else key = proto3 ? field.name.to_s.camelize(:lower).to_sym : field.name diff --git a/lib/protobuf/refinements/hash/slice.rb b/lib/protobuf/refinements/hash/slice.rb index 52858012..95efdb85 100644 --- a/lib/protobuf/refinements/hash/slice.rb +++ b/lib/protobuf/refinements/hash/slice.rb @@ -26,7 +26,7 @@ def slice!(*keep_keys) rejected = keys - keep_keys removed = {} - rejected.each{ |k| removed[k] = delete(k) } + rejected.each { |k| removed[k] = delete(k) } removed end end From 3f71a77cc2eb6fb64b5275477637c2951d6ce8ec Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Wed, 3 Feb 2021 23:55:20 +1100 Subject: [PATCH 19/19] [Remove activesupport] Satisfy RuboCop --- lib/protobuf/deprecation.rb | 2 +- lib/protobuf/refinements/hash/symbolize_keys.rb | 8 +++++++- lib/protobuf/refinements/object/try.rb | 4 ++-- lib/protobuf/refinements/string/camelize.rb | 8 ++++---- lib/protobuf/refinements/string/constantize.rb | 2 +- lib/protobuf/refinements/string/underscore.rb | 14 +++++++------- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/protobuf/deprecation.rb b/lib/protobuf/deprecation.rb index 428db92c..d1bc1328 100644 --- a/lib/protobuf/deprecation.rb +++ b/lib/protobuf/deprecation.rb @@ -1,6 +1,6 @@ begin require 'active_support/deprecation' -rescue LoadError +rescue LoadError # rubocop:disable Lint/HandleExceptions end module Protobuf diff --git a/lib/protobuf/refinements/hash/symbolize_keys.rb b/lib/protobuf/refinements/hash/symbolize_keys.rb index a17f817f..205003d0 100644 --- a/lib/protobuf/refinements/hash/symbolize_keys.rb +++ b/lib/protobuf/refinements/hash/symbolize_keys.rb @@ -6,7 +6,13 @@ module Hash module SymbolizeKeys refine ::Hash do def symbolize_keys - transform_keys { |key| key.to_sym rescue key } + transform_keys do |key| + begin + key.to_sym + rescue + key + end + end end end end diff --git a/lib/protobuf/refinements/object/try.rb b/lib/protobuf/refinements/object/try.rb index 7074c54b..e2e6bba8 100644 --- a/lib/protobuf/refinements/object/try.rb +++ b/lib/protobuf/refinements/object/try.rb @@ -43,11 +43,11 @@ def try!(method_name = nil, *args, &b) end refine ::NilClass do - def try(method_name = nil, *args) + def try(_method_name = nil, *_args) nil end - def try!(method_name = nil, *args) + def try!(_method_name = nil, *_args) nil end end diff --git a/lib/protobuf/refinements/string/camelize.rb b/lib/protobuf/refinements/string/camelize.rb index 9415f5d7..7505974d 100644 --- a/lib/protobuf/refinements/string/camelize.rb +++ b/lib/protobuf/refinements/string/camelize.rb @@ -13,17 +13,17 @@ def camelize(*separators) separators = ['_', '\s'] if separators.empty? - str = self.dup + str = dup separators.each do |s| - str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase } + str = str.gsub(/(?:#{s}+)([a-z])/) { $1.upcase } # rubocop:disable Style/PerlBackrefs end case first_letter when :upper, true - str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase } + str = str.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase } # rubocop:disable Style/PerlBackrefs when :lower, false - str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase } + str = str.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase } # rubocop:disable Style/PerlBackrefs end str diff --git a/lib/protobuf/refinements/string/constantize.rb b/lib/protobuf/refinements/string/constantize.rb index a9386184..c0d75f18 100644 --- a/lib/protobuf/refinements/string/constantize.rb +++ b/lib/protobuf/refinements/string/constantize.rb @@ -24,7 +24,7 @@ def constantize # Go down the ancestors to check if it is owned directly. The check # stops when we reach Object or the end of ancestors tree. - constant = constant.ancestors.inject(constant) do |const, ancestor| + constant = constant.ancestors.inject(constant) do |const, ancestor| # rubocop:disable Style/EachWithObject break const if ancestor == Object break ancestor if ancestor.const_defined?(name, false) const diff --git a/lib/protobuf/refinements/string/underscore.rb b/lib/protobuf/refinements/string/underscore.rb index 25a48d88..62a88620 100644 --- a/lib/protobuf/refinements/string/underscore.rb +++ b/lib/protobuf/refinements/string/underscore.rb @@ -5,13 +5,13 @@ module Refinements module String module Underscore refine ::String do - def underscore(*separators) - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). - gsub(/([a-z\d])([A-Z])/,'\1_\2'). - tr('-', '_'). - gsub(/\s/, '_'). - gsub(/__+/, '_'). - downcase + def underscore(*_separators) + gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') + .gsub(/([a-z\d])([A-Z])/, '\1_\2') + .tr('-', '_') + .gsub(/\s/, '_') + .gsub(/__+/, '_') + .downcase end end end