From 1a99fea820eec3a429eeb91d8b536c39354dbbf2 Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Sat, 17 Jun 2017 01:55:36 +0900 Subject: [PATCH] complete PartialObject utilities --- Sources/PartialObject.swift | 126 ++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/Sources/PartialObject.swift b/Sources/PartialObject.swift index 3f3f90d..b103d21 100644 --- a/Sources/PartialObject.swift +++ b/Sources/PartialObject.swift @@ -36,6 +36,9 @@ public struct PartialObject { return O.cs_fromRaw(object: self.rawObject) } + + // MARK: Value.Required utilities + public func value(for property: (O) -> ValueContainer.Required) -> V { return V.cs_fromImportableNativeType( @@ -43,22 +46,6 @@ public struct PartialObject { )! } - public func value(for property: (O) -> ValueContainer.Optional) -> V? { - - return (self.rawObject.value(forKey: property(O.meta).keyPath) as! V.ImportableNativeType?) - .flatMap(V.cs_fromImportableNativeType) - } - - public func value(for property: (O) -> TransformableContainer.Required) -> V { - - return self.rawObject.value(forKey: property(O.meta).keyPath)! as! V - } - - public func value(for property: (O) -> TransformableContainer.Optional) -> V? { - - return self.rawObject.value(forKey: property(O.meta).keyPath) as! V? - } - public func setValue(_ value: V, for property: (O) -> ValueContainer.Required) { self.rawObject.setValue( @@ -67,51 +54,11 @@ public struct PartialObject { ) } - public func setValue(_ value: V?, for property: (O) -> ValueContainer.Optional) { - - self.rawObject.setValue( - value?.cs_toImportableNativeType(), - forKey: property(O.meta).keyPath - ) - } - - public func setValue(_ value: V, for property: (O) -> TransformableContainer.Required) { - - self.rawObject.setValue( - value, - forKey: property(O.meta).keyPath - ) - } - - public func setValue(_ value: V?, for property: (O) -> TransformableContainer.Optional) { - - self.rawObject.setValue( - value, - forKey: property(O.meta).keyPath - ) - } - public func primitiveValue(for property: (O) -> ValueContainer.Required) -> V { return V.cs_fromImportableNativeType( self.rawObject.primitiveValue(forKey: property(O.meta).keyPath)! as! V.ImportableNativeType - )! - } - - public func primitiveValue(for property: (O) -> ValueContainer.Optional) -> V? { - - return (self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) as! V.ImportableNativeType?) - .flatMap(V.cs_fromImportableNativeType) - } - - public func primitiveValue(for property: (O) -> TransformableContainer.Required) -> V { - - return self.rawObject.primitiveValue(forKey: property(O.meta).keyPath)! as! V - } - - public func primitiveValue(for property: (O) -> TransformableContainer.Optional) -> V? { - - return self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) as! V? + )! } public func setPrimitiveValue(_ value: V, for property: (O) -> ValueContainer.Required) { @@ -122,6 +69,29 @@ public struct PartialObject { ) } + + // MARK: Value.Optional utilities + + public func value(for property: (O) -> ValueContainer.Optional) -> V? { + + return (self.rawObject.value(forKey: property(O.meta).keyPath) as! V.ImportableNativeType?) + .flatMap(V.cs_fromImportableNativeType) + } + + public func setValue(_ value: V?, for property: (O) -> ValueContainer.Optional) { + + self.rawObject.setValue( + value?.cs_toImportableNativeType(), + forKey: property(O.meta).keyPath + ) + } + + public func primitiveValue(for property: (O) -> ValueContainer.Optional) -> V? { + + return (self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) as! V.ImportableNativeType?) + .flatMap(V.cs_fromImportableNativeType) + } + public func setPrimitiveValue(_ value: V?, for property: (O) -> ValueContainer.Optional) { self.rawObject.setPrimitiveValue( @@ -130,6 +100,27 @@ public struct PartialObject { ) } + + // MARK: Transformable.Required utilities + + public func value(for property: (O) -> TransformableContainer.Required) -> V { + + return self.rawObject.value(forKey: property(O.meta).keyPath)! as! V + } + + public func setValue(_ value: V, for property: (O) -> TransformableContainer.Required) { + + self.rawObject.setValue( + value, + forKey: property(O.meta).keyPath + ) + } + + public func primitiveValue(for property: (O) -> TransformableContainer.Required) -> V { + + return self.rawObject.primitiveValue(forKey: property(O.meta).keyPath)! as! V + } + public func setPrimitiveValue(_ value: V, for property: (O) -> TransformableContainer.Required) { self.rawObject.setPrimitiveValue( @@ -138,6 +129,27 @@ public struct PartialObject { ) } + + // MARK: Transformable.Optional utilities + + public func value(for property: (O) -> TransformableContainer.Optional) -> V? { + + return self.rawObject.value(forKey: property(O.meta).keyPath) as! V? + } + + public func setValue(_ value: V?, for property: (O) -> TransformableContainer.Optional) { + + self.rawObject.setValue( + value, + forKey: property(O.meta).keyPath + ) + } + + public func primitiveValue(for property: (O) -> TransformableContainer.Optional) -> V? { + + return self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) as! V? + } + public func setPrimitiveValue(_ value: V?, for property: (O) -> TransformableContainer.Optional) { self.rawObject.setPrimitiveValue(