class Google::Apis::Generator::Annotator
Modifies an API description to support ruby code generation. Primarily does:
-
Ensure all names follow appopriate ruby conventions
-
Maps types to ruby types, classes, and resolves $refs
-
Attempts to simplify names where possible to make APIs more sensible
Constants
- PARAMETER_BLACKLIST
Don't expose these in the API directly.
Public Class Methods
new(description, api_names = nil)
click to toggle source
@param [Google::Apis::DiscoveryV1::RestDescription] description
API Description
@param [Google::Api::Generator::Names] api_names
Name helper instanace
# File lib/google/apis/generator/annotator.rb, line 172 def initialize(description, api_names = nil) api_names = Names.new if api_names.nil? @names = api_names @rest_description = description @registered_types = [] @deferred_types = [] @strip_prefixes = [] @all_methods = {} @path = [] end
process(description, api_names = nil)
click to toggle source
Prepare the API for the templates. @param [Google::Apis::DiscoveryV1::RestDescription] description
API Description
# File lib/google/apis/generator/annotator.rb, line 164 def self.process(description, api_names = nil) Annotator.new(description, api_names).annotate_api end
Public Instance Methods
annotate_api()
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 183 def annotate_api @names.with_path(@rest_description.id) do @strip_prefixes << @rest_description.name if @rest_description.auth @rest_description.auth.oauth2.scopes.each do |key, value| value.constant = constantize_scope(key) end end @rest_description.force_alt_json = @names.option('force_alt_json') @rest_description.parameters.reject! { |k, _v| PARAMETER_BLACKLIST.include?(k) } annotate_parameters(@rest_description.parameters) annotate_resource(@rest_description.name, @rest_description) @rest_description.schemas.each do |k, v| annotate_type(k, v, @rest_description) end unless @rest_description.schemas.nil? end resolve_type_references resolve_variants end
annotate_method(method, parent_resource = nil)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 238 def annotate_method(method, parent_resource = nil) @names.with_path(method.id) do method.parent = parent_resource method.generated_name = @names.infer_method_name(method) check_duplicate_method(method) annotate_parameters(method.parameters) end end
annotate_parameters(parameters)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 247 def annotate_parameters(parameters) parameters.each do |key, value| @names.with_path(key) do value.name = key value.generated_name = @names.infer_parameter_name @deferred_types << value if value._ref end end unless parameters.nil? end
annotate_resource(name, resource, parent_resource = nil)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 226 def annotate_resource(name, resource, parent_resource = nil) @strip_prefixes << name resource.parent = parent_resource unless parent_resource.nil? resource.api_methods.each do |_k, v| annotate_method(v, resource) end unless resource.api_methods.nil? resource.resources.each do |k, v| annotate_resource(k, v, resource) end unless resource.resources.nil? end
annotate_type(name, type, parent)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 203 def annotate_type(name, type, parent) @names.with_path(name) do type.name = name type.path = @names.key type.generated_name = @names.infer_property_name if type.type == 'object' type.generated_class_name = ActiveSupport::Inflector.camelize(type.generated_name) @registered_types << type end type.parent = parent @deferred_types << type if type._ref type.properties.each do |k, v| annotate_type(k, v, type) end unless type.properties.nil? if type.additional_properties type.type = 'hash' annotate_type(ActiveSupport::Inflector.singularize(type.generated_name), type.additional_properties, parent) end annotate_type(ActiveSupport::Inflector.singularize(type.generated_name), type.items, parent) if type.items end end
check_duplicate_method(m)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 282 def check_duplicate_method(m) if @all_methods.include?(m.generated_name) logger.error do sprintf('Duplicate method %s generated, path %s', m.generated_name, @names.key) end fail 'Duplicate name generated' end @all_methods[m.generated_name] = m end
resolve_type_references()
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 257 def resolve_type_references @deferred_types.each do |type| if type._ref ref = @rest_description.schemas[type._ref] ivars = ref.instance_variables - [:@name, :@generated_name] (ivars).each do |var| type.instance_variable_set(var, ref.instance_variable_get(var)) end end end end
resolve_variants()
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 269 def resolve_variants @deferred_types.each do |type| if type.variant type.variant.map.each do |v| ref = @rest_description.schemas[v._ref] ref.base_ref = type ref.discriminant = type.variant.discriminant ref.discriminant_value = v.type_value end end end end