Skip to content

Instantly share code, notes, and snippets.

@loihd
Forked from amrangry/ValueWrapper.swift
Created August 16, 2020 14:31
Show Gist options
  • Save loihd/296cfebe0f3995a579e5a8feaaf27b6e to your computer and use it in GitHub Desktop.
Save loihd/296cfebe0f3995a579e5a8feaaf27b6e to your computer and use it in GitHub Desktop.
//
// ValueWrapper.swift
// ADKATech.com
//
// Created by Amr Elghadban on 9/20/18.
// Copyright © 2018 Mobile DevOps. All rights reserved.
//
import Foundation
enum ValueWrapper: Codable {
case stringValue(String)
case intValue(Int)
case doubleValue(Double)
case boolValue(Bool)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(String.self) {
self = .stringValue(value)
return
}
if let value = try? container.decode(Bool.self) {
self = .boolValue(value)
return
}
if let value = try? container.decode(Double.self) {
self = .doubleValue(value)
return
}
if let value = try? container.decode(Int.self) {
self = .intValue(value)
return
}
throw DecodingError.typeMismatch(ValueWrapper.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ValueWrapper"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .stringValue(value):
try container.encode(value)
case let .boolValue(value):
try container.encode(value)
case let .intValue(value):
try container.encode(value)
case let .doubleValue(value):
try container.encode(value)
}
}
var rawValue: String {
var result: String
switch self {
case let .stringValue(value):
result = value
case let .boolValue(value):
result = String(value)
case let .intValue(value):
result = String(value)
case let .doubleValue(value):
result = String(value)
}
return result
}
var intValue: Int? {
var result: Int?
switch self {
case let .stringValue(value):
result = Int(value)
case let .intValue(value):
result = value
case let .boolValue(value):
result = value ? 1 : 0
case let .doubleValue(value):
result = Int(value)
}
return result
}
var boolValue: Bool? {
var result: Bool?
switch self {
case let .stringValue(value):
result = Bool(value)
case let .boolValue(value):
result = value
case let .intValue(value):
result = Bool(truncating: value as NSNumber)
case let .doubleValue(value):
result = Bool(truncating: value as NSNumber)
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment