class Gapic::Presenters::FieldPresenter

A presenter for proto fields.

Public Class Methods

new(api, message, field) click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 28
def initialize api, message, field
  @api = api
  @message = message
  @field = field
end

Public Instance Methods

as_kwarg(value: nil) click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 61
def as_kwarg value: nil
  "#{name}: #{value || name}"
end
camel_name() click to toggle source

Name of this field, camel-cased @return [String]

# File lib/gapic/presenters/field_presenter.rb, line 131
def camel_name
  camel_name_for name
end
default_value() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 55
def default_value
  single = default_singular_value
  return "[#{single}]" if @field.repeated? && !@field.map?
  single
end
default_value_for_type() click to toggle source

Returns a stringified default value for the protobuf types that are possible to fit into the query string parameter and nil for the other types (e.g. Messages)

@return [String, nil]

# File lib/gapic/presenters/field_presenter.rb, line 114
def default_value_for_type
  if @field.message?
    nil
  elsif @field.enum?
    ":#{@field.enum.values.first.name}"
  else
    case @field.type
    when 1, 2, 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "0" # floating point or integer
    when 9, 12                                   then "\"\""
    when 8                                       then "false"
    end
  end
end
doc_attribute_type() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 42
def doc_attribute_type
  mode = @field.output_only? ? "r" : "rw"
  "@!attribute [#{mode}] #{@field.name}"
end
doc_description() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 51
def doc_description
  @field.docs_leading_comments
end
doc_types() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 38
def doc_types
  field_doc_types @field, false
end
enum?() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 79
def enum?
  @field.enum?
end
map?() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 95
def map?
  @field.map?
end
message?() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 75
def message?
  @field.message?
end
name() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 34
def name
  @field.name
end
oneof?() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 99
def oneof?
  @field.oneof?
end
oneof_name() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 103
def oneof_name
  @message.oneof_decl[@field.oneof_index].name
end
output_doc_types() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 47
def output_doc_types
  field_doc_types @field, true
end
proto3_optional?() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 83
def proto3_optional?
  @field.proto3_optional?
end
repeated?() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 87
def repeated?
  @field.repeated?
end
required?() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 91
def required?
  @field.required?
end
type_name() click to toggle source

TODO: remove, only used in tests

# File lib/gapic/presenters/field_presenter.rb, line 66
def type_name
  @field.type_name
end
type_name_full() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 70
def type_name_full
  return nil if type_name.blank?
  ruby_namespace @api, type_name
end

Protected Instance Methods

camel_name_for(attr_name) click to toggle source

Converts a snake_case parameter name into camelCase for query string parameters @param attr_name [String] @return [String] camel-cased parameter name

# File lib/gapic/presenters/field_presenter.rb, line 201
def camel_name_for attr_name
  parts = attr_name.split "_"
  first_part = parts[0]
  other_parts = parts[1..-1]
  other_parts_pascal = other_parts.map(&:capitalize).join
  "#{first_part}#{other_parts_pascal}"
end
default_singular_value() click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 175
def default_singular_value
  if @field.message?
    "{}"
  elsif @field.enum?
    # TODO: select the first non-0 enum value
    ":#{@field.enum.values.first.name}"
  else
    case @field.type
    when 1, 2                              then "3.5"
    when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "42"
    when 9, 12                             then "\"hello world\""
    when 8                                 then "true"
    else
      "::Object"
    end
  end
end
field_doc_types(field, output) click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 137
def field_doc_types field, output
  return field_map_type field.message, output if field.map?
  base_type =
    if field.message?
      type = message_ruby_type field.message
      output ? type : "#{type}, ::Hash"
    elsif field.enum?
      # TODO: handle when arg message is nil and enum is the type
      message_ruby_type field.enum
    else
      case field.type
      when 1, 2                              then "::Float"
      when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "::Integer"
      when 9, 12                             then "::String"
      when 8                                 then "::Boolean"
      else
        "::Object"
      end
    end
  field.repeated? ? "::Array<#{base_type}>" : base_type
end
field_map_type(entry_message, output) click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 159
def field_map_type entry_message, output
  key_field = value_field = nil
  entry_message.fields.each do |field|
    key_field = field if field.name == "key"
    value_field = field if field.name == "value"
  end
  class_name = output ? "::Google::Protobuf::Map" : "::Hash"
  if key_field && value_field
    key_type = field_doc_types key_field, output
    value_type = field_doc_types value_field, output
    "#{class_name}{#{key_type} => #{value_type}}"
  else
    class_name
  end
end
message_ruby_type(message) click to toggle source
# File lib/gapic/presenters/field_presenter.rb, line 193
def message_ruby_type message
  ruby_namespace @api, message.address.join(".")
end