class Google::Apis::Generator::Annotator

Modifies an API description to support ruby code generation. Primarily does:

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 180
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 = {}
  @dup_method_names_for_rpc = collect_dup_method_names_for_rpc
  @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 172
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 215
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')
    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 269
def annotate_method(method, parent_resource = nil)
  @names.with_path(method.id) do
    method.parent = parent_resource
    # Grab the method name generated from the request object without
    # inserting into, or querying, the names hash.
    method_name_for_rpc = @names.infer_method_name_for_rpc(method, false)
    # If `method_name_for_rpc` is a duplicate (more than one method in
    # the API will generate this name), generate the method name from
    # the method ID instead.
    if @dup_method_names_for_rpc.include?(method_name_for_rpc)
      method.generated_name = @names.infer_method_name_from_id(method)
    # Otherwise, proceed as normal.
    elsif method_name_for_rpc
      method.generated_name = @names.infer_method_name_for_rpc(method)
    else
      method.generated_name = @names.infer_method_name_from_id(method)
    end
    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 291
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 257
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 234
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 326
def check_duplicate_method(m)
  if @all_methods.include?(m.generated_name)
    logger.error do
      sprintf('Duplicate method %s generated, conflicting paths %s and %s',
        m.generated_name, @names.key, @all_methods[m.generated_name])
    end
    fail 'Duplicate name generated'
  end
  @all_methods[m.generated_name] = @names.key
end
collect_dup_method_names_for_rpc() click to toggle source
# File lib/google/apis/generator/annotator.rb, line 209
def collect_dup_method_names_for_rpc
  method_names_for_rpc = []
  collect_method_names_for_rpc(@rest_description, method_names_for_rpc)
  method_names_for_rpc.group_by{ |e| e }.select { |k, v| v.size > 1 }.map(&:first)
end
collect_method_names_for_rpc(resource, method_names_for_rpc) click to toggle source
# File lib/google/apis/generator/annotator.rb, line 192
def collect_method_names_for_rpc(resource, method_names_for_rpc)
  resource.api_methods.each do |_k, v|
    # First look for the method name in the `@names` hash. If there's
    # no override set, generate it without inserting the generated name
    # into the `@names` hash.
    method_name_for_rpc = @names[@names.key]
    if method_name_for_rpc.nil?
      method_name_for_rpc = @names.infer_method_name_for_rpc(v, false)
    end
    method_names_for_rpc << method_name_for_rpc if method_name_for_rpc
  end unless resource.api_methods.nil?

  resource.resources.each do |_k, v|
    collect_method_names_for_rpc(v, method_names_for_rpc)
  end unless resource.resources.nil?
end
resolve_type_references() click to toggle source
# File lib/google/apis/generator/annotator.rb, line 301
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 313
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