Skip to content

Commit 1ebc7f9

Browse files
committed
GeneratorHelper -> GoUtil
1 parent 6eb2090 commit 1ebc7f9

File tree

8 files changed

+30
-42
lines changed

8 files changed

+30
-42
lines changed

_tools/ruby_h_to_go/lib/ruby_h_to_go.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require_relative "ruby_header_parser"
66

7-
require_relative "ruby_h_to_go/generator_helper"
7+
require_relative "ruby_h_to_go/go_util"
88
require_relative "ruby_h_to_go/argument_definition"
99
require_relative "ruby_h_to_go/cli"
1010
require_relative "ruby_h_to_go/enum_definition"

_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class ArgumentDefinition
77

88
def_delegators :@definition, :==, :type, :type=, :name, :name=, :pointer, :pointer=, :pointer?, :length, :length=
99

10-
include GeneratorHelper
11-
1210
# @param definition [RubyHeaderParser::ArgumentDefinition]
1311
# @param header_dir [String]
1412
def initialize(definition:)
@@ -35,25 +33,25 @@ def go_name
3533

3634
# @return [String]
3735
def go_function_arg
38-
"#{go_name} #{ruby_c_type_to_go_type(type, pointer:, pointer_length: length, pos: :arg)}"
36+
"#{go_name} #{GoUtil.ruby_c_type_to_go_type(type, pointer:, pointer_length: length, pos: :arg)}"
3937
end
4038

4139
# @return [String]
4240
def cast_to_cgo
4341
case pointer
4442
when :array
45-
return "toCArray[#{ruby_c_type_to_go_type(type)}, #{cast_to_cgo_type(type)}](#{go_name})"
43+
return "toCArray[#{GoUtil.ruby_c_type_to_go_type(type)}, #{GoUtil.cast_to_cgo_type(type)}](#{go_name})"
4644
when :ref_array
47-
return "toCArray[*#{ruby_c_type_to_go_type(type)}, *#{cast_to_cgo_type(type)}](#{go_name})"
45+
return "toCArray[*#{GoUtil.ruby_c_type_to_go_type(type)}, *#{GoUtil.cast_to_cgo_type(type)}](#{go_name})"
4846
when :sref
4947
return go_name if type == "void" && length == 2
5048

51-
return "(#{"*" * length}#{cast_to_cgo_type(type)})(unsafe.Pointer(#{go_name}))"
49+
return "(#{"*" * length}#{GoUtil.cast_to_cgo_type(type)})(unsafe.Pointer(#{go_name}))"
5250
when :in_ref
53-
return "(*#{cast_to_cgo_type(type)})(#{go_name})"
51+
return "(*#{GoUtil.cast_to_cgo_type(type)})(#{go_name})"
5452
end
5553

56-
"#{cast_to_cgo_type(type)}(#{go_name})"
54+
"#{GoUtil.cast_to_cgo_type(type)}(#{go_name})"
5755
end
5856

5957
# @param char_var_count [Integer]
@@ -75,10 +73,10 @@ def generate_go_arguments(char_var_count:, chars_var_count:)
7573
[go_name, [], []]
7674

7775
else
78-
c_var_name = "c#{snake_to_camel(go_name)}"
76+
c_var_name = "c#{GoUtil.snake_to_camel(go_name)}"
7977

80-
before_call_function_line = "var #{c_var_name} #{cast_to_cgo_type(type)}"
81-
after_call_function_line = "*#{go_name} = #{ruby_c_type_to_go_type(type, pos: :arg)}(#{c_var_name})"
78+
before_call_function_line = "var #{c_var_name} #{GoUtil.cast_to_cgo_type(type)}"
79+
after_call_function_line = "*#{go_name} = #{GoUtil.ruby_c_type_to_go_type(type, pos: :arg)}(#{c_var_name})"
8280

8381
["&#{c_var_name}", [before_call_function_line], [after_call_function_line]]
8482
end
@@ -105,7 +103,7 @@ def generate_go_arguments(char_var_count:, chars_var_count:)
105103
def generate_go_arguments_for_char_pointer(char_var_count)
106104
# self is string
107105
if char_var_count >= 2
108-
chars_var_name = "char#{snake_to_camel(go_name)}"
106+
chars_var_name = "char#{GoUtil.snake_to_camel(go_name)}"
109107
clean_var_name = "cleanChar#{go_name}"
110108
else
111109
chars_var_name = "char"
@@ -129,7 +127,7 @@ def generate_go_arguments_for_char_pointer(char_var_count)
129127
# - after_call_function_lines [Array<String>]
130128
def generate_go_arguments_for_str_array(chars_var_count)
131129
if chars_var_count >= 2
132-
chars_var_name = "chars#{snake_to_camel(go_name)}"
130+
chars_var_name = "chars#{GoUtil.snake_to_camel(go_name)}"
133131
clean_var_name = "cleanChars#{go_name}"
134132
else
135133
chars_var_name = "chars"

_tools/ruby_h_to_go/lib/ruby_h_to_go/enum_definition.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class EnumDefinition
77

88
def_delegators :@definition, :==, :name, :name=, :values, :values=
99

10-
include GeneratorHelper
11-
1210
# @param definition [RubyHeaderParser::EnumDefinition]
1311
def initialize(definition:)
1412
@definition = definition
@@ -19,7 +17,7 @@ def initialize(definition:)
1917
def write_go_file(dist_dir)
2018
go_file_path = File.join(dist_dir, "enum_generated.go")
2119

22-
generate_initial_go_file(go_file_path)
20+
GoUtil.generate_initial_go_file(go_file_path)
2321

2422
File.open(go_file_path, "a") do |f|
2523
f.write(generate_go_content)
@@ -28,7 +26,7 @@ def write_go_file(dist_dir)
2826

2927
# @return [String]
3028
def generate_go_content
31-
go_type_name = snake_to_camel(name)
29+
go_type_name = GoUtil.snake_to_camel(name)
3230

3331
go_enum_lines = [
3432
"// #{go_type_name} is a type for passing `C.#{name}` in and out of package",

_tools/ruby_h_to_go/lib/ruby_h_to_go/function_definition.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class FunctionDefinition
77

88
def_delegators :@definition, :==, :name, :name=, :definition, :definition=
99

10-
include GeneratorHelper
11-
1210
# @param definition [RubyHeaderParser::FunctionDefinition]
1311
def initialize(definition:)
1412
@definition = definition
@@ -29,7 +27,7 @@ def args
2927
def write_go_file(dist_dir)
3028
go_file_path = File.join(dist_dir, "function_generated.go")
3129

32-
generate_initial_go_file(go_file_path)
30+
GoUtil.generate_initial_go_file(go_file_path)
3331

3432
File.open(go_file_path, "a") do |f|
3533
f.write(generate_go_content)
@@ -93,7 +91,7 @@ def generate_go_content
9391
def go_function_name
9492
return name if name.match?(/^[A-Z0-9_]+$/)
9593

96-
snake_to_camel(name)
94+
GoUtil.snake_to_camel(name)
9795
end
9896
end
9997
end

_tools/ruby_h_to_go/lib/ruby_h_to_go/generator_helper.rb renamed to _tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
module RubyHToGo
44
# helper methods for generating go code
5-
module GeneratorHelper
5+
module GoUtil
66
# @param str [String]
77
# @return [String]
8-
def snake_to_camel(str)
8+
def self.snake_to_camel(str)
99
return str if %w[VALUE ID].include?(str)
1010

1111
str.split("_").map(&:capitalize).join.gsub(/(?<=\d)([a-z])/) { _1.upcase } # rubocop:disable Style/SymbolProc
1212
end
1313

1414
# Generate initial go file whether not exists
1515
# @param go_file_path [String]
16-
def generate_initial_go_file(go_file_path)
16+
def self.generate_initial_go_file(go_file_path)
1717
return if File.exist?(go_file_path)
1818

1919
File.binwrite(go_file_path, <<~GO)
@@ -53,7 +53,7 @@ def generate_initial_go_file(go_file_path)
5353
# @param pointer [Symbol,nil] pointer hint (:ref, :array, :ref_array, :function, :sref, :str_array, :in_ref, :raw)
5454
# @param pointer_length [Integer]
5555
# @return [String]
56-
def ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
56+
def self.ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
5757
return ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:) if pointer
5858

5959
return C_TYPE_TO_GO_TYPE[typename] if C_TYPE_TO_GO_TYPE[typename]
@@ -87,21 +87,19 @@ def ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
8787
# Cast C type to cgo type. (Used in wrapper function)
8888
# @param typename [String]
8989
# @return [String]
90-
def cast_to_cgo_type(typename)
90+
def self.cast_to_cgo_type(typename)
9191
return C_TYPE_TO_CGO_TYPE[typename] if C_TYPE_TO_CGO_TYPE[typename]
9292

9393
"C.#{typename}"
9494
end
9595

96-
private
97-
9896
# Convert pointer C type to Go type. (used in wrapper function args and return type etc)
9997
# @param typename [String]
10098
# @param pos [Symbol,nil] :arg, :typeref, :return
10199
# @param pointer [Symbol,nil] pointer hint (:ref, :array, :ref_array, :function, :sref, :str_array, :in_ref, :raw)
102100
# @param pointer_length [Integer]
103101
# @return [String]
104-
def ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:)
102+
def self.ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:)
105103
go_type_name =
106104
if typename == "int" && %i[return typeref].include?(pos)
107105
"Int"
@@ -139,5 +137,7 @@ def ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:)
139137

140138
"*#{go_type_name}"
141139
end
140+
141+
private_class_method :ruby_pointer_c_type_to_go_type
142142
end
143143
end

_tools/ruby_h_to_go/lib/ruby_h_to_go/struct_definition.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class StructDefinition
77

88
def_delegators :@definition, :==, :name, :name=
99

10-
include GeneratorHelper
11-
1210
# @param definition [RubyHeaderParser::StructDefinition]
1311
def initialize(definition:)
1412
@definition = definition
@@ -19,7 +17,7 @@ def initialize(definition:)
1917
def write_go_file(dist_dir)
2018
go_file_path = File.join(dist_dir, "struct_generated.go")
2119

22-
generate_initial_go_file(go_file_path)
20+
GoUtil.generate_initial_go_file(go_file_path)
2321

2422
File.open(go_file_path, "a") do |f|
2523
f.write(generate_go_content)
@@ -28,7 +26,7 @@ def write_go_file(dist_dir)
2826

2927
# @return [String]
3028
def generate_go_content
31-
go_type_name = snake_to_camel(name)
29+
go_type_name = GoUtil.snake_to_camel(name)
3230

3331
<<~GO
3432
// #{go_type_name} is a type for passing `C.#{name}` in and out of package

_tools/ruby_h_to_go/lib/ruby_h_to_go/type_definition.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class TypeDefinition
77

88
def_delegators :@definition, :==, :name, :name=
99

10-
include GeneratorHelper
11-
1210
# @param definition [RubyHeaderParser::TypeDefinition]
1311
def initialize(definition:)
1412
@definition = definition
@@ -19,7 +17,7 @@ def initialize(definition:)
1917
def write_go_file(dist_dir)
2018
go_file_path = File.join(dist_dir, "type_generated.go")
2119

22-
generate_initial_go_file(go_file_path)
20+
GoUtil.generate_initial_go_file(go_file_path)
2321

2422
File.open(go_file_path, "a") do |f|
2523
f.write(generate_go_content)
@@ -28,7 +26,7 @@ def write_go_file(dist_dir)
2826

2927
# @return [String]
3028
def generate_go_content
31-
go_type_name = snake_to_camel(name)
29+
go_type_name = GoUtil.snake_to_camel(name)
3230

3331
<<~GO
3432
// #{go_type_name} is a type for passing `C.#{name}` in and out of package

_tools/ruby_h_to_go/lib/ruby_h_to_go/typeref_definition.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class TyperefDefinition
77

88
def_delegators :@definition, :==, :type, :type=, :pointer, :pointer=, :pointer?
99

10-
include GeneratorHelper
11-
1210
# @param definition [RubyHeaderParser::TyperefDefinition]
1311
def initialize(definition:)
1412
@definition = definition
@@ -18,14 +16,14 @@ def initialize(definition:)
1816
def go_function_typeref
1917
return "" if type == "void" && !pointer?
2018

21-
ruby_c_type_to_go_type(type, pos: :typeref, pointer:)
19+
GoUtil.ruby_c_type_to_go_type(type, pos: :typeref, pointer:)
2220
end
2321

2422
# @return [String]
2523
def cast_func_for_function_return
2624
return "" if type == "void" && !pointer?
2725

28-
cast_func = ruby_c_type_to_go_type(type, pos: :return, pointer:)
26+
cast_func = GoUtil.ruby_c_type_to_go_type(type, pos: :return, pointer:)
2927
return "(#{cast_func})" if cast_func.start_with?("*")
3028

3129
cast_func

0 commit comments

Comments
 (0)