Class: GoonModelGen::Golang::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/goon_model_gen/golang/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type_name, tags, goon_id: false) ⇒ Field

Returns a new instance of Field.



20
21
22
23
24
# 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

Instance Attribute Details

#goon_idObject (readonly)

true/false



14
15
16
# File 'lib/goon_model_gen/golang/field.rb', line 14

def goon_id
  @goon_id
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/goon_model_gen/golang/field.rb', line 12

def name
  @name
end

#prepare_methodObject

Returns the value of attribute prepare_method.



17
18
19
# File 'lib/goon_model_gen/golang/field.rb', line 17

def prepare_method
  @prepare_method
end

#structObject

Returns the value of attribute struct.



16
17
18
# File 'lib/goon_model_gen/golang/field.rb', line 16

def struct
  @struct
end

#tagsObject (readonly)

Hash<string,Array> ex. for datastore, validate, json, etc…



13
14
15
# File 'lib/goon_model_gen/golang/field.rb', line 13

def tags
  @tags
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/goon_model_gen/golang/field.rb', line 15

def type
  @type
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



12
13
14
# File 'lib/goon_model_gen/golang/field.rb', line 12

def type_name
  @type_name
end

#uniqueObject

Returns the value of attribute unique.



18
19
20
# File 'lib/goon_model_gen/golang/field.rb', line 18

def unique
  @unique
end

Instance Method Details

#definition_in(pkg) ⇒ string

Parameters:

Returns:

  • (string)


41
42
43
44
45
# 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?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# 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) ⇒ Object

Parameters:



27
28
29
# 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) ⇒ string

Parameters:

  • pkg2alias (Hash<String,String>) (defaults to: nil)

Returns:

  • (string)


88
89
90
# File 'lib/goon_model_gen/golang/field.rb', line 88

def short_desc(pkg2alias = nil)
  "#{name}: #{type.qualified_name(pkg2alias)}"
end

#slice?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# 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?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# 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_stringObject



31
32
33
34
35
36
37
# 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?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
# 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