-
Notifications
You must be signed in to change notification settings - Fork 2
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
186 additions
and
8 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
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,70 @@ | ||
// | ||
// NumberArrayProtectionTest.swift | ||
// JsonDecodeProtectionTests | ||
// | ||
// Created by Eden on 2024/7/3. | ||
// | ||
|
||
import XCTest | ||
@testable | ||
import JsonDecodeProtection | ||
|
||
private | ||
struct SomeObject: Decodable | ||
{ | ||
@ObjectProtection | ||
var subObjects: Array<SubObject>? | ||
|
||
@NumberArrayProtection | ||
var dices: Array<Int>? | ||
} | ||
|
||
extension SomeObject | ||
{ | ||
struct SubObject: Decodable | ||
{ | ||
private(set) | ||
var name: String? | ||
|
||
private(set) | ||
var number: Int? | ||
} | ||
} | ||
|
||
final class NumberArrayProtectionTest: XCTestCase | ||
{ | ||
var jsonString: String! | ||
|
||
override func setUpWithError() throws | ||
{ | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
|
||
// Arrange | ||
|
||
// Act | ||
|
||
// Assert | ||
} | ||
|
||
func testNumberObjectProtectionSuccess() throws | ||
{ | ||
// Arrange | ||
self.jsonString = """ | ||
{ | ||
"subObjects": "[{\\"name\\": \\"Jo\\", \\"number\\": 233}, {\\"name\\": \\"Ana\\", \\"number\\": 4565}]", | ||
"dices": "[1, 5.2, 1.0]" | ||
} | ||
""" | ||
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> = [1, 5, 1] | ||
|
||
XCTAssertEqual(actual, expect) | ||
} | ||
} |
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
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
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,75 @@ | ||
// | ||
// NumberArrayProtection.swift | ||
// | ||
// Created by Darktt on 2024/7/3. | ||
// Copyright © 2024 Darktt. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@propertyWrapper | ||
public | ||
struct NumberArrayProtection<Element> where Element: Decodable, Element: NumberType | ||
{ | ||
// MARK: - Properties - | ||
|
||
public | ||
typealias WarppedValue = Array<Element> | ||
|
||
public | ||
var wrappedValue: WarppedValue? | ||
|
||
// MARK: - Methods - | ||
// MARK: Initial Method | ||
|
||
public | ||
init() { } | ||
} | ||
|
||
// MARK: - Conform Protocols - | ||
|
||
extension NumberArrayProtection: Decodable | ||
{ | ||
public | ||
init(from decoder: Decoder) throws | ||
{ | ||
let container: SingleValueDecodingContainer = try decoder.singleValueContainer() | ||
|
||
if container.decodeNil() { | ||
|
||
self.wrappedValue = nil | ||
return | ||
} | ||
|
||
let jsonString: String = try container.decode(String.self) | ||
let wrappedValue: Array<Element>? = try self.decode(with: jsonString) | ||
|
||
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 | ||
{ | ||
public | ||
var description: String | ||
{ | ||
self.wrappedValue.map { | ||
|
||
"\($0)" | ||
} ?? "nil" | ||
} | ||
} |
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
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