Class: GoaModelGen::Field
- Inherits:
-
Object
- Object
- GoaModelGen::Field
- 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
-
#datastore_tag ⇒ Object
readonly
Returns the value of attribute datastore_tag.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#format ⇒ Object
for swagger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
Returns the value of attribute required.
-
#swagger_name ⇒ Object
Returns the value of attribute swagger_name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#type_obj ⇒ Object
readonly
Returns the value of attribute type_obj.
-
#unique ⇒ Object
Returns the value of attribute unique.
-
#validation ⇒ Object
Returns the value of attribute validation.
Instance Method Summary collapse
- #assign_type_base(types) ⇒ Object
- #conv_func_part_for(value, with_pointer) ⇒ Object
- #conv_func_part_for_media_type ⇒ Object
- #conv_func_part_for_model ⇒ Object
- #conv_func_part_for_payload ⇒ Object
- #custom? ⇒ Boolean
- #definition ⇒ Object
- #goa_name ⇒ Object
- #golang_type ⇒ Object
-
#initialize(name, attrs) ⇒ Field
constructor
A new instance of Field.
- #media_type_assignment_options(f) ⇒ Object
- #not_null? ⇒ Boolean
- #nullable? ⇒ Boolean
- #optional? ⇒ Boolean
- #payload_assignment_options(f) ⇒ Object
- #primitive? ⇒ Boolean
- #tag ⇒ Object
- #type_package ⇒ Object
- #unique? ⇒ Boolean
- #zero_value_expression ⇒ Object
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_tag ⇒ Object (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 |
#default ⇒ Object (readonly)
Returns the value of attribute default.
9 10 11 |
# File 'lib/goa_model_gen/field.rb', line 9 def default @default end |
#format ⇒ Object
for swagger. See swagger.io/docs/specification/data-models/data-types/
10 11 12 |
# File 'lib/goa_model_gen/field.rb', line 10 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/goa_model_gen/field.rb', line 9 def name @name end |
#required ⇒ Object
Returns the value of attribute required.
11 12 13 |
# File 'lib/goa_model_gen/field.rb', line 11 def required @required end |
#swagger_name ⇒ Object
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 |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/goa_model_gen/field.rb', line 9 def type @type end |
#type_obj ⇒ Object (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 |
#unique ⇒ Object
Returns the value of attribute unique.
12 13 14 |
# File 'lib/goa_model_gen/field.rb', line 12 def unique @unique end |
#validation ⇒ Object
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_type ⇒ Object
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_model ⇒ Object
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_payload ⇒ Object
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
46 47 48 |
# File 'lib/goa_model_gen/field.rb', line 46 def custom? !primitive? end |
#definition ⇒ Object
92 93 94 |
# File 'lib/goa_model_gen/field.rb', line 92 def definition "#{ name } #{ type } `#{ tag }`" end |
#goa_name ⇒ Object
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_type ⇒ Object
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 (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
58 59 60 |
# File 'lib/goa_model_gen/field.rb', line 58 def not_null? required || !default.nil? end |
#nullable? ⇒ Boolean
61 62 63 |
# File 'lib/goa_model_gen/field.rb', line 61 def nullable? !not_null? end |
#optional? ⇒ 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 (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
42 43 44 |
# File 'lib/goa_model_gen/field.rb', line 42 def primitive? PRIMITIVE_TYPES.include?(type) end |
#tag ⇒ Object
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? = nullable? ? [] : ['required'] << validation.presence .compact! [ ['json', json_tag], ['validate', .join(',').presence], ['datastore', datastore_tag], ].map{|k,v| v ? "#{k}:\"#{v}\"" : nil}.compact.join(' ') end |
#type_package ⇒ Object
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
54 55 56 |
# File 'lib/goa_model_gen/field.rb', line 54 def unique? !!unique end |
#zero_value_expression ⇒ Object
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 |