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

swift6.modelOneOf.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum {{classname}}: {{^useClasses}}Sendable, {{/useClasses}}{{#useClasses}}{{#readonlyProperties}}Sendable, {{/readonlyProperties}}{{/useClasses}}{{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable, JSONEncodable{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}}{{/useVapor}} {
    {{#oneOf}}
    case type{{.}}({{.}})
    {{/oneOf}}
    {{#oneOfUnknownDefaultCase}}
    case unknownDefaultOpenApi
    {{/oneOfUnknownDefaultCase}}

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        {{#oneOf}}
        case .type{{.}}(let value):
            try container.encode(value)
        {{/oneOf}}
        {{#oneOfUnknownDefaultCase}}
        case unknownDefaultOpenApi(let type):
            try container.encodeNil()
        {{/oneOfUnknownDefaultCase}}
        }
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        {{#oneOf}}
        {{#-first}}
        if let value = try? container.decode({{.}}.self) {
        {{/-first}}
        {{^-first}}
        } else if let value = try? container.decode({{.}}.self) {
        {{/-first}}
            self = .type{{.}}(value)
        {{/oneOf}}
        } else {
            {{#oneOfUnknownDefaultCase}}
            self = .unknownDefaultOpenApi
            {{/oneOfUnknownDefaultCase}}
            {{^oneOfUnknownDefaultCase}}
            throw DecodingError.typeMismatch(Self.Type.self, .init(codingPath: decoder.codingPath, debugDescription: "Unable to decode instance of {{classname}}"))
            {{/oneOfUnknownDefaultCase}}
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy