-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
201 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
27 changes: 0 additions & 27 deletions
27
...Configuration.xcodeproj/xcuserdata/kyouhei.xcuserdatad/xcschemes/xcschememanagement.plist
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// Configuration.swift | ||
// SwiftyConfiguration | ||
// | ||
// Created by kyo__hei on 2016/07/05. | ||
// Copyright © 2016年 kyo__hei. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol PlistValueType {} | ||
|
||
extension String: PlistValueType {} | ||
extension NSNumber: PlistValueType {} | ||
extension Int: PlistValueType {} | ||
extension Float: PlistValueType {} | ||
extension Double: PlistValueType {} | ||
extension Bool: PlistValueType {} | ||
extension NSDate: PlistValueType {} | ||
extension NSData: PlistValueType {} | ||
extension Array: PlistValueType {} | ||
extension Dictionary: PlistValueType {} | ||
|
||
public class Keys {} | ||
|
||
public class Key<ValueType: PlistValueType>: Keys { | ||
|
||
public let key: String | ||
|
||
internal var separatedKeys: [String] { | ||
return key.componentsSeparatedByString(".") | ||
} | ||
|
||
public init(_ key: String) { | ||
self.key = key | ||
} | ||
|
||
} | ||
|
||
|
||
public struct Configuration { | ||
|
||
private let dictionary: NSDictionary | ||
|
||
public init?(plistPath: String) { | ||
guard let plist = NSDictionary(contentsOfFile: plistPath) else { | ||
assertionFailure("could not read plist file.") | ||
return nil | ||
} | ||
dictionary = plist | ||
} | ||
|
||
public func get<T>(key: Key<T>) -> T? { | ||
var object: AnyObject = dictionary | ||
|
||
key.separatedKeys.enumerate().forEach { idx, separatedKey in | ||
if let index = Int(separatedKey) { | ||
let array = object as! Array<AnyObject> | ||
object = array[index] | ||
} else { | ||
let dictionary = object as! NSDictionary | ||
object = dictionary[separatedKey]! | ||
} | ||
} | ||
|
||
let optionalValue: T? | ||
|
||
switch T.self { | ||
case is Int.Type: optionalValue = object.integerValue as? T | ||
case is Float.Type: optionalValue = object.floatValue as? T | ||
case is Double.Type: optionalValue = object.doubleValue as? T | ||
case is NSURL.Type: optionalValue = NSURL(string: (object as? String) ?? "") as? T | ||
default: optionalValue = object as? T | ||
} | ||
|
||
guard let value = optionalValue else { | ||
assertionFailure("Could not cast value of type \(object.dynamicType) to \(T.self)") | ||
return nil | ||
} | ||
|
||
return value | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>dictionary</key> | ||
<dict> | ||
<key>innerString</key> | ||
<string>moge</string> | ||
</dict> | ||
<key>array</key> | ||
<array> | ||
<integer>0</integer> | ||
<array> | ||
<string>array.1.0</string> | ||
</array> | ||
</array> | ||
<key>string</key> | ||
<string>hoge</string> | ||
<key>int</key> | ||
<integer>1</integer> | ||
<key>float</key> | ||
<real>1.1</real> | ||
<key>double</key> | ||
<string>3.14</string> | ||
<key>bool</key> | ||
<true/> | ||
<key>date</key> | ||
<date>1970-01-01T00:00:00Z</date> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// SwiftyConfigurationTests.swift | ||
// SwiftyConfigurationTests | ||
// | ||
// Created by kyo__hei on 2016/07/05. | ||
// Copyright © 2016年 kyo__hei. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftyConfiguration | ||
|
||
private extension Keys { | ||
|
||
static let string = Key<String>("string") | ||
static let int = Key<Int>("int") | ||
static let float = Key<Float>("float") | ||
static let double = Key<Double>("double") | ||
static let date = Key<NSDate>("date") | ||
static let bool = Key<Bool>("bool") | ||
|
||
static let array = Key<Array<NSObject>>("array") | ||
static let innerInt = Key<Int>("array.0") | ||
static let innerArray = Key<String>("array.1.0") | ||
|
||
static let dictionary = Key<[String : String]>("dictionary") | ||
static let innerString = Key<String>("dictionary.innerString") | ||
} | ||
|
||
class SwiftyConfigurationTests: XCTestCase { | ||
|
||
private lazy var plistPath: String = { | ||
return NSBundle(forClass: self.dynamicType).pathForResource("Configuration", ofType: "plist")! | ||
}() | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
} | ||
|
||
func testGetValue() { | ||
let config = Configuration(plistPath: plistPath)! | ||
|
||
XCTAssertTrue("hoge" == config.get(.string)!) | ||
XCTAssertTrue(1 == config.get(.int)!) | ||
XCTAssertTrue(1.1 == config.get(.float)!) | ||
XCTAssertTrue(3.14 == config.get(.double)!) | ||
XCTAssertTrue(config.get(.bool)!) | ||
XCTAssertTrue(NSDate(timeIntervalSince1970: 0).timeIntervalSince1970 == config.get(.date)!.timeIntervalSince1970) | ||
|
||
XCTAssertTrue([0,["array.1.0"]] == config.get(.array)!) | ||
XCTAssertTrue(0 == config.get(.innerInt)!) | ||
XCTAssertTrue("array.1.0" == config.get(.innerArray)!) | ||
|
||
XCTAssertTrue(["innerString" : "moge"] == config.get(.dictionary)!) | ||
XCTAssertTrue("moge" == config.get(.innerString)!) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.