-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* override modelObject.mustache * container -> codingContainer
- Loading branch information
dphuang2
authored
Sep 24, 2024
1 parent
5015827
commit b8801bf
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
generator/konfig-generator-api/src/main/resources/swift5/modelObject.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#useClasses}}final class{{/useClasses}}{{^useClasses}}struct{{/useClasses}} {{{classname}}}: {{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable{{#useJsonEncodable}}, JSONEncodable{{/useJsonEncodable}}{{/useVapor}}{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}} { | ||
{{/objcCompatible}}{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable{{#useJsonEncodable}}, JSONEncodable{{/useJsonEncodable}} { | ||
{{/objcCompatible}} | ||
|
||
{{#allVars}} | ||
{{#isEnum}} | ||
{{> modelInlineEnumDeclaration}} | ||
{{/isEnum}} | ||
{{/allVars}} | ||
{{#allVars}} | ||
{{#isEnum}} | ||
{{#description}}/** {{{.}}} */ | ||
{{/description}}{{#deprecated}}@available(*, deprecated, message: "This property is deprecated.") | ||
{{/deprecated}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{{name}}}: {{#vendorExtensions.x-null-encodable}}NullEncodable<{{{datatypeWithEnum}}}>{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}{{#defaultValue}} = {{#vendorExtensions.x-null-encodable}}{{{vendorExtensions.x-null-encodable-default-value}}}{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{.}}}{{/vendorExtensions.x-null-encodable}}{{/defaultValue}} | ||
{{/isEnum}} | ||
{{^isEnum}} | ||
{{#description}}/** {{{.}}} */ | ||
{{/description}}{{#deprecated}}@available(*, deprecated, message: "This property is deprecated.") | ||
{{/deprecated}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{{name}}}: {{#vendorExtensions.x-null-encodable}}NullEncodable<{{{datatype}}}>{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{datatype}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}{{#defaultValue}} = {{#vendorExtensions.x-null-encodable}}{{{vendorExtensions.x-null-encodable-default-value}}}{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{.}}}{{/vendorExtensions.x-null-encodable}}{{/defaultValue}} | ||
{{#objcCompatible}} | ||
{{#vendorExtensions.x-swift-optional-scalar}} | ||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{{name}}}Num: NSNumber? { | ||
get { | ||
{{^vendorExtensions.x-null-encodable}} | ||
return {{{name}}} as NSNumber? | ||
{{/vendorExtensions.x-null-encodable}} | ||
{{#vendorExtensions.x-null-encodable}} | ||
if case .encodeValue(let value) = {{name}} { | ||
return value as NSNumber? | ||
} else { | ||
return nil | ||
} | ||
{{/vendorExtensions.x-null-encodable}} | ||
} | ||
} | ||
{{/vendorExtensions.x-swift-optional-scalar}} | ||
{{/objcCompatible}} | ||
{{/isEnum}} | ||
{{/allVars}} | ||
{{#hasVars}} | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init({{#allVars}}{{{name}}}: {{#vendorExtensions.x-null-encodable}}NullEncodable<{{{datatypeWithEnum}}}>{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}{{#defaultValue}} = {{#vendorExtensions.x-null-encodable}}{{{vendorExtensions.x-null-encodable-default-value}}}{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{.}}}{{/vendorExtensions.x-null-encodable}}{{/defaultValue}}{{^defaultValue}}{{^required}} = {{#vendorExtensions.x-null-encodable}}.encodeNull{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}nil{{/vendorExtensions.x-null-encodable}}{{/required}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/allVars}}) { | ||
{{#allVars}} | ||
self.{{{name}}} = {{{name}}} | ||
{{/allVars}} | ||
} | ||
{{/hasVars}} | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum CodingKeys: {{#hasVars}}String, {{/hasVars}}CodingKey, CaseIterable { | ||
{{#allVars}} | ||
case {{{name}}}{{#vendorExtensions.x-codegen-escaped-property-name}} = "{{{baseName}}}"{{/vendorExtensions.x-codegen-escaped-property-name}} | ||
{{/allVars}} | ||
}{{#generateModelAdditionalProperties}}{{#additionalPropertiesType}} | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var additionalProperties: [String: {{{additionalPropertiesType}}}] = [:] | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} subscript(key: String) -> {{{additionalPropertiesType}}}? { | ||
get { | ||
if let value = additionalProperties[key] { | ||
return value | ||
} | ||
return nil | ||
} | ||
|
||
set { | ||
additionalProperties[key] = newValue | ||
} | ||
}{{/additionalPropertiesType}}{{/generateModelAdditionalProperties}} | ||
|
||
// Encodable protocol methods | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func encode(to encoder: Encoder) throws { | ||
var codingContainer = encoder.container(keyedBy: CodingKeys.self) | ||
{{#allVars}} | ||
{{#vendorExtensions.x-null-encodable}} | ||
switch {{{name}}} { | ||
case .encodeNothing: break | ||
case .encodeNull, .encodeValue: try container.encode({{{name}}}, forKey: .{{{name}}}) | ||
} | ||
{{/vendorExtensions.x-null-encodable}} | ||
{{^vendorExtensions.x-null-encodable}} | ||
try codingContainer.encode{{^required}}IfPresent{{/required}}({{{name}}}, forKey: .{{{name}}}) | ||
{{/vendorExtensions.x-null-encodable}} | ||
{{/allVars}} | ||
{{#generateModelAdditionalProperties}} | ||
{{#additionalPropertiesType}} | ||
var additionalPropertiesContainer = encoder.container(keyedBy: String.self) | ||
try additionalPropertiesContainer.encodeMap(additionalProperties) | ||
{{/additionalPropertiesType}} | ||
{{/generateModelAdditionalProperties}} | ||
}{{#generateModelAdditionalProperties}}{{#additionalPropertiesType}} | ||
|
||
// Decodable protocol methods | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}}{{#objcCompatible}} required{{/objcCompatible}} init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
{{#allVars}} | ||
{{{name}}} = try container.decode{{#required}}{{#isNullable}}IfPresent{{/isNullable}}{{/required}}{{^required}}IfPresent{{/required}}({{{datatypeWithEnum}}}.self, forKey: .{{{name}}}) | ||
{{/allVars}} | ||
var nonAdditionalPropertyKeys = Set<String>() | ||
{{#allVars}} | ||
nonAdditionalPropertyKeys.insert("{{{baseName}}}") | ||
{{/allVars}} | ||
let additionalPropertiesContainer = try decoder.container(keyedBy: String.self) | ||
additionalProperties = try additionalPropertiesContainer.decodeMap({{{additionalPropertiesType}}}.self, excludedKeys: nonAdditionalPropertyKeys) | ||
}{{/additionalPropertiesType}}{{/generateModelAdditionalProperties}}{{^objcCompatible}}{{#useClasses}}{{#vendorExtensions.x-swift-hashable}} | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func == (lhs: {{classname}}, rhs: {{classname}}) -> Bool { | ||
{{#allVars}} | ||
lhs.{{{name}}} == rhs.{{{name}}}{{^-last}} &&{{/-last}} | ||
{{/allVars}} | ||
{{#generateModelAdditionalProperties}}{{#additionalPropertiesType}}{{#hasVars}}&& {{/hasVars}}lhs.additionalProperties == rhs.additionalProperties{{/additionalPropertiesType}}{{/generateModelAdditionalProperties}} | ||
} | ||
|
||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func hash(into hasher: inout Hasher) { | ||
{{#allVars}} | ||
hasher.combine({{{name}}}{{^vendorExtensions.x-null-encodable}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}.hashValue) | ||
{{/allVars}} | ||
{{#generateModelAdditionalProperties}}{{#additionalPropertiesType}}hasher.combine(additionalProperties.hashValue){{/additionalPropertiesType}}{{/generateModelAdditionalProperties}} | ||
}{{/vendorExtensions.x-swift-hashable}}{{/useClasses}}{{/objcCompatible}} | ||
} |