class GoonModelGen::Golang::Field

Attributes

goon_id[R]
name[R]
prepare_method[RW]
struct[RW]
tags[R]
type[R]
type_name[R]
unique[RW]

Public Class Methods

new(name, type_name, tags, goon_id: false) click to toggle source
# File lib/goon_model_gen/golang/field.rb, line 20
def initialize(name, type_name, tags, goon_id: false)
  @name, @type_name = name, type_name
  @tags = tags || {}
  @goon_id = goon_id
end

Public Instance Methods

definition_in(pkg) click to toggle source

@param pkg [Package] @return [string]

# File lib/goon_model_gen/golang/field.rb, line 41
def definition_in(pkg)
  type_exp =
    (type.package.path == pkg.path) ? type.name : type.qualified_name
  "#{ name } #{ type_exp } `#{ tags_string }`"
end
ptr?() click to toggle source
# File lib/goon_model_gen/golang/field.rb, line 47
def ptr?
  case type
  when Modifier then (type.prefix == "*")
  when Type then false
  else raise "Unsupported type class #{type.inspect}"
  end
end
resolve(pkgs) click to toggle source

@param pkgs [Packages]

# File lib/goon_model_gen/golang/field.rb, line 27
def resolve(pkgs)
  @type = pkgs.type_for(type_name) || raise("#{type_name.inspect} not found for #{struct.qualified_name}.#{name}")
end
short_desc(pkg2alias = nil) click to toggle source

@param pkg2alias [Hash<String,String>] @return [string]

# File lib/goon_model_gen/golang/field.rb, line 88
def short_desc(pkg2alias = nil)
  "#{name}: #{type.qualified_name(pkg2alias)}"
end
slice?() click to toggle source
# File lib/goon_model_gen/golang/field.rb, line 55
def slice?
  case type
  when Modifier then (type.prefix == "[]")
  when NamedSlice then true
  when Type then false
  else raise "Unsupported type class #{type.inspect}"
  end
end
struct?() click to toggle source
# File lib/goon_model_gen/golang/field.rb, line 64
def struct?
  case type
  when Modifier then false
  when Struct then true
  when Type then false
  else raise "Unsupported type class #{type.inspect}"
  end
end
tags_string() click to toggle source
# File lib/goon_model_gen/golang/field.rb, line 31
def tags_string
  tags.keys.sort.map do |key|
    val = tags[key]
    vals = val.is_a?(Array) ? val.join(',') : val.to_s
    vals.empty? ? nil : "#{key}:\"#{vals}\""
  end.compact.join(' ')
end
value_ptr?() click to toggle source
# File lib/goon_model_gen/golang/field.rb, line 73
def value_ptr?
  case type
  when Modifier then
    return false unless type.prefix == '*'
    case type.target
    when Builtin then type.target.name != 'interface'
    else false
    end
  when Type then false
  else raise "Unsupported type class #{type.inspect}"
  end
end