Class: GoaModelGen::Field

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

Constant Summary collapse

PRIMITIVE_TYPES =
%w[bool int int64 float string time.Time uuid.UUID *datastore.Key]
SWAGGER_TYPE_TO_GOLANG_TYPE =
{
  "string" => Hash.new("string"),
  "number" => Hash.new("float32").update(
    "double" => "float64",
  ),
  "integer" => Hash.new("int"),
  "boolean" => Hash.new("bool"),
}
MODEL_FUNC_NAME_FILTERS =
{
  "TimeTime" => "Time",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs) ⇒ Field

Returns a new instance of Field.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/goa_model_gen/field.rb', line 18

def initialize(name, attrs)
  @name = name
  @type = attrs['type']
  @format = attrs['format']
  @required = attrs['required']
  @unique = attrs['unique']
  @default = attrs['default']
  @validation = attrs['validation']
  @goa_name = attrs['goa_name']
  @swagger_name = attrs['swagger_name']
  @datastore_tag = attrs['datastore_tag']
end

Instance Attribute Details

#datastore_tagObject (readonly)

Returns the value of attribute datastore_tag.



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

def datastore_tag
  @datastore_tag
end

#defaultObject (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/goa_model_gen/field.rb', line 9

def default
  @default
end

#formatObject



10
11
12
# File 'lib/goa_model_gen/field.rb', line 10

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/goa_model_gen/field.rb', line 9

def name
  @name
end

#requiredObject

Returns the value of attribute required.



11
12
13
# File 'lib/goa_model_gen/field.rb', line 11

def required
  @required
end

#swagger_nameObject

Returns the value of attribute swagger_name.



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

def swagger_name
  @swagger_name
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/goa_model_gen/field.rb', line 9

def type
  @type
end

#type_objObject (readonly)

Returns the value of attribute type_obj.



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

def type_obj
  @type_obj
end

#uniqueObject

Returns the value of attribute unique.



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

def unique
  @unique
end

#validationObject

Returns the value of attribute validation.



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

def validation
  @validation
end

Instance Method Details

#assign_type_base(types) ⇒ Object



75
76
77
# File 'lib/goa_model_gen/field.rb', line 75

def assign_type_base(types)
  @type_obj = types[self.type]
end

#conv_func_part_for(value, with_pointer) ⇒ Object



135
136
137
138
# File 'lib/goa_model_gen/field.rb', line 135

def conv_func_part_for(value, with_pointer)
  r = value.sub(/\A\*/, '').split('.').map(&:camelize).join
  with_pointer ? "#{r}Pointer" : r
end

#conv_func_part_for_media_typeObject



131
132
133
# File 'lib/goa_model_gen/field.rb', line 131

def conv_func_part_for_media_type
  conv_func_part_for(golang_type, nullable?)
end

#conv_func_part_for_modelObject



122
123
124
125
# File 'lib/goa_model_gen/field.rb', line 122

def conv_func_part_for_model
  r = conv_func_part_for(type, !!(/\A\*/ =~ type))
  MODEL_FUNC_NAME_FILTERS[r] || r
end

#conv_func_part_for_payloadObject



127
128
129
# File 'lib/goa_model_gen/field.rb', line 127

def conv_func_part_for_payload
  conv_func_part_for(golang_type, nullable?)
end

#custom?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/goa_model_gen/field.rb', line 46

def custom?
  !primitive?
end

#definitionObject



92
93
94
# File 'lib/goa_model_gen/field.rb', line 92

def definition
  "#{ name } #{ type } `#{ tag }`"
end

#goa_nameObject



34
35
36
# File 'lib/goa_model_gen/field.rb', line 34

def goa_name
  @goa_name.presence || Goa.capitalize_join(swagger_name.split("_"))
end

#golang_typeObject



112
113
114
115
116
# File 'lib/goa_model_gen/field.rb', line 112

def golang_type
  format2type = SWAGGER_TYPE_TO_GOLANG_TYPE[type]
  raise "Golang type not found for #{self.inspect}" unless format2type
  return format2type[format]
end

#media_type_assignment_options(f) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/goa_model_gen/field.rb', line 167

def media_type_assignment_options(f)
  if custom?
    if type_obj && type_obj.base
      if f.not_null?
        return false, false, type_obj.base # 型キャスト
      else
        st = type_obj.base.camelize
        dt = f.golang_type.camelize
        return false, false, [type_obj.base, "#{st}To#{dt}Pointer"] # 型キャストしてポインタを値に変換
      end
    else
      return false, true, "#{type}ModelToMediaType"
    end
  else
    if type == f.golang_type
      if f.not_null?
        return true, nil, nil
      else
        return false, false, "#{conv_func_part_for_model}To#{f.conv_func_part_for_payload}"
      end
    else
      with_error = (type == 'string')
      return false, with_error, "#{conv_func_part_for_model}To#{f.conv_func_part_for_payload}"
    end
  end
end

#not_null?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/goa_model_gen/field.rb', line 58

def not_null?
  required || !default.nil?
end

#nullable?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/goa_model_gen/field.rb', line 61

def nullable?
  !not_null?
end

#optional?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/goa_model_gen/field.rb', line 50

def optional?
  !required
end

#payload_assignment_options(f) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/goa_model_gen/field.rb', line 140

def payload_assignment_options(f)
  if custom?
    if type_obj && type_obj.base
      if f.not_null?
        return false, false, type # 型キャスト
      else
        st = f.golang_type.camelize
        dt = type_obj.base.camelize
        return false, false, ["#{st}PointerTo#{dt}", type] # ポインタを値にしてから型キャスト
      end
    else
      return false, true, "#{type}PayloadToModel"
    end
  else
    if type == f.golang_type
      if f.not_null?
        return true, nil, nil
      else
        return false, false, "#{f.conv_func_part_for_payload}To#{conv_func_part_for_model}"
      end
    else
      with_error = (f.type == 'string')
      return false, with_error, "#{f.conv_func_part_for_payload}To#{conv_func_part_for_model}"
    end
  end
end

#primitive?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/goa_model_gen/field.rb', line 42

def primitive?
  PRIMITIVE_TYPES.include?(type)
end

#tagObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/goa_model_gen/field.rb', line 79

def tag
  json_tag = name.underscore.dup
  json_tag << ',omitempty' if nullable?
  validate_tags = nullable? ? [] : ['required']
  validate_tags << validation.presence
  validate_tags.compact!
  [
    ['json', json_tag],
    ['validate', validate_tags.join(',').presence],
    ['datastore', datastore_tag],
  ].map{|k,v| v ? "#{k}:\"#{v}\"" : nil}.compact.join(' ')
end

#type_packageObject



96
97
98
# File 'lib/goa_model_gen/field.rb', line 96

def type_package
  type.include?('.') ? type.split('.', 2).first.sub(/\A\*/, '') : nil
end

#unique?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/goa_model_gen/field.rb', line 54

def unique?
  !!unique
end

#zero_value_expressionObject



65
66
67
68
69
70
71
72
73
# File 'lib/goa_model_gen/field.rb', line 65

def zero_value_expression
  case type
  when 'bool' then 'false'
  when 'int', 'int32', 'int64',
       'float', 'float32', 'float64' then '0'
  when 'string', 'UUID' then '""'
  else raise "Unsupproted zero value for #{type}"
  end
end