Skip to content

Commit

Permalink
Generator: Refactors code generation for visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Sep 16, 2024
1 parent 9150c6a commit aa6d529
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions elasticsearch-api/utils/thor/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,38 @@ module API
class EndpointSpec
include EndpointSpecifics

# These APIs are private, but were added in 8.x since the generator didn't consider
# visibility. They will be removed in 9.x since we're using a different generator that
# considers visibility. But new private APIs code won't be generated for the client.
EXCLUDED_8X = [
'autoscaling.delete_autoscaling_policy', 'autoscaling.get_autoscaling_capacity',
'autoscaling.get_autoscaling_policy', 'autoscaling.put_autoscaling_policy', 'capabilities',
'connector.secret_delete', 'connector.secret_get', 'connector.secret_post',
'connector.secret_put', 'fleet.delete_secret', 'fleet.get_secret', 'fleet.post_secret',
'ml.validate', 'ml.validate_detector', 'monitoring.bulk', 'profiling.flamegraph',
'profiling.stacktraces', 'profiling.status', 'profiling.topn_functions',
'security.activate_user_profile', 'security.disable_user_profile',
'security.enable_user_profile', 'security.get_user_profile',
'security.has_privileges_user_profile', 'security.suggest_user_profiles',
'security.update_user_profile_data', 'shutdown.delete_node', 'shutdown.get_node',
'shutdown.put_node'
].freeze

def initialize(filepath)
@path = Pathname(filepath)
json = MultiJson.load(File.read(@path))
@spec = json.values.first
@endpoint_name = json.keys.first

full_namespace = parse_full_namespace
@namespace_depth = full_namespace.size > 0 ? full_namespace.size - 1 : 0
@namespace_depth = full_namespace.size.positive? ? full_namespace.size - 1 : 0
@module_namespace = full_namespace[0, @namespace_depth]
@method_name = full_namespace.last

@path_parts = parse_endpoint_parts(@spec)
@params = @spec['params'] || {}
@paths = @spec['url']['paths'].map { |b| b['path'] } if @spec['url']
@path_params = path_variables.flatten.uniq.collect(&:to_sym)
@path_params = path_variables.flatten.uniq.collect(&:to_sym)
@http_method = parse_http_method(@spec)
@deprecation_note = @spec['url']['paths'].last&.[]('deprecated')
@http_path = parse_http_path(@paths)
Expand Down Expand Up @@ -71,6 +88,16 @@ def stability
@spec['stability']
end

def visibility
@spec['visibility']
end

def skippable?
return true if module_namespace.flatten.first == '_internal'

visibility != 'public' && !EXCLUDED_8X.include?(endpoint_name)
end

# Function that adds the listified h param code
def specific_params
super(@module_namespace.first, @method_name)
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-api/utils/thor/generate_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def generate_source
@spec = EndpointSpec.new(filepath)
say_status 'json', @spec.path, :yellow
# Don't generate code for internal APIs:
next if @spec.module_namespace.flatten.first == '_internal'
next if @spec.skippable?

path_to_file = output.join(@spec.module_namespace.join('/')).join("#{@spec.method_name}.rb")
dir = output.join(@spec.module_namespace.join('/'))
Expand Down

0 comments on commit aa6d529

Please sign in to comment.