Skip to content

Commit 1a81e16

Browse files
committed
[API] Code Generator: Refactors documentation helper functions
1 parent 99673ef commit 1a81e16

File tree

3 files changed

+67
-47
lines changed

3 files changed

+67
-47
lines changed

elasticsearch-api/utils/thor/generate_source.rb

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
require 'coderay'
2424
require 'pry'
2525
require_relative 'generator/build_hash_helper'
26+
require_relative 'generator/docs_helper'
2627
require_relative 'generator/files_helper'
2728
require_relative './endpoint_spec'
2829

@@ -42,6 +43,7 @@ class SourceGenerator < Thor
4243
namespace 'code'
4344
include Thor::Actions
4445
include EndpointSpecifics
46+
include DocsHelper
4547

4648
desc 'generate', 'Generate source code and tests from the REST API JSON specification'
4749
method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
@@ -65,68 +67,24 @@ def generate_source
6567
FilesHelper.files.each do |filepath|
6668
@spec = EndpointSpec.new(filepath)
6769
say_status 'json', @spec.path, :yellow
68-
6970
# Don't generate code for internal APIs:
7071
next if @spec.module_namespace.flatten.first == '_internal'
7172

7273
path_to_file = output.join(@spec.module_namespace.join('/')).join("#{@spec.method_name}.rb")
7374
dir = output.join(@spec.module_namespace.join('/'))
74-
7575
empty_directory(dir, verbose: false)
7676

7777
# Write the file with the ERB template:
7878
template('templates/method.erb', path_to_file, force: true)
7979

80+
# Optionals:
8081
print_source_code(path_to_file) if options[:verbose]
81-
8282
generate_tests if options[:tests]
83-
84-
puts
8583
end
86-
8784
run_rubocop
8885
BuildHashHelper.add_hash(@build_hash)
8986
end
9087

91-
def docs_helper(name, info)
92-
info['type'] = 'String' if info['type'] == 'enum' # Rename 'enums' to 'strings'
93-
info['type'] = 'Integer' if info['type'] == 'int' # Rename 'int' to 'Integer'
94-
tipo = info['type'] ? info['type'].capitalize : 'String'
95-
description = info['description'] ? info['description'].strip : '[TODO]'
96-
options = info['options'] ? "(options: #{info['options'].join(', ').strip})" : nil
97-
required = info['required'] ? '(*Required*)' : ''
98-
deprecated = info['deprecated'] ? '*Deprecated*' : ''
99-
optionals = [required, deprecated, options].join(' ').strip
100-
101-
"# @option arguments [#{tipo}] :#{name} #{description} #{optionals}\n"
102-
end
103-
104-
def stability_doc_helper(stability)
105-
return if stability == 'stable'
106-
107-
if stability == 'experimental'
108-
<<~MSG
109-
# This functionality is Experimental and may be changed or removed
110-
# completely in a future release. Elastic will take a best effort approach
111-
# to fix any issues, but experimental features are not subject to the
112-
# support SLA of official GA features.
113-
MSG
114-
elsif stability == 'beta'
115-
<<~MSG
116-
# This functionality is in Beta and is subject to change. The design and
117-
# code is less mature than official GA features and is being provided
118-
# as-is with no warranties. Beta features are not subject to the support
119-
# SLA of official GA features.
120-
MSG
121-
else
122-
<<~MSG
123-
# This functionality is subject to potential breaking changes within a
124-
# minor version, meaning that your referencing code may break when this
125-
# library is upgraded.
126-
MSG
127-
end
128-
end
129-
13088
def generate_tests
13189
copy_file 'templates/test_helper.rb', @output.join('test').join('test_helper.rb')
13290

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
module Elasticsearch
19+
module API
20+
# Helper with file related methods for code generation
21+
module DocsHelper
22+
def docs(name, info)
23+
info['type'] = 'String' if info['type'] == 'enum' # Rename 'enums' to 'strings'
24+
info['type'] = 'Integer' if info['type'] == 'int' # Rename 'int' to 'Integer'
25+
tipo = info['type'] ? info['type'].capitalize : 'String'
26+
description = info['description'] ? info['description'].strip : '[TODO]'
27+
options = info['options'] ? "(options: #{info['options'].join(', ').strip})" : nil
28+
required = info['required'] ? '(*Required*)' : ''
29+
deprecated = info['deprecated'] ? '*Deprecated*' : ''
30+
optionals = [required, deprecated, options].join(' ').strip
31+
32+
"# @option arguments [#{tipo}] :#{name} #{description} #{optionals}\n"
33+
end
34+
35+
def stability_doc_helper(stability)
36+
return if stability == 'stable'
37+
38+
if stability == 'experimental'
39+
<<~MSG
40+
# This functionality is Experimental and may be changed or removed
41+
# completely in a future release. Elastic will take a best effort approach
42+
# to fix any issues, but experimental features are not subject to the
43+
# support SLA of official GA features.
44+
MSG
45+
elsif stability == 'beta'
46+
<<~MSG
47+
# This functionality is in Beta and is subject to change. The design and
48+
# code is less mature than official GA features and is being provided
49+
# as-is with no warranties. Beta features are not subject to the support
50+
# SLA of official GA features.
51+
MSG
52+
else
53+
<<~MSG
54+
# This functionality is subject to potential breaking changes within a
55+
# minor version, meaning that your referencing code may break when this
56+
# library is upgraded.
57+
MSG
58+
end
59+
end
60+
end
61+
end
62+
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
<%= ' '*(@spec.namespace_depth+3) %>#
2626
<%- unless @spec.path_parts.nil? || @spec.path_parts.empty? %><%# URL parts -%>
2727
<%- @spec.path_parts.each do |name, info| -%>
28-
<%= docs_helper(name, info) -%>
28+
<%= docs(name, info) -%>
2929
<%- end -%>
3030
<%- end -%><%# Body -%>
3131
<%# URL parameters -%>
3232
<%- @spec.params.each do |name, info| -%>
33-
<%= docs_helper(name, info) unless ([email protected]_parts.empty? && @spec.path_parts.keys.include?(name)) -%>
33+
<%= docs(name, info) unless ([email protected]_parts.empty? && @spec.path_parts.keys.include?(name)) -%>
3434
<%- end -%>
3535
# @option arguments [Hash] :headers Custom HTTP headers
3636
<%- if @spec.body -%>

0 commit comments

Comments
 (0)