Skip to content

Commit

Permalink
Renamed context -> features
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoMangiavacchi committed Nov 6, 2019
1 parent e85f59b commit cf80bde
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
22 changes: 11 additions & 11 deletions Sources/SwiftTFRecords/Record.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import Foundation

public struct Record {
public var context: [String : Feature]
public var features: [String : Feature]

public var data: Data? {
var example = Tfrecords_Example()

for (name, feature) in context {
for (name, feature) in features {
var tfFeature = Tfrecords_Feature()

switch feature {
Expand Down Expand Up @@ -50,37 +50,37 @@ public struct Record {
}

public init() {
self.context = [String : Feature]()
self.features = [String : Feature]()
}

public init(withData data: Data) {
self.context = [String : Feature]()
self.features = [String : Feature]()

guard let example = try? Tfrecords_Example(serializedData: data) else { return }

for (name, feature) in example.features.feature {
switch feature.kind {
case let .bytesList(list):
if !list.value.isEmpty {
context[name] = Feature.Bytes(list.value[0])
features[name] = Feature.Bytes(list.value[0])
}
case let .floatList(list):
switch list.value.count {
case 0:
break
case 1:
context[name] = Feature.Float(list.value[0])
features[name] = Feature.Float(list.value[0])
default:
context[name] = Feature.FloatArray(list.value)
features[name] = Feature.FloatArray(list.value)
}
case let .int64List(list):
switch list.value.count {
case 0:
break
case 1:
context[name] = Feature.Int(Int(list.value[0]))
features[name] = Feature.Int(Int(list.value[0]))
default:
context[name] = Feature.IntArray(list.value.map { Int($0) })
features[name] = Feature.IntArray(list.value.map { Int($0) })
}
if !list.value.isEmpty {
}
Expand All @@ -91,10 +91,10 @@ public struct Record {
}

public mutating func set(_ name: String, feature: Feature?) {
context[name] = feature
features[name] = feature
}

public func get(_ name: String) -> Feature? {
return context[name]
return features[name]
}
}
24 changes: 12 additions & 12 deletions Tests/SwiftTFRecordsTests/TFRecordsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ final class TFRecordsTests: XCTestCase {

XCTAssertEqual(tfRecordOut.records.count, 2)

XCTAssertEqual(tfRecordOut.records[0].context["Int"]?.toInt(), 1)
XCTAssertEqual(tfRecordOut.records[0].context["Float"]?.toFloat(), 2.3)
XCTAssertEqual(tfRecordOut.records[0].context["Bytes"]?.toBytes(), Data([1, 2, 3, 4]))
XCTAssertEqual(tfRecordOut.records[0].context["String"]?.toString(), "Jacopo πŸ˜ƒ")
XCTAssertEqual(tfRecordOut.records[0].context["IntArray"]?.toIntArray(), [1, 2, 3, 4])
XCTAssertEqual(tfRecordOut.records[0].context["FloatArray"]?.toFloatArray(), [2.1, 2.2, 2.3])
XCTAssertEqual(tfRecordOut.records[0].features["Int"]?.toInt(), 1)
XCTAssertEqual(tfRecordOut.records[0].features["Float"]?.toFloat(), 2.3)
XCTAssertEqual(tfRecordOut.records[0].features["Bytes"]?.toBytes(), Data([1, 2, 3, 4]))
XCTAssertEqual(tfRecordOut.records[0].features["String"]?.toString(), "Jacopo πŸ˜ƒ")
XCTAssertEqual(tfRecordOut.records[0].features["IntArray"]?.toIntArray(), [1, 2, 3, 4])
XCTAssertEqual(tfRecordOut.records[0].features["FloatArray"]?.toFloatArray(), [2.1, 2.2, 2.3])

XCTAssertEqual(tfRecordOut.records[1].context["Int"]?.toInt(), 2)
XCTAssertEqual(tfRecordOut.records[1].context["Float"]?.toFloat(), 4.6)
XCTAssertEqual(tfRecordOut.records[1].context["Bytes"]?.toBytes(), Data([5, 6, 7, 8]))
XCTAssertEqual(tfRecordOut.records[1].context["String"]?.toString(), "Jacopo πŸ˜ƒπŸ˜ƒ")
XCTAssertEqual(tfRecordOut.records[1].context["IntArray"]?.toIntArray(), [1, 2, 3, 4])
XCTAssertEqual(tfRecordOut.records[1].context["FloatArray"]?.toFloatArray(), [2.1, 2.2, 2.3])
XCTAssertEqual(tfRecordOut.records[1].features["Int"]?.toInt(), 2)
XCTAssertEqual(tfRecordOut.records[1].features["Float"]?.toFloat(), 4.6)
XCTAssertEqual(tfRecordOut.records[1].features["Bytes"]?.toBytes(), Data([5, 6, 7, 8]))
XCTAssertEqual(tfRecordOut.records[1].features["String"]?.toString(), "Jacopo πŸ˜ƒπŸ˜ƒ")
XCTAssertEqual(tfRecordOut.records[1].features["IntArray"]?.toIntArray(), [1, 2, 3, 4])
XCTAssertEqual(tfRecordOut.records[1].features["FloatArray"]?.toFloatArray(), [2.1, 2.2, 2.3])
}

static var allTests = [
Expand Down

0 comments on commit cf80bde

Please sign in to comment.