-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conform BinanceAuth/KucoinAuth to RawRepresentable
- Allows for using @AppStorage.
- Loading branch information
1 parent
209d153
commit 665e14b
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
Sources/SwiftTrader/Model/Binance/BinanceAuth/BinanceAuth+RawRepresentable.swift
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 @@ | ||
// | ||
// BinanceAuth+RawRepresentable.swift | ||
// | ||
// | ||
// Created by Fernando Fernandes on 11.03.24. | ||
// | ||
|
||
import Foundation | ||
|
||
extension BinanceAuth: RawRepresentable { | ||
public typealias RawValue = String | ||
|
||
public init?(rawValue: RawValue) { | ||
guard let data = rawValue.data(using: .utf8), | ||
let result = try? JSONDecoder().decode(BinanceAuth.self, from: data) else { | ||
return nil | ||
} | ||
self = result | ||
} | ||
|
||
public var rawValue: RawValue { | ||
guard let data = try? JSONEncoder().encode(self) else { | ||
return "Failed to encode data" | ||
} | ||
guard let jsonString = String(data: data, encoding: .utf8) else { | ||
return "Faile to convert data to String" | ||
} | ||
return jsonString | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Sources/SwiftTrader/Model/Kucoin/KucoinAuth/KucoinAuth+RawRepresentable.swift
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,29 @@ | ||
// | ||
// KucoinAuth+RawRepresentable.swift | ||
// | ||
// | ||
// Created by Fernando Fernandes on 06.03.24. | ||
// | ||
|
||
import Foundation | ||
|
||
extension KucoinAuth: RawRepresentable { | ||
public typealias RawValue = String | ||
|
||
public init?(rawValue: RawValue) { | ||
guard let data = rawValue.data(using: .utf8), | ||
let result = try? JSONDecoder().decode(KucoinAuth.self, from: data) else { | ||
return nil | ||
} | ||
self = result | ||
} | ||
|
||
public var rawValue: RawValue { | ||
guard let data = try? JSONEncoder().encode(self) else { | ||
return "Failed to encode data" | ||
} | ||
guard let jsonString = String(data: data, encoding: .utf8) else { | ||
return "Faile to convert data to String" | ||
} | ||
return jsonString | ||
}} |