Transformable.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // Transformable.swift
  3. // HandyJSON
  4. //
  5. // Created by zhouzhuo on 15/07/2017.
  6. // Copyright © 2017 aliyun. All rights reserved.
  7. //
  8. import Foundation
  9. public protocol _Transformable: _Measurable {}
  10. extension _Transformable {
  11. static func transform(from object: Any) -> Self? {
  12. if let typedObject = object as? Self {
  13. return typedObject
  14. }
  15. switch self {
  16. case let type as _ExtendCustomBasicType.Type:
  17. return type._transform(from: object) as? Self
  18. case let type as _BuiltInBridgeType.Type:
  19. return type._transform(from: object) as? Self
  20. case let type as _BuiltInBasicType.Type:
  21. return type._transform(from: object) as? Self
  22. case let type as _RawEnumProtocol.Type:
  23. return type._transform(from: object) as? Self
  24. case let type as _ExtendCustomModelType.Type:
  25. return type._transform(from: object) as? Self
  26. default:
  27. return nil
  28. }
  29. }
  30. func plainValue() -> Any? {
  31. switch self {
  32. case let rawValue as _ExtendCustomBasicType:
  33. return rawValue._plainValue()
  34. case let rawValue as _BuiltInBridgeType:
  35. return rawValue._plainValue()
  36. case let rawValue as _BuiltInBasicType:
  37. return rawValue._plainValue()
  38. case let rawValue as _RawEnumProtocol:
  39. return rawValue._plainValue()
  40. case let rawValue as _ExtendCustomModelType:
  41. return rawValue._plainValue()
  42. default:
  43. return nil
  44. }
  45. }
  46. }