-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbase_object.mustache
203 lines (194 loc) · 6.64 KB
/
base_object.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
166
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
193
194
195
196
197
198
199
200
201
202
203
# Builds the object from hash
# @param [Hash] attributes Model attributes in the form of hash
# @return [Object] Returns the model itself
{{^useCustomTemplateCode}}
def self.build_from_hash(attributes)
return nil unless attributes.is_a?(Hash)
{{#parent}}
super(attributes)
{{/parent}}
attributes = attributes.transform_keys(&:to_sym)
transformed_hash = {}
openapi_types.each_pair do |key, type|
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
transformed_hash["#{key}"] = nil
elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute
# is documented as an array but the input is not
if attributes[attribute_map[key]].is_a?(Array)
transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
end
elsif !attributes[attribute_map[key]].nil?
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
end
end
new(transformed_hash)
end
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
def self.build_from_hash(attributes)
{{#discriminator}}
if !attributes[self.openapi_discriminator_name].nil?
klass = self.discriminator_class_name(attributes[self.openapi_discriminator_name])
if klass != new.class.to_s
obj = Object.const_get(klass).new.build_from_hash(attributes)
return obj
end
end
{{/discriminator}}
new.build_from_hash(attributes)
end
# Builds the object from hash
# @param [Hash] attributes Model attributes in the form of hash
# @return [Object] Returns the model itself
def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash)
{{#parent}}
super(attributes)
{{/parent}}
attribute_map = self.class.merged_attributes
self.class.merged_types.each_pair do |key, type|
if type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute
# is documented as an array but the input is not
if attributes[attribute_map[key]].is_a?(Array)
self.send("#{key}=", attributes[attribute_map[key]].map { |v| _deserialize($1, v) })
end
elsif !attributes[attribute_map[key]].nil?
self.send("#{key}=", _deserialize(type, attributes[attribute_map[key]]))
end
end
self
end
{{/useCustomTemplateCode}}
# Deserializes the data based on type
# @param string type Data type
# @param string value Value to be deserialized
# @return [Object] Deserialized data
{{^useCustomTemplateCode}}
def self._deserialize(type, value)
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
def _deserialize(type, value)
{{/useCustomTemplateCode}}
case type.to_sym
when :Time
Time.parse(value)
when :Date
Date.parse(value)
when :String
value.to_s
when :Integer
value.to_i
when :Float
value.to_f
when :Boolean
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
true
else
false
end
when :Object
# generic object (usually a Hash), return directly
value
when /\AArray<(?<inner_type>.+)>\z/
inner_type = Regexp.last_match[:inner_type]
value.map { |v| _deserialize(inner_type, v) }
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
k_type = Regexp.last_match[:k_type]
v_type = Regexp.last_match[:v_type]
{}.tap do |hash|
value.each do |k, v|
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
end
end
else # model
{{^useCustomTemplateCode}}
# models (e.g. Pet) or oneOf
{{/useCustomTemplateCode}}
klass = {{moduleName}}.const_get(type)
klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
end
end
# Returns the string representation of the object
# @return [String] String presentation of the object
def to_s
to_hash.to_s
end
# to_body is an alias to to_hash (backward compatibility)
# @return [Hash] Returns the object in the form of hash
def to_body
to_hash
end
# Returns the object in the form of hash
# @return [Hash] Returns the object in the form of hash
{{^useCustomTemplateCode}}
def to_hash
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
def to_hash(include_nil = true)
{{/useCustomTemplateCode}}
hash = {{^parent}}{}{{/parent}}{{#parent}}super{{/parent}}
{{^useCustomTemplateCode}}
self.class.attribute_map.each_pair do |attr, param|
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
self.class.merged_attributes.each_pair do |attr, param|
{{/useCustomTemplateCode}}
value = self.send(attr)
if value.nil?
{{^useCustomTemplateCode}}
is_nullable = self.class.openapi_nullable.include?(attr)
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
next unless include_nil
is_nullable = self.class.merged_nullable.include?(attr)
{{/useCustomTemplateCode}}
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end
{{^useCustomTemplateCode}}
hash[param] = _to_hash(value)
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
hash[param] = _to_hash(value, include_nil)
{{/useCustomTemplateCode}}
end
hash
end
# Outputs non-array value in the form of hash
# For object, use to_hash. Otherwise, just return the value
# @param [Object] value Any valid value
# @return [Hash] Returns the value in the form of hash
{{^useCustomTemplateCode}}
def _to_hash(value)
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
def _to_hash(value, include_nil = true)
{{/useCustomTemplateCode}}
if value.is_a?(Array)
{{^useCustomTemplateCode}}
value.compact.map { |v| _to_hash(v) }
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
value.compact.map { |v| _to_hash(v, include_nil) }
{{/useCustomTemplateCode}}
elsif value.is_a?(Hash)
{}.tap do |hash|
{{^useCustomTemplateCode}}
value.each { |k, v| hash[k] = _to_hash(v) }
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
value.each { |k, v| hash[k] = _to_hash(v, include_nil) }
{{/useCustomTemplateCode}}
end
elsif value.respond_to? :to_hash
{{^useCustomTemplateCode}}
value.to_hash
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
value.to_hash(include_nil)
{{/useCustomTemplateCode}}
else
value
end
end