All Downloads are FREE. Search and download functionalities are using the official Maven repository.

v2.swift4.modelObject.mustache Maven / Gradle / Ivy

The newest version!

public struct {{classname}}: Codable {

{{#allVars}}{{#is this 'enum'}}{{> modelInlineEnumDeclaration}}{{/is}}{{/allVars}}{{#allVars}}{{#is this 'enum'}}    {{#description}}/** {{description}} */
    {{/description}}public var {{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/is}}
{{#isNot this 'enum'}}    {{#description}}/** {{description}} */
    {{/description}}public var {{name}}: {{{datatype}}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#objcCompatible}}{{#vendorExtensions.x-swift-optional-scalar}}
    public var {{name}}Num: NSNumber? {
        get {
            return {{name}}.map({ return NSNumber(value: $0) })
        }
    }{{/vendorExtensions.x-swift-optional-scalar}}{{/objcCompatible}}
{{/isNot}}{{/allVars}}{{#has this 'vars'}}
    public init({{#allVars}}{{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#has this 'more'}}, {{/has}}{{/allVars}}) { {{#allVars}}
        self.{{name}} = {{name}}{{/allVars}}
    }
{{/has~}}{{#additionalPropertiesType}}
    public var additionalProperties: [String:{{{additionalPropertiesType}}}] = [:]

    public subscript(key: String) -> {{{additionalPropertiesType}}}? {
        get {
            if let value = additionalProperties[key] {
                return value
            }
            return nil
        }

        set {
            additionalProperties[key] = newValue
        }
    }

    // Encodable protocol methods

    public func encode(to encoder: Encoder) throws {

        var container = encoder.container(keyedBy: String.self)

        {{#allVars}}
        try container.encode{{^required}}IfPresent{{/required}}({{{name}}}, forKey: "{{{baseName}}}")
        {{/allVars}}
        try container.encodeMap(additionalProperties)
    }

    // Decodable protocol methods

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: String.self)

        {{#allVars}}
        {{name}} = try container.decode{{^required}}IfPresent{{/required}}({{{datatypeWithEnum}}}.self, forKey: "{{{baseName}}}")
        {{/allVars}}
        var nonAdditionalPropertyKeys = Set()
        {{#allVars}}
        nonAdditionalPropertyKeys.insert("{{{baseName}}}")
        {{/allVars}}
        additionalProperties = try container.decodeMap({{{additionalPropertiesType}}}.self, excludedKeys: nonAdditionalPropertyKeys)
    }

{{/additionalPropertiesType}}{{^additionalPropertiesType}}{{#vendorExtensions.x-codegen-has-escaped-property-names}}
    public enum CodingKeys: String, CodingKey { {{#allVars}}
        case {{name}}{{#vendorExtensions.x-codegen-escaped-property-name}} = "{{{baseName}}}"{{/vendorExtensions.x-codegen-escaped-property-name}}{{/allVars}}
    }
{{/vendorExtensions.x-codegen-has-escaped-property-names}}{{/additionalPropertiesType}}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy