Skip to content

Commit 685171b

Browse files
committed
[API] Code Generator: remove namespace depth in templates, RuboCop deals with that
1 parent a76da99 commit 685171b

File tree

7 files changed

+34
-36
lines changed

7 files changed

+34
-36
lines changed

elasticsearch-api/utils/thor/endpoint_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def initialize(filepath)
3232

3333
full_namespace = parse_full_namespace
3434
@namespace_depth = full_namespace.size > 0 ? full_namespace.size - 1 : 0
35-
@module_namespace = full_namespace[0, namespace_depth]
35+
@module_namespace = full_namespace[0, @namespace_depth]
3636
@method_name = full_namespace.last
3737

3838
@path_parts = parse_endpoint_parts(@spec)
@@ -56,7 +56,6 @@ def initialize(filepath)
5656
:http_path,
5757
:required_parts,
5858
:http_method,
59-
:namespace_depth,
6059
:path_params,
6160
:perform_request_opts
6261

elasticsearch-api/utils/thor/endpoint_specifics.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ def msearch_template_body_helper
136136

137137
def bulk_body_helper
138138
<<~SRC
139-
if body.is_a? Array
140-
payload = Elasticsearch::API::Utils.__bulkify(body)
141-
else
142-
payload = body
143-
end
139+
payload = if body.is_a? Array
140+
Elasticsearch::API::Utils.__bulkify(body)
141+
else
142+
body
143+
end
144144
SRC
145145
end
146146

elasticsearch-api/utils/thor/templates/_body.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ when 'mtermvectors'
3030
<%- when 'ml.find_file_structure' %>
3131
body = Utils.__bulkify(arguments.delete(:body))
3232
<%- else -%>
33-
<%= ' '*(@spec.namespace_depth+3) %>body = <%= @spec.body.nil? ? 'nil' : 'arguments.delete(:body)' %>
33+
body = <%= @spec.body.nil? ? 'nil' : 'arguments.delete(:body)' %>
3434
<%- end -%>

elasticsearch-api/utils/thor/templates/_documentation_top.erb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
# under the License.
1818
%>
1919
<%- if @spec.documentation['description'] -%>
20-
<%= ' '*(@spec.namespace_depth+3) %># <%= @spec.documentation['description'] .gsub("\n", "\n#{' '*(@spec.namespace_depth+3)}# ") -%>
20+
# <%= @spec.documentation['description'].gsub("\n", "\n # ") -%>
2121
<%- else %>
22-
<%= ' '*(@spec.namespace_depth+3) %># TODO: Description
22+
# TODO: Description
2323
<%- end %>
2424
<%= stability_doc_helper(@spec.stability) -%>
25-
<%= ' '*(@spec.namespace_depth+3) %>#
25+
#
2626
<%- unless @spec.path_parts.nil? || @spec.path_parts.empty? %><%# URL parts -%>
2727
<%- @spec.path_parts.each do |name, info| -%>
2828
<%= docs(name, info) -%>
@@ -37,10 +37,9 @@
3737
<%- if @spec.method_name == 'bulk' -%>
3838
<%= bulk_doc_helper(@spec.body['description']) -%>
3939
<%- else -%>
40-
<%= ' '*(@spec.namespace_depth+1) +
41-
'# @option arguments [Hash] :body ' + (@spec.body['description'] ?
42-
@spec.body['description'].strip : 'TODO: Description') +
43-
(@spec.body['required'] ? ' (*Required*)' : '') + "\n"
40+
<%= '# @option arguments [Hash] :body ' + (@spec.body['description'] ?
41+
@spec.body['description'].strip : 'TODO: Description') +
42+
(@spec.body['required'] ? ' (*Required*)' : '') + "\n"
4443
-%>
4544
<%- end -%>
4645
<%- end -%>
@@ -51,7 +50,7 @@
5150
# Deprecated since version <%= @spec.deprecation_note['version'] %>
5251
#
5352
<% end -%>
54-
<%= ' '*(@spec.namespace_depth+3) -%>#
53+
#
5554
<%# Documentation link -%>
56-
<%= ' '*(@spec.namespace_depth+3) %># @see <%= @spec.documentation['url'] ? Elasticsearch::API::FilesHelper.documentation_url(@spec.documentation['url']) : "[TODO]" %>
57-
<%= ' '*(@spec.namespace_depth+3) %>#
55+
# @see <%= @spec.documentation['url'] ? Elasticsearch::API::FilesHelper.documentation_url(@spec.documentation['url']) : "[TODO]" %>
56+
#

elasticsearch-api/utils/thor/templates/_method_setup.erb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
%>
1919
<%- @spec.path_parts.each do |name, _| %>
2020
<%- unless @spec.method_name == 'get_field_mapping' && name == 'fields' %>
21-
<%= ' '*(@spec.namespace_depth+3) + "_#{name}" %> = arguments.delete(:<%=name %>)
21+
<%= "_#{name}" %> = arguments.delete(:<%=name %>)
2222
<%- end -%>
2323
<%- end -%>
2424

25-
<%= ' '*(@spec.namespace_depth+4) %>method = <%= @spec.http_method %>
25+
method = <%= @spec.http_method %>
2626
<%- if @spec.method_name == 'termvectors' %>
2727
endpoint = arguments.delete(:endpoint) || '_termvectors'
2828
<%- end -%>
29-
<%= ' '*(@spec.namespace_depth+4) %>path = <%= @spec.http_path %>
29+
path = <%= @spec.http_path %>
3030
<%- if [email protected]? || needs_ignore_404?(@spec.endpoint_name) || needs_complex_ignore_404?(@spec.endpoint_name)-%>
31-
<%= ' '*(@spec.namespace_depth+4) %>params = Utils.process_params(arguments)
31+
params = Utils.process_params(arguments)
3232
<%- else -%>
33-
<%= ' '*(@spec.namespace_depth+4) %>params = {}
33+
params = {}
3434
<%- end -%>
3535
<%- @spec.specific_params.each do |param| -%>
3636
<%= param %>

elasticsearch-api/utils/thor/templates/_perform_request.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
)
4848
<%- end -%>
4949
<%- end -%>
50-
<%= ' '*(@spec.namespace_depth+3) %>end
50+
end

elasticsearch-api/utils/thor/templates/method.erb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
# under the License.
1717
#
1818
module Elasticsearch
19-
<%= ' '*(@spec.namespace_depth) %>module API
19+
module API
2020
<%- @spec.module_namespace.each_with_index do |name, i| -%>
2121
<%= ' '*i %>module <%= module_name_helper(name) %>
2222
<%- end -%>
23-
<%= ' '*(@spec.namespace_depth+2) %>module Actions
23+
module Actions
2424
<%= ERB.new(File.new("./thor/templates/_documentation_top.erb").read, trim_mode: '-').result(binding) -%>
2525
<%# Method definition -%>
26-
<%= ' '*(@spec.namespace_depth+3) -%>def <%= @spec.method_name %>(arguments = {})
26+
def <%= @spec.method_name %>(arguments = {})
2727
request_opts = { endpoint: arguments[:endpoint] || "<%= @spec.endpoint_name %>" }
2828
<%- unless @spec.path_params.empty? %>
2929
defined_params =<%= @spec.path_params %>.inject({}) do |set_variables, variable|
@@ -34,20 +34,20 @@ module Elasticsearch
3434
<%- end %>
3535
<%- %>
3636
<%- if @spec.endpoint_name == 'create' -%>
37-
<%= ' '*(@spec.namespace_depth+3) %>if arguments[:id]
38-
<%= ' '*(@spec.namespace_depth+3) %> index arguments.update op_type: 'create'
39-
<%= ' '*(@spec.namespace_depth+3) %>else
40-
<%= ' '*(@spec.namespace_depth+3) %> index arguments
41-
<%= ' '*(@spec.namespace_depth+3) %>end
42-
<%= ' '*(@spec.namespace_depth+2) %>end
37+
if arguments[:id]
38+
index arguments.update op_type: 'create'
39+
else
40+
index arguments
41+
end
42+
end
4343
<%- else -%>
4444
<%- if @spec.method_name == 'get_field_mapping' %>
4545
arguments = arguments.clone
4646
_fields = arguments.delete(:field) || arguments.delete(:fields)
4747
raise ArgumentError, "Required argument 'field' missing" unless _fields
4848
<%- else -%>
4949
<%- @spec.required_parts.each do |required| %><%# Arguments -%>
50-
<%= ' '*(@spec.namespace_depth+3) + "raise ArgumentError, \"Required argument '#{required}' missing\" unless arguments[:#{required}]" + "\n" -%>
50+
<%= "raise ArgumentError, \"Required argument '#{required}' missing\" unless arguments[:#{required}]" + "\n" -%>
5151
<%- end -%>
5252
arguments = arguments.clone
5353
<%- end -%>
@@ -70,10 +70,10 @@ module Elasticsearch
7070
termvectors(arguments.merge endpoint: '_termvector')
7171
end
7272
<%- end -%>
73-
7473
<%- @spec.namespace_depth.downto(1) do |i| -%>
75-
<%= ' '*(i-1) %>end
74+
<%= ' '*(i-1) %>end
7675
<%- end if @spec.namespace_depth > 0 -%>
76+
7777
end
7878
end
7979
end

0 commit comments

Comments
 (0)