Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SwiftLint Errors #9

Closed
wants to merge 29 commits into from
Closed

Fix SwiftLint Errors #9

wants to merge 29 commits into from

Conversation

ShawnBaek
Copy link
Contributor

@ShawnBaek ShawnBaek commented Dec 30, 2018

I changed the shortest names like an r, t, and e to fix SwiftLint Errors.

I fixed deprecated warnings.

CocoaPod

This Repo doesn't have any version tag. So We need to add version tag.

screen shot 2018-12-31 at 7 38 58 pm

Considering the Test Cases for [Any]

I think subscript for [Any] is not allowed in Parse-Swift

Because of the generic type of T is not a Codable in Swift4.

I don't want to change set and get functions in KeychainStore.

How can we re-write testSetComplextObject?

I need some advice from reviewers

func testSetComplextObject() {
        let complexObject: [Any]? = [["key": "value"], "string2", 1234, NSNull()]
        testStore["complexObject"] = complexObject
        guard let retrievedObject: [Any] = testStore["complexObject"] else {
            return XCTFail("Should retrieve the object")
        }

        XCTAssertTrue(retrievedObject.count == 4)
        retrievedObject.enumerated().forEach { (offset, retrievedValue) in
            let value = complexObject[offset]
            switch offset {
            case 0:
                guard let dict = value as? [String: String],
                    let retrivedDict = retrievedValue as? [String: String] else {
                        return XCTFail("Should be both dictionaries")
                }
                XCTAssertTrue(dict["key"] == retrivedDict["key"])
            case 1:
                guard let string = value as? String,
                    let retrievedString = retrievedValue as? String else {
                        return XCTFail("Should be both strings")
                }
                XCTAssertTrue(string == retrievedString)
            case 2:
                guard let int = value as? Int,
                    let retrievedInt = retrievedValue as? Int else {
                        return XCTFail("Should be both ints")
                }
                XCTAssertTrue(int == retrievedInt)
            case 3:
                guard let retrieved = retrievedValue as? NSNull else {
                        return XCTFail("Should be both ints")
                }
                XCTAssertTrue(retrieved == NSNull())
            default: break
            }
        }
    }

Copy link
Contributor

@flovilmart flovilmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you address the comments please? I’d like to keep the setters non discardable to force the call sites to not ignore if setting a value failed

Sources/ParseSwift/Storage/SecureStorage.swift Outdated Show resolved Hide resolved
@@ -45,7 +44,16 @@ struct KeychainStore: SecureStorage {
return NSKeyedUnarchiver.unarchiveObject(with: data) as? T
}

func set<T>(object: T?, forKey key: String) -> Bool where T: Encodable {
func object<T>(forKey key: String) -> [T]? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding this implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one subscript and two functions to set and get an [Any] to subscript in KeychainStore for pass testSetComplextObject Test Cases. But I would love to get your feedback. Thank you

subscript<T>(key: String) -> [T]? 
func object<T>(forKey key: String) -> [T]?
func set<T>(object: [T]?, forKey key: String) -> Bool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no need for that, as a T can be an [U] if you specify it so.

Copy link
Contributor Author

@ShawnBaek ShawnBaek Jan 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for advice me. I removed these implement.

I got an error message below

testStore["complexObject"] = complexObject //Cannot assign value of type '[Any]' to type '_?'
guard let retrievedObject: [Any] = testStore["complexObject"] else {
    return XCTFail("Should retrieve the object")
} //Value of optional type 'KeychainStore?' must be unwrapped to refer to member 'subscript' of wrapped base type 'KeychainStore'

So I removed where T: Decodable where T: Codable where T: Encodable
Is it ok? It works fine..

func object<T>(forKey key: String) -> T? {
        guard let data = synchronizationQueue.sync(execute: { () -> Data? in
            return self.data(forKey: key)
        }) else {
            return nil
        }
        if T.self is Decodable {
            return NSKeyedUnarchiver.unarchiveObject(with: data) as? T
        }
        else {
            return nil
        }
    }


func set<T>(object: T?, forKey key: String) -> Bool {
        guard let object = object, object is Encodable else {
            return removeObject(forKey: key)
        }
        let data = NSKeyedArchiver.archivedData(withRootObject: object)
        let query = keychainQuery(forKey: key)
        let update = [
            kSecValueData as String: data
        ]

        let status = synchronizationQueue.sync(flags: .barrier) { () -> OSStatus in
            if self.data(forKey: key) != nil {
                return SecItemUpdate(query as CFDictionary, update as CFDictionary)
            }
            let mergedQuery = query.merging(update) { (_, otherValue) -> Any in otherValue }
            return SecItemAdd(mergedQuery as CFDictionary, nil)
        }

        return status == errSecSuccess
    }


subscript<T>(key: String) -> T? {
        get {
            return object(forKey: key)
        }
        set (object) {
            _ = set(object: object, forKey: key)
        }
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I removed where T: Decodable where T: Codable where T: Encodable
Is it ok? It works fine..

It is not OK, as we do not want at compile time to store non Codable entries.

Do we really need array support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need [Any] object support.

Sources/ParseSwift/Storage/KeychainStore.swift Outdated Show resolved Hide resolved
ParseSwift.podspec Outdated Show resolved Hide resolved
Tests/ParseSwiftTests/KeychainStoreTests.swift Outdated Show resolved Hide resolved
Tests/ParseSwiftTests/KeychainStoreTests.swift Outdated Show resolved Hide resolved
Tests/ParseSwiftTests/KeychainStoreTests.swift Outdated Show resolved Hide resolved
Tests/ParseSwiftTests/KeychainStoreTests.swift Outdated Show resolved Hide resolved
Copy link
Contributor

@flovilmart flovilmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will also check for the cocoapods failure in the CI. Thank for getting this out :)

@@ -45,7 +44,16 @@ struct KeychainStore: SecureStorage {
return NSKeyedUnarchiver.unarchiveObject(with: data) as? T
}

func set<T>(object: T?, forKey key: String) -> Bool where T: Encodable {
func object<T>(forKey key: String) -> [T]? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no need for that, as a T can be an [U] if you specify it so.

@flovilmart
Copy link
Contributor

@ShawnBaek for the pods, you can try: pod lib lint --verbose --no-clean

@flovilmart
Copy link
Contributor

@ShawnBaek that was for you to debug the error :)

@ShawnBaek
Copy link
Contributor Author

@ShawnBaek that was for you to debug the error :)

Ah Ok Sorry I revert it. 👍

@ShawnBaek
Copy link
Contributor Author

ShawnBaek commented Jan 5, 2019

@ShawnBaek that was for you to debug the error :)

@flovilmart I think we need to add version tag 0.0.1 to pass Cocoapod lint check.

Pod::Spec.new do |s|
  s.name     = "ParseSwift"
  s.version  = "0.0.1"
  s.summary  = "Parse Pure Swift SDK"
  s.homepage = "https://github.com/parse-community/Parse-Swift"
  s.author = {
      "[Name]" => "[Mail Address]"
  }
  s.source = {
      :git => "#{s.homepage}.git",
      :tag => "#{s.version}",
  }
  s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.2' }
  s.ios.deployment_target = "10.0"
  s.osx.deployment_target = "10.10"
  s.tvos.deployment_target = "9.0"
  s.source_files = "Sources/ParseSwift/**/*.swift"
  s.license = {
    :type => "MIT",
    :text => <<-LICENSE
      Copyright (c) 2016 parse-community
      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
      The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    LICENSE
  }
end

screen shot 2019-01-05 at 1 35 29 pm

@flovilmart
Copy link
Contributor

@ShawnBaek this is unlikely, one should be able to lint the lib without having any tag published

class KeychainStoreTests: XCTestCase {
struct ComplexObject<T: Codable> : Codable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not what is being tested initially. What was tested was supporting storing arrays of Codables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flovilmart Thank you. I'm sorry for too many commits in here without considering all test cases. I am going to remove the nested structure.

}
//MARK:- Todo
// func testSetComplextObject() {
// let complexObject: [Any] = [["key": "value"], "string2", 1234, NSNull()]
Copy link
Contributor Author

@ShawnBaek ShawnBaek Feb 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flovilmart How about use AnyCodable for complexObject?

Reference : https://github.com/Flight-School/AnyCodable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? Seems like a decent idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'm going to use AnyCodable in Parse-Swift Project.

This was referenced Jul 9, 2020
@cbaker6
Copy link
Contributor

cbaker6 commented Jul 14, 2020

@TomWFox you can close this one out as #12 incorporates all of it's changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants