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

swift-combine.modelOneOf.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
{{#isDeprecated}}
@available(*, deprecated, message: "Deprecated API parameter")
{{/isDeprecated}}
public enum {{classname}}: Codable {
    {{#oneOf}}
    case type{{.}}({{.}})
    {{/oneOf}}

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

    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 {
            throw DecodingError.typeMismatch(Self.Type.self, .init(codingPath: decoder.codingPath, debugDescription: "Unable to decode instance of {{classname}}"))
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy