Skip to content

Commit

Permalink
Removed non list in the Feature enum
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoMangiavacchi committed Nov 7, 2019
1 parent 2143d1a commit 77afc64
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 69 deletions.
84 changes: 55 additions & 29 deletions Sources/SwiftTFRecords/Feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import Foundation

public enum Feature {
case Float(_ value: Float)
case Int(_ value: Int)
case Bytes(_ value: Data)
case FloatArray(_ value: [Float])
case IntArray(_ value: [Int])
case BytesArray(_ value: [Data])
Expand All @@ -24,78 +21,107 @@ public enum Feature {
public static func StringArray(_ value: [Swift.String]) -> Self {
return Feature.BytesArray(value.map{ Data(Swift.String("\($0)").utf8) })
}

public func toFloat() -> Float? {

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 .Float(let value):
case .FloatArray(let value):
return value

default:
return nil
}
}

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

default:
return nil
}
}

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

default:
return nil
}
}

public func toFloatArray() -> [Float]? {
public func toString() -> Swift.String? {
switch self {
case .FloatArray(let value):
return value
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 toIntArray() -> [Int]? {
public func toStringArray() -> [Swift.String]? {
switch self {
case .IntArray(let value):
return value
case .BytesArray(let value):
let stringArray = value.compactMap{ Swift.String(bytes: $0, encoding: .utf8) }
return stringArray.isEmpty ? nil : stringArray

default:
return nil
}
}

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

default:
return nil
}
}
public func toString() -> Swift.String? {

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

return nil

default:
return nil
}
}
public func toStringArray() -> [Swift.String]? {

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

default:
return nil
}
Expand Down
36 changes: 3 additions & 33 deletions Sources/SwiftTFRecords/Record.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ public struct Record {
var tfFeature = Tfrecords_Feature()

switch feature {
case let .Float(value):
var list = Tfrecords_FloatList()
list.value = [value]
tfFeature.floatList = list

case let .Int(value):
var list = Tfrecords_Int64List()
list.value = [Int64(value)]
tfFeature.int64List = list

case let .Bytes(value):
var list = Tfrecords_BytesList()
list.value = [value]
tfFeature.bytesList = list

case let .FloatArray(value):
var list = Tfrecords_FloatList()
list.value = value
Expand Down Expand Up @@ -66,32 +51,17 @@ public struct Record {
for (name, feature) in example.features.feature {
switch feature.kind {
case let .floatList(list):
switch list.value.count {
case 0:
break
case 1:
features[name] = Feature.Float(list.value[0])
default:
if !list.value.isEmpty {
features[name] = Feature.FloatArray(list.value)
}

case let .int64List(list):
switch list.value.count {
case 0:
break
case 1:
features[name] = Feature.Int(Int(list.value[0]))
default:
if !list.value.isEmpty {
features[name] = Feature.IntArray(list.value.map { Int($0) })
}

case let .bytesList(list):
switch list.value.count {
case 0:
break
case 1:
features[name] = Feature.Bytes(list.value[0])
default:
if !list.value.isEmpty {
features[name] = Feature.BytesArray(list.value)
}

Expand Down
14 changes: 7 additions & 7 deletions Tests/SwiftTFRecordsTests/FeatureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ final class FeatureTests: XCTestCase {
let feature: Feature = 12.34

XCTAssertEqual(feature.toFloat(), 12.34)
XCTAssertEqual(feature.toFloatArray(), [12.34])
XCTAssertNil(feature.toInt())
XCTAssertNil(feature.toBytes())
XCTAssertNil(feature.toString())
XCTAssertNil(feature.toIntArray())
XCTAssertNil(feature.toFloatArray())
XCTAssertNil(feature.toBytesArray())
XCTAssertNil(feature.toStringArray())
}
Expand All @@ -19,10 +19,10 @@ final class FeatureTests: XCTestCase {
let feature: Feature = 17

XCTAssertEqual(feature.toInt(), 17)
XCTAssertEqual(feature.toIntArray(), [17])
XCTAssertNil(feature.toFloat())
XCTAssertNil(feature.toBytes())
XCTAssertNil(feature.toString())
XCTAssertNil(feature.toIntArray())
XCTAssertNil(feature.toFloatArray())
XCTAssertNil(feature.toBytesArray())
XCTAssertNil(feature.toStringArray())
Expand All @@ -32,39 +32,39 @@ final class FeatureTests: XCTestCase {
let feature: Feature = Feature.Bytes(Data([0, 202, 255, 44, 5]))

XCTAssertEqual(feature.toBytes(), Data([0, 202, 255, 44, 5]))
XCTAssertEqual(feature.toBytesArray(), [Data([0, 202, 255, 44, 5])])
XCTAssertNil(feature.toInt())
XCTAssertNil(feature.toFloat())
XCTAssertNil(feature.toString())
XCTAssertNil(feature.toIntArray())
XCTAssertNil(feature.toFloatArray())
XCTAssertNil(feature.toBytesArray())
XCTAssertNil(feature.toStringArray())
}

func testString() {
let feature: Feature = "Jacopo πŸ˜ƒ"

XCTAssertEqual(feature.toString(), "Jacopo πŸ˜ƒ")
XCTAssertEqual(feature.toStringArray(), ["Jacopo πŸ˜ƒ"])
XCTAssertEqual(feature.toBytes(), Data([74, 97, 99, 111, 112, 111, 32, 240, 159, 152, 131]))
XCTAssertEqual(feature.toBytesArray(), [Data([74, 97, 99, 111, 112, 111, 32, 240, 159, 152, 131])])
XCTAssertNil(feature.toInt())
XCTAssertNil(feature.toFloat())
XCTAssertNil(feature.toIntArray())
XCTAssertNil(feature.toFloatArray())
XCTAssertNil(feature.toBytesArray())
XCTAssertNil(feature.toStringArray())
}

func testString2() {
let feature: Feature = Feature.String("Jacopo πŸ˜ƒ")

XCTAssertEqual(feature.toString(), "Jacopo πŸ˜ƒ")
XCTAssertEqual(feature.toStringArray(), ["Jacopo πŸ˜ƒ"])
XCTAssertEqual(feature.toBytes(), Data([74, 97, 99, 111, 112, 111, 32, 240, 159, 152, 131]))
XCTAssertEqual(feature.toBytesArray(), [Data([74, 97, 99, 111, 112, 111, 32, 240, 159, 152, 131])])
XCTAssertNil(feature.toInt())
XCTAssertNil(feature.toFloat())
XCTAssertNil(feature.toIntArray())
XCTAssertNil(feature.toFloatArray())
XCTAssertNil(feature.toBytesArray())
XCTAssertNil(feature.toStringArray())
}

func testFloatArray() {
Expand Down

0 comments on commit 77afc64

Please sign in to comment.