Quellcode durchsuchen

Rename `fields` to `properties`.

Add convenient accessors.
maxep vor 6 Jahren
Ursprung
Commit
ad1156f91b
3 geänderte Dateien mit 74 neuen und 20 gelöschten Zeilen
  1. 17 3
      Sources/KDB/Entry.swift
  2. 34 3
      Sources/KDB/Group.swift
  3. 23 14
      Sources/KDB/Row.swift

+ 17 - 3
Sources/KDB/Entry.swift

@@ -64,22 +64,36 @@ public final class Entry: Row, Streamable {
 
     var parent: Group?
 
-    public var fields: [Field<Type>]
+    public var properties: [Property<Type>]
 
     public required init() {
-        fields = []
+        properties = []
     }    
 }
 
 extension Entry {
 
+    public var creationDate: Date {
+        date(at: .creationTime) ?? Date.distantPast
+    }
+
+    public var lastModifiedDate: Date {
+        get { date(at: .lastModifiedTime) ?? Date.distantPast }
+        set { set(newValue, at: .lastModifiedTime) }
+    }
+
+    public var lastAccessDate: Date {
+        get { date(at: .lastAccessTime) ?? Date.distantPast }
+        set { set(newValue, at: .lastAccessTime) }
+    }
+
     var isMetaEntry: Bool {
         return false
     }
 
     public func removeFromParent() {
         parent?.entries.removeAll(where: { $0 == self })
-        fields.removeAll(.groupID)
+        self[.groupID] = -1
     }
 }
 

+ 34 - 3
Sources/KDB/Group.swift

@@ -39,14 +39,14 @@ public final class Group: Row, Streamable {
 
     var parent: Group?
 
-    public var fields: [Field<Type>]
+    public var properties: [Property<Type>]
 
     public var childs: [Group]
 
     public var entries: [Entry]
 
     public required init() {
-        fields = []
+        properties = []
         childs = []
         entries = []
     }
@@ -54,6 +54,35 @@ public final class Group: Row, Streamable {
 
 extension Group {
 
+    public var name: String {
+        get { self[.name] ?? "" }
+        set { self[.name] = newValue }
+    }
+
+    public var level: Int {
+        get { self[.groupLevel] ?? 0 }
+        set { self[.groupLevel] = newValue }
+    }
+
+    public var icon: Int {
+        get { self[.iconID] ?? 0 }
+        set { self[.iconID] = newValue }
+    }
+
+    public var creationDate: Date {
+        date(at: .creationTime) ?? Date.distantPast
+    }
+
+    public var lastModifiedDate: Date {
+        get { date(at: .lastModifiedTime) ?? Date.distantPast }
+        set { set(newValue, at: .lastModifiedTime) }
+    }
+
+    public var lastAccessDate: Date {
+        get { date(at: .lastAccessTime) ?? Date.distantPast }
+        set { set(newValue, at: .lastAccessTime) }
+    }
+
     public func removeFromParent() {
         parent?.childs.removeAll(where: { $0 == self })
     }
@@ -67,6 +96,7 @@ extension Group {
     public func add(_ group: Group) {
         group.removeFromParent()
         childs.append(group)
+        group.level = level + 1
     }
 }
 
@@ -78,8 +108,9 @@ extension Group: Hashable {
     }
 
     public func hash(into hasher: inout Hasher) {
+        if let groupID = self[.groupID] { hasher.combine(groupID) }
+        if let name = self[.name] { hasher.combine(name) }
         if let groupLevel = self[.groupLevel] { hasher.combine(groupLevel) }
-        if let groupFlags = self[.groupFlags] { hasher.combine(groupFlags) }
     }
 
 }

+ 23 - 14
Sources/KDB/Row.swift

@@ -19,7 +19,7 @@
 import Foundation
 import Binary
 
-public typealias Field<Type> = TLV<Type, UInt32>
+public typealias Property<Type> = TLV<Type, UInt32>
 
 public protocol Row: class {
 
@@ -27,7 +27,7 @@ public protocol Row: class {
 
     static var End: Type { get }
 
-    var fields: [Field<Type>] { get set }
+    var properties: [Property<Type>] { get set }
 
     init()
 }
@@ -35,18 +35,27 @@ public protocol Row: class {
 extension Row {
 
     public subscript(_ type: Type) -> Bytes? {
-        get { fields.first(where: { $0.type == type })?.value }
+        get { properties.first(where: { $0.type == type })?.value }
         set {
-            fields.removeAll(where: { $0.type == type })
+            properties.removeAll(type)
             guard let value = newValue else { return }
-            let tlv = Field(type: type, value: value)
-            fields.insert(tlv, at: 0)
+            let tlv = Property(type: type, value: value)
+            properties.insert(tlv, at: 0)
         }
     }
 
-    public func set(_ field: Field<Type>) {
-        fields.removeAll(where: { $0.type == field.type })
-        fields.insert(field, at: 0)
+    public func set(_ field: Property<Type>) {
+        properties.removeAll(field.type)
+        properties.insert(field, at: 0)
+    }
+
+    public func set(_ date: Date, at type: Type) {
+        self[type] = Database.bytes(from: date)
+    }
+
+    public func date(at type: Type) -> Date? {
+        guard let bytes = self[type] else { return nil }
+        return Database.date(from: bytes)
     }
 
     public subscript<T>(_ type: Type) -> T? where T: BytesRepresentable {
@@ -58,7 +67,7 @@ extension Row {
     }
 
     public func remove(_ type: Type) {
-        fields.removeAll(where: { $0.type == type })
+        properties.removeAll(type)
     }
 }
 
@@ -67,9 +76,9 @@ extension Readable where Self: Row {
     public init(from input: Input) throws {
         self.init()
         while true {
-            let field = try input.read() as Field<Type>
+            let field = try input.read() as Property<Type>
             guard field.type != Self.End else { break }
-            fields.append(field)
+            properties.append(field)
         }
     }
 
@@ -78,8 +87,8 @@ extension Readable where Self: Row {
 extension Writable where Self: Row {
 
     public func write(to output: Output) throws {
-        try output.write(fields)
-        let end = Field(type: Self.End, value: [])
+        try output.write(properties)
+        let end = Property(type: Self.End, value: [])
         try output.write(end)
     }