Skip to content

Commit

Permalink
Separate feature enum extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoMangiavacchi committed Nov 7, 2019
1 parent 77afc64 commit fb6b599
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 146 deletions.
146 changes: 0 additions & 146 deletions Sources/SwiftTFRecords/Feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,150 +11,4 @@ public enum Feature {
case FloatArray(_ value: [Float])
case IntArray(_ value: [Int])
case BytesArray(_ value: [Data])

// Not possible to define String as enum case as TFRecords protobut only manage generic Bytes (Data)
public static func String(_ value: Swift.String) -> Self {
return Feature.Bytes(Data(Swift.String("\(value)").utf8))
}

// Not possible to define StringArray as enum case as TFRecords protobut only manage generic Bytes (Data)
public static func StringArray(_ value: [Swift.String]) -> Self {
return Feature.BytesArray(value.map{ Data(Swift.String("\($0)").utf8) })
}

public static func Float(_ value: Swift.Float) -> Self {
return Feature.FloatArray([value])
}

public static func Int(_ value: Swift.Int) -> Self {
return Feature.IntArray([value])
}

public static func Bytes(_ value: Data) -> Self {
return Feature.BytesArray([value])
}

public func toFloatArray() -> [Float]? {
switch self {
case .FloatArray(let value):
return value

default:
return nil
}
}

public func toIntArray() -> [Int]? {
switch self {
case .IntArray(let value):
return value

default:
return nil
}
}

public func toBytesArray() -> [Data]? {
switch self {
case .BytesArray(let value):
return value

default:
return nil
}
}

public func toString() -> Swift.String? {
switch self {
case .BytesArray(let value):
if value.count == 1, let string = Swift.String(bytes: value[0], encoding: .utf8) {
return string
}
return nil

default:
return nil
}
}

public func toStringArray() -> [Swift.String]? {
switch self {
case .BytesArray(let value):
let stringArray = value.compactMap{ Swift.String(bytes: $0, encoding: .utf8) }
return stringArray.isEmpty ? nil : stringArray

default:
return nil
}
}

public func toFloat() -> Float? {
switch self {
case .FloatArray(let value):
if value.count == 1 {
return value[0]
}
return nil

default:
return nil
}
}

public func toInt() -> Int? {
switch self {
case .IntArray(let value):
if value.count == 1 {
return value[0]
}
return nil

default:
return nil
}
}

public func toBytes() -> Data? {
switch self {
case .BytesArray(let value):
if value.count == 1 {
return value[0]
}
return nil

default:
return nil
}
}
}

// Utility Initializers for basic Literal types
extension Feature: ExpressibleByFloatLiteral {
public init(floatLiteral: FloatLiteralType) {
self = Feature.Float(Swift.Float(floatLiteral))
}
}

extension Feature: ExpressibleByIntegerLiteral {
public init(integerLiteral: IntegerLiteralType) {
self = Feature.Int(integerLiteral)
}
}

extension Feature: ExpressibleByStringLiteral {
// By using 'StaticString' we disable string interpolation, for safety
public init(stringLiteral value: StaticString) {
self = Feature.String(Swift.String("\(value)"))
}
}

extension Feature: ExpressibleByArrayLiteral {
// Default to Float Array
public init(arrayLiteral elements: Float...) {
self = Feature.FloatArray(elements)
}

// public init(arrayLiteral elements: Int...) {
// self = Feature.IntArray(elements)
// }
}
167 changes: 167 additions & 0 deletions Sources/SwiftTFRecords/FeatureExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//
// Feature.swift
//
//
// Created by Jacopo Mangiavacchi on 11/6/19.
//

import Foundation

// Easy cast
extension Feature {
public func toFloatArray() -> [Float]? {
switch self {
case .FloatArray(let value):
return value

default:
return nil
}
}

public func toIntArray() -> [Int]? {
switch self {
case .IntArray(let value):
return value

default:
return nil
}
}

public func toBytesArray() -> [Data]? {
switch self {
case .BytesArray(let value):
return value

default:
return nil
}
}
}

//String support
extension Feature {
// Not possible to define String as enum case as TFRecords protobut only manage generic Bytes (Data)
public static func String(_ value: Swift.String) -> Self {
return Feature.Bytes(Data(Swift.String("\(value)").utf8))
}

// Not possible to define StringArray as enum case as TFRecords protobut only manage generic Bytes (Data)
public static func StringArray(_ value: [Swift.String]) -> Self {
return Feature.BytesArray(value.map{ Data(Swift.String("\($0)").utf8) })
}

public func toString() -> Swift.String? {
switch self {
case .BytesArray(let value):
if value.count == 1, let string = Swift.String(bytes: value[0], encoding: .utf8) {
return string
}
return nil

default:
return nil
}
}

public func toStringArray() -> [Swift.String]? {
switch self {
case .BytesArray(let value):
let stringArray = value.compactMap{ Swift.String(bytes: $0, encoding: .utf8) }
return stringArray.isEmpty ? nil : stringArray

default:
return nil
}
}
}

// Single instance support
extension Feature {
public static func Float(_ value: Swift.Float) -> Self {
return Feature.FloatArray([value])
}

public static func Int(_ value: Swift.Int) -> Self {
return Feature.IntArray([value])
}

public static func Bytes(_ value: Data) -> Self {
return Feature.BytesArray([value])
}

public func toFloat() -> Float? {
switch self {
case .FloatArray(let value):
if value.count == 1 {
return value[0]
}
return nil

default:
return nil
}
}

public func toInt() -> Int? {
switch self {
case .IntArray(let value):
if value.count == 1 {
return value[0]
}
return nil

default:
return nil
}
}

public func toBytes() -> Data? {
switch self {
case .BytesArray(let value):
if value.count == 1 {
return value[0]
}
return nil

default:
return nil
}
}
}

// Float Literal support
extension Feature: ExpressibleByFloatLiteral {
public init(floatLiteral: FloatLiteralType) {
self = Feature.Float(Swift.Float(floatLiteral))
}
}

// Int Literal support
extension Feature: ExpressibleByIntegerLiteral {
public init(integerLiteral: IntegerLiteralType) {
self = Feature.Int(integerLiteral)
}
}

// String Literal support
extension Feature: ExpressibleByStringLiteral {
// By using 'StaticString' we disable string interpolation, for safety
public init(stringLiteral value: StaticString) {
self = Feature.String(Swift.String("\(value)"))
}
}

// Float Array Literal support
extension Feature: ExpressibleByArrayLiteral {
// Default to Float Array
public init(arrayLiteral elements: Float...) {
self = Feature.FloatArray(elements)
}

// public init(arrayLiteral elements: Int...) {
// self = Feature.IntArray(elements)
// }
}

0 comments on commit fb6b599

Please sign in to comment.