Skip to content

Commit 4594e3f

Browse files
authored
Merge pull request #158 from sue445/feature/remove_ModuleLength
Remove Metrics/ModuleLength in .rubocop_todo.yml
2 parents 9f44025 + 6059b16 commit 4594e3f

File tree

2 files changed

+46
-63
lines changed

2 files changed

+46
-63
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ Metrics/CyclomaticComplexity:
3939
Metrics/MethodLength:
4040
Max: 75
4141

42-
# Offense count: 1
43-
# Configuration parameters: CountComments, CountAsOne.
44-
Metrics/ModuleLength:
45-
Max: 108
46-
4742
# Offense count: 3
4843
# Configuration parameters: AllowedMethods, AllowedPatterns.
4944
Metrics/PerceivedComplexity:

_tools/ruby_h_to_go/lib/ruby_h_to_go/generator_helper.rb

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ def generate_initial_go_file(go_file_path)
3636
GO
3737
end
3838

39+
C_TYPE_TO_GO_TYPE = {
40+
"RUBY_DATA_FUNC" => "unsafe.Pointer",
41+
"long long" => "Longlong",
42+
"rb_alloc_func_t" => "unsafe.Pointer",
43+
"unsigned char" => "Uchar",
44+
"unsigned int" => "uint",
45+
"unsigned long" => "uint",
46+
"unsigned long long" => "Ulonglong",
47+
"unsigned short" => "Ushort",
48+
}.freeze
49+
3950
# Convert C type to Go type. (used in wrapper function args and return type etc)
4051
# @param typename [String]
4152
# @param pos [Symbol,nil] :arg, :typeref, :return
@@ -45,19 +56,9 @@ def generate_initial_go_file(go_file_path)
4556
def ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
4657
return ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:) if pointer
4758

59+
return C_TYPE_TO_GO_TYPE[typename] if C_TYPE_TO_GO_TYPE[typename]
60+
4861
case typename
49-
when "unsigned int", "unsigned long"
50-
return "uint"
51-
when "unsigned short"
52-
return "Ushort"
53-
when "unsigned char"
54-
return "Uchar"
55-
when "long long"
56-
return "Longlong"
57-
when "unsigned long long"
58-
return "Ulonglong"
59-
when "RUBY_DATA_FUNC", "rb_alloc_func_t"
60-
return "unsafe.Pointer"
6162
when /^[A-Z]+$/, "int"
6263
# e.g. VALUE
6364
return typename
@@ -68,36 +69,26 @@ def ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
6869
snake_to_camel(typename)
6970
end
7071

72+
C_TYPE_TO_CGO_TYPE = {
73+
"RUBY_DATA_FUNC" => "toCFunctionPointer",
74+
"long long" => "C.longlong",
75+
"rb_io_wait_readwrite" => "C.enum_rb_io_wait_readwrite",
76+
"ruby_value_type" => "C.enum_ruby_value_type",
77+
"unsigned char" => "C.uchar",
78+
"unsigned int" => "C.uint",
79+
"unsigned long" => "C.ulong",
80+
"unsigned long long" => "C.ulonglong",
81+
"unsigned short" => "C.ushort",
82+
"st_hash_type" => "C.struct_st_hash_type",
83+
"timespec" => "C.struct_timespec",
84+
"timeval" => "C.struct_timeval",
85+
}.freeze
86+
7187
# Cast C type to cgo type. (Used in wrapper function)
7288
# @param typename [String]
7389
# @return [String]
7490
def cast_to_cgo_type(typename)
75-
case typename
76-
when "unsigned long"
77-
return "C.ulong"
78-
when "unsigned int"
79-
return "C.uint"
80-
when "unsigned char"
81-
return "C.uchar"
82-
when "unsigned short"
83-
return "C.ushort"
84-
when "long long"
85-
return "C.longlong"
86-
when "unsigned long long"
87-
return "C.ulonglong"
88-
when "timeval"
89-
return "C.struct_timeval"
90-
when "timespec"
91-
return "C.struct_timespec"
92-
when "st_hash_type"
93-
return "C.struct_st_hash_type"
94-
when "ruby_value_type"
95-
return "C.enum_ruby_value_type"
96-
when "rb_io_wait_readwrite"
97-
return "C.enum_rb_io_wait_readwrite"
98-
when "RUBY_DATA_FUNC"
99-
return "toCFunctionPointer"
100-
end
91+
return C_TYPE_TO_CGO_TYPE[typename] if C_TYPE_TO_CGO_TYPE[typename]
10192

10293
"C.#{typename}"
10394
end
@@ -111,43 +102,40 @@ def cast_to_cgo_type(typename)
111102
# @param pointer_length [Integer]
112103
# @return [String]
113104
def ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:)
105+
go_type_name =
106+
if typename == "int" && %i[return typeref].include?(pos)
107+
"Int"
108+
else
109+
ruby_c_type_to_go_type(typename, pos:, pointer: nil)
110+
end
111+
114112
case pointer
115113
when :sref
116114
return "*unsafe.Pointer" if typename == "void" && pointer_length == 2
117115

118-
go_type_name = ruby_c_type_to_go_type(typename, pos:, pointer: nil)
119116
return "#{"*" * pointer_length}#{go_type_name}"
117+
120118
when :str_array
121119
return "[]string"
122-
end
123120

124-
case typename
125-
when "char", "const char"
126-
if pointer == :ref
121+
when :array
122+
return "[]#{go_type_name}"
123+
124+
when :ref_array
125+
return "[]*#{go_type_name}"
126+
127+
when :ref
128+
if typename == "char"
127129
case pos
128130
when :arg, :typeref
129131
return "string"
130132
else
131133
return "char2String"
132134
end
133135
end
134-
when "void"
135-
return "unsafe.Pointer"
136136
end
137137

138-
go_type_name =
139-
if typename == "int" && %i[return typeref].include?(pos)
140-
"Int"
141-
else
142-
ruby_c_type_to_go_type(typename, pos:, pointer: nil)
143-
end
144-
145-
case pointer
146-
when :array
147-
return "[]#{go_type_name}"
148-
when :ref_array
149-
return "[]*#{go_type_name}"
150-
end
138+
return "unsafe.Pointer" if typename == "void"
151139

152140
"*#{go_type_name}"
153141
end

0 commit comments

Comments
 (0)