Skip to content

Commit

Permalink
ObjectProtection 與 NumberArrayProtection 增加空字串的判斷
Browse files Browse the repository at this point in the history
  • Loading branch information
Darktt committed Dec 2, 2024
1 parent 442d286 commit 3392402
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v4
- name: Select Xcode
Expand Down
22 changes: 22 additions & 0 deletions JsonDecodeProtectionTests/NumberArrayProtectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,26 @@ final class NumberArrayProtectionTest: XCTestCase

XCTAssertEqual(actual, expect)
}

func testNumberObjectProtectionWithEmptyString() throws
{
// Arrange
self.jsonString = """
{
"subObjects": "[{\\"name\\": \\"Jo\\", \\"number\\": 233}, {\\"name\\": \\"Ana\\", \\"number\\": 4565}]",
"dices": ""
}
"""
let jsonData: Data = self.jsonString.data(using: .utf8)!
let jsonDecoder = JSONDecoder()

// Act
let object = try jsonDecoder.decode(SomeObject.self, from: jsonData)

// Assert
let actual: Array<Int>? = object.dices
let expect: Array<Int>? = nil

XCTAssertEqual(actual, expect)
}
}
31 changes: 30 additions & 1 deletion JsonDecodeProtectionTests/ObjectProtectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ struct SomeObject: Decodable

extension SomeObject
{
struct SubObject: Decodable
struct SubObject: Decodable, Equatable
{
private(set)
var name: String?

private(set)
var number: Int?

static
func == (lhs: SomeObject.SubObject, rhs: SomeObject.SubObject) -> Bool
{
lhs.name == rhs.name && lhs.number == rhs.number
}
}
}

Expand Down Expand Up @@ -90,4 +96,27 @@ final class ObjectProtectionTest: XCTestCase

XCTAssertEqual(actual, expect)
}

func testObjectProtectionWithEmptyString() throws
{
// Arrange
self.jsonString = """
{
"subObjects": "",
"dices": "[1, 5, 1]"
}
"""

let jsonData: Data = self.jsonString.data(using: .utf8)!
let jsonDecoder = JSONDecoder()

// Act
let object = try jsonDecoder.decode(SomeObject.self, from: jsonData)

// Assert
let actual: Array<SomeObject.SubObject>? = object.subObjects
let expect: Array<SomeObject.SubObject>? = nil

XCTAssertEqual(actual, expect)
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

* File > Swift Packages > Add Package Dependency
* Add https://github.com/Darktt/JsonProtection
* Select "Up to Next Major" with "1.2.0"
* Select "Up to Next Major" with "1.2.1"

## 功能說明

Expand Down
35 changes: 21 additions & 14 deletions SourceCode/JsonProtection/NumberArrayProtection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ struct NumberArrayProtection<Element> where Element: Decodable, Element: NumberT
init() { }
}

// MARK: - Private Methods -

private
extension NumberArrayProtection
{
private
func decode(with string: String) throws -> Array<Element>?
{
guard !string.isEmpty,
let data: Data = string.data(using: .utf8) else {

return nil
}

let jsonDecoder = JSONDecoder()
let wrapperValue = try jsonDecoder.decode(NumbersProtection<Element>.self, from: data)

return wrapperValue.wrappedValue
}
}

// MARK: - Conform Protocols -

extension NumberArrayProtection: Decodable
Expand All @@ -46,20 +67,6 @@ extension NumberArrayProtection: Decodable

self.wrappedValue = wrappedValue
}

private
func decode(with string: String) throws -> Array<Element>?
{
guard let data: Data = string.data(using: .utf8) else {

return nil
}

let jsonDecoder = JSONDecoder()
let wrapperValue = try jsonDecoder.decode(NumbersProtection<Element>.self, from: data)

return wrapperValue.wrappedValue
}
}

extension NumberArrayProtection: CustomStringConvertible
Expand Down
3 changes: 2 additions & 1 deletion SourceCode/JsonProtection/ObjectProtection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ extension ObjectProtection
{
func decode(with string: String) throws -> Value?
{
guard let data: Data = string.data(using: .utf8) else {
guard !string.isEmpty,
let data: Data = string.data(using: .utf8) else {

return nil
}
Expand Down

0 comments on commit 3392402

Please sign in to comment.