Skip to content

Commit 9f20834

Browse files
committed
[rb] add mutable and immutable record objects to manage serialization
1 parent 7e1e0a8 commit 9f20834

File tree

9 files changed

+126
-15
lines changed

9 files changed

+126
-15
lines changed

rb/lib/selenium/webdriver/bidi.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class BiDi
2424
autoload :LogHandler, 'selenium/webdriver/bidi/log_handler'
2525
autoload :Browser, 'selenium/webdriver/bidi/browser'
2626
autoload :BrowsingContext, 'selenium/webdriver/bidi/browsing_context'
27-
autoload :Struct, 'selenium/webdriver/bidi/struct'
2827
autoload :Network, 'selenium/webdriver/bidi/network'
2928
autoload :InterceptedRequest, 'selenium/webdriver/bidi/network/intercepted_request'
3029
autoload :InterceptedResponse, 'selenium/webdriver/bidi/network/intercepted_response'

rb/lib/selenium/webdriver/bidi/log_handler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module Selenium
2121
module WebDriver
2222
class BiDi
2323
class LogHandler
24-
ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)
25-
JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)
24+
ConsoleLogEntry = WebDriver::Types::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)
25+
JavaScriptLogEntry = WebDriver::Types::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)
2626

2727
def initialize(bidi)
2828
@bidi = bidi
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class Types
23+
autoload :Data, 'selenium/webdriver/common/types/data'
24+
autoload :Struct, 'selenium/webdriver/common/types/struct'
25+
26+
def self.camel_to_snake(s)
27+
s = s.to_s
28+
s.gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
29+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
30+
.tr('-', '_')
31+
.downcase
32+
.sub(/\A_/, '')
33+
end
34+
35+
def self.normalize_args(args, opts)
36+
unless args.empty? || (args.length == 1 && args.first.is_a?(Hash))
37+
raise ArgumentError, 'positional args not allowed; use keywords or a single hash'
38+
end
39+
40+
raw = opts.any? ? opts : (args.first || {})
41+
raw.transform_keys { |k| camel_to_snake(k.to_s).to_sym }
42+
end
43+
end
44+
end # WebDriver
45+
end # Selenium
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
module Types
23+
class Data < ::Data
24+
def self.define(*members, &blk)
25+
klass = super(*members.map(&:to_sym), &blk)
26+
27+
klass.singleton_class.prepend(Module.new {
28+
def new(*args, **opts)
29+
norm = WebDriver::Types.normalize_args(args, opts)
30+
super(*members.map { |m| norm[m] })
31+
end
32+
})
33+
34+
klass
35+
end
36+
end
37+
end # Types
38+
end # WebDriver
39+
end # Selenium

rb/lib/selenium/webdriver/bidi/struct.rb renamed to rb/lib/selenium/webdriver/common/types/struct.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@
1919

2020
module Selenium
2121
module WebDriver
22-
class BiDi
22+
module Types
2323
class Struct < ::Struct
2424
class << self
25-
def new(*args, &block)
26-
super do
27-
define_method(:initialize) do |**kwargs|
28-
converted_kwargs = kwargs.transform_keys { |key| self.class.camel_to_snake(key.to_s).to_sym }
29-
super(*converted_kwargs.values_at(*self.class.members))
25+
def define(*members, &blk)
26+
klass = super(*members.map(&:to_sym), keyword_init: true, &blk)
27+
28+
klass.singleton_class.prepend(Module.new {
29+
def new(*args, **opts)
30+
norm = WebDriver::Types.normalize_args(args, opts)
31+
super(**members.to_h { |m| [m, norm[m]] })
3032
end
31-
class_eval(&block) if block
32-
end
33-
end
33+
})
3434

35-
def camel_to_snake(camel_str)
36-
camel_str.gsub(/([A-Z])/, '_\1').downcase
35+
klass
3736
end
37+
38+
alias new define
3839
end
3940
end
40-
end # BiDi
41+
end # Types
4142
end # WebDriver
4243
end # Selenium
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Selenium
2+
module WebDriver
3+
module Types
4+
def self.camel_to_snake: (String s) -> String
5+
6+
def self.normalize_args: (Array[untyped] args, Hash[untyped, untyped] opts) -> Hash[Symbol, untyped]
7+
end
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Selenium
2+
module WebDriver
3+
module Types
4+
class Data < ::Data
5+
def self.define: (*untyped members, ?blk { () -> void }) -> Class
6+
end
7+
end
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Selenium
2+
module WebDriver
3+
module Types
4+
class Struct < ::Struct
5+
def self.define: (*untyped members, ?keyword_init: bool, ?blk { () -> void }) -> Class
6+
end
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)