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

Improve Data, Date and URL coding #36

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gering
Copy link

@gering gering commented Dec 6, 2021

This PR will change the default encoding strategies of Date, Data and URL objects, since they can be stored directly in Dictionaries unlike JSON, which does not support those types directly and thus defaults to formatted/base64 Strings.

  • Data now does not encode to base64 String, but to NSData directly
  • Data does decode from base64 String and NSData object
  • Date objects do now encode to Date directly, if no other strategy is chosen
  • Date objects do decode from all the variations but also from Date objects directly
  • URLs now do not encode to Strings anymore, but to NSURL directly
  • URLs do decode from Strings and NSURLs

Also, following test was not passing before, since a base64 encoded String was expected for Data objects.

    func testDecoding_dataFromVariousTypes_shouldSucceed() throws {
        // given
        struct JustData: Codable {
            let data: Data
        }

        let encoded1: [String: Any] = ["data": "dGVzdA=="]
        let encoded2: [String: Any] = ["data": "test2".data(using: .utf8)!]

        // when
        let decoded1 = try sut.decode(JustData.self, from: encoded1)
        let decoded2 = try sut.decode(JustData.self, from: encoded2)

        // then
        XCTAssertEqual(decoded1.data, "test".data(using: .utf8))
        XCTAssertEqual(decoded2.data, "test2".data(using: .utf8))
    }

This PR will also cleanup and add some more tests.

  • Test function names follow now a common method naming convention
  • Test functions are now grouped into "given, when, then" sections.

@gering gering force-pushed the improve-data-date-url-coding branch from 098106d to e5efb0d Compare December 6, 2021 14:09
@gering gering marked this pull request as ready for review December 6, 2021 14:19
Refactor tests, ensure Data, Date and URL are directly supported
@gering gering force-pushed the improve-data-date-url-coding branch from e5efb0d to 18c0d08 Compare December 6, 2021 14:21
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.

1 participant