Skip to content

Commit

Permalink
Improved ValueDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Brandon Wiley committed Sep 30, 2021
1 parent cd8ba92 commit 388b8e6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build/
Package.resolved
.swiftpm
rethinkdb_data
.DS_Store
Expand Down
25 changes: 0 additions & 25 deletions Package.resolved

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/Symphony/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension Symphony
try FileManager.default.createDirectory(at: seqPath, withIntermediateDirectories: true, attributes: nil)
}

let result = ValueDictionary<K,V>(path: seqPath, relativePath: seqRelPath)
let result = ValueDictionary<K,V>(symphony: self, path: seqPath, relativePath: seqRelPath)
return result
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ extension Symphony
let song = SongDecoder()
let oldType = try song.decode(ValueType.self, from: data)
guard oldType.type == newType else {return nil}
return ValueDictionary<K,V>(path: seqPath, relativePath: seqRelPath)
return ValueDictionary<K,V>(symphony: self, path: seqPath, relativePath: seqRelPath)
}
else
{
Expand Down
66 changes: 28 additions & 38 deletions Sources/Symphony/ValueDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@

import Foundation

public struct ValueDictionary<K,V> where K: Codable, V: Codable
public struct ValueDictionary<K,V> where K: Codable, V: Codable, K: CustomStringConvertible
{
let path: URL
let relativePath: URL
let symphony: Symphony

public init(symphony: Symphony, path: URL, relativePath: URL)
{
self.path = path
self.relativePath = relativePath
self.symphony = symphony
}
}

extension ValueDictionary
Expand Down Expand Up @@ -64,43 +72,25 @@ extension ValueDictionary
// }
//}
//
//extension ValueDictionary: Collection, MutableCollection
//{
// public typealias Index = K
//
// public var startIndex: Index
// {
// return 0
// }
//
// public var endIndex: Index
// {
// return self.count
// }
//
// public subscript(position: Index) -> V
// {
// get
// {
// let filename = position.string
// let valuePath = self.relativePath.appendingPathComponent(filename)
// let result = Symphony.instance.readValue(type: T.self, at: valuePath)
// return result!
// }
//
// set(newValue)
// {
// let filename = position.string
// let valuePath = self.relativePath.appendingPathComponent(filename)
// let _ = Symphony.instance.writeValue(value: newValue, at: valuePath)
// }
// }
//
// public func index(after i: Index) -> Index
// {
// return i + 1
// }
//}

extension ValueDictionary
{
public func get(_ key: K) -> V?
{
let filename = key.description
let valuePath = self.relativePath.appendingPathComponent(filename)
let result = self.symphony.readValue(type: V.self, at: valuePath)
return result
}

public func set(_ key: K, _ value: V) -> Bool
{
let filename = key.description
let valuePath = self.relativePath.appendingPathComponent(filename)
return self.symphony.writeValue(value: value, at: valuePath)
}
}

//
//extension ValueDictionary: BidirectionalCollection
//{
Expand Down

0 comments on commit 388b8e6

Please sign in to comment.