gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taler-swift-test] 01/01: init


From: gnunet
Subject: [taler-taler-swift-test] 01/01: init
Date: Sun, 19 Feb 2023 20:14:30 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository taler-swift-test.

commit 999ee54c53c71e59d2137dcf543e8efcadff7a8a
Author: Florian Dold <florian@dold.me>
AuthorDate: Sun Feb 19 20:13:32 2023 +0100

    init
---
 TalerWalletMessages/.gitignore                     |  9 +++
 TalerWalletMessages/Package.swift                  | 22 ++++++
 TalerWalletMessages/README.md                      |  3 +
 .../TalerWalletMessages/TalerWalletMessages.swift  | 80 ++++++++++++++++++++++
 .../TalerWalletMessagesTests.swift                 | 11 +++
 5 files changed, 125 insertions(+)

diff --git a/TalerWalletMessages/.gitignore b/TalerWalletMessages/.gitignore
new file mode 100644
index 0000000..3b29812
--- /dev/null
+++ b/TalerWalletMessages/.gitignore
@@ -0,0 +1,9 @@
+.DS_Store
+/.build
+/Packages
+/*.xcodeproj
+xcuserdata/
+DerivedData/
+.swiftpm/config/registries.json
+.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
+.netrc
diff --git a/TalerWalletMessages/Package.swift 
b/TalerWalletMessages/Package.swift
new file mode 100644
index 0000000..4e3afcf
--- /dev/null
+++ b/TalerWalletMessages/Package.swift
@@ -0,0 +1,22 @@
+// swift-tools-version: 5.7
+// The swift-tools-version declares the minimum version of Swift required to 
build this package.
+
+import PackageDescription
+
+let package = Package(
+    name: "TalerWalletMessages",
+    dependencies: [
+        // Dependencies declare other packages that this package depends on.
+        // .package(url: /* package url */, from: "1.0.0"),
+    ],
+    targets: [
+        // Targets are the basic building blocks of a package. A target can 
define a module or a test suite.
+        // Targets can depend on other targets in this package, and on 
products in packages this package depends on.
+        .executableTarget(
+            name: "TalerWalletMessages",
+            dependencies: []),
+        .testTarget(
+            name: "TalerWalletMessagesTests",
+            dependencies: ["TalerWalletMessages"]),
+    ]
+)
diff --git a/TalerWalletMessages/README.md b/TalerWalletMessages/README.md
new file mode 100644
index 0000000..df9a29f
--- /dev/null
+++ b/TalerWalletMessages/README.md
@@ -0,0 +1,3 @@
+# TalerWalletMessages
+
+A description of this package.
diff --git 
a/TalerWalletMessages/Sources/TalerWalletMessages/TalerWalletMessages.swift 
b/TalerWalletMessages/Sources/TalerWalletMessages/TalerWalletMessages.swift
new file mode 100644
index 0000000..08a7cbd
--- /dev/null
+++ b/TalerWalletMessages/Sources/TalerWalletMessages/TalerWalletMessages.swift
@@ -0,0 +1,80 @@
+import Foundation
+
+
+struct TransactionCommon: Decodable {
+    var type: String
+    var amountEffective: String
+    var amountRaw: String
+}
+
+struct WithdrawalTransactionDetails: Decodable {
+    var exchangeBaseUrl: String
+}
+
+struct PaymentTransactionDetails: Decodable {
+    var proposalId: String
+}
+
+struct WithdrawalTransaction: Decodable {
+    var common: TransactionCommon
+    var details: WithdrawalTransactionDetails
+}
+
+struct PaymentTransaction {
+    var common: TransactionCommon
+    var details: PaymentTransactionDetails
+}
+
+
+enum Transaction: Decodable {
+    case withdrawal (WithdrawalTransaction)
+    case payment (PaymentTransaction)
+
+    init(from decoder: Decoder) throws {
+        let common = try TransactionCommon.init(from: decoder)
+
+        switch (common.type) {
+        case "withdrawal":
+            let withdrawalDetails = try 
WithdrawalTransactionDetails.init(from: decoder)
+            self = .withdrawal(WithdrawalTransaction(common: common, details: 
withdrawalDetails))
+        case "payment":
+            let paymentDetails = try PaymentTransactionDetails.init(from: 
decoder)
+            self = .payment(PaymentTransaction(common: common, details: 
paymentDetails))
+            default:
+                let context = DecodingError.Context(
+                    codingPath: decoder.codingPath,
+                    debugDescription: "Invalid transaction type")
+                throw DecodingError.typeMismatch(Transaction.self, context)
+        }
+    }
+}
+
+let jsonExampleStr = """
+[
+    {
+        "type": "withdrawal",
+        "amountEffective": "KUDOS:14.8",
+        "amountRaw": "KUDOS:15",
+        "exchangeBaseUrl": "foo"
+    },
+    {
+        "type": "payment",
+        "amountEffective": "KUDOS:14.8",
+        "amountRaw": "KUDOS:15",
+        "proposalId": "ASD123"
+    }
+]
+"""
+
+@main
+public struct TalerWalletMessages {
+    public private(set) var text = "Hello, World!"
+
+    public static func main() {
+        let jsonData = jsonExampleStr.data(using: .utf8)!
+        let jsonDecoder = JSONDecoder()
+        let dec = try! jsonDecoder.decode([Transaction].self, from: jsonData)
+        print("Parsed:")
+        print(dec)
+    }
+}
diff --git 
a/TalerWalletMessages/Tests/TalerWalletMessagesTests/TalerWalletMessagesTests.swift
 
b/TalerWalletMessages/Tests/TalerWalletMessagesTests/TalerWalletMessagesTests.swift
new file mode 100644
index 0000000..3f96347
--- /dev/null
+++ 
b/TalerWalletMessages/Tests/TalerWalletMessagesTests/TalerWalletMessagesTests.swift
@@ -0,0 +1,11 @@
+import XCTest
+@testable import TalerWalletMessages
+
+final class TalerWalletMessagesTests: XCTestCase {
+    func testExample() throws {
+        // This is an example of a functional test case.
+        // Use XCTAssert and related functions to verify your tests produce 
the correct
+        // results.
+        XCTAssertEqual(TalerWalletMessages().text, "Hello, World!")
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]