From 309cfec38d6b5464aa035181c303c53184ffa68a Mon Sep 17 00:00:00 2001
From: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
Date: Wed, 20 Nov 2024 00:39:52 -0500
Subject: [PATCH] Fix JSON parsing error by making `formattedPrice` & `price`
 optional for `SearchResult`.

Use new `displayPrice` to get around optional `formattedPrice`. Doesn't handle i18n, but we can clean that up after we completely redo JSON handling.

Resolve #493

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
---
 Sources/mas/Formatters/AppInfoFormatter.swift      | 2 +-
 Sources/mas/Formatters/SearchResultFormatter.swift | 2 +-
 Sources/mas/Models/SearchResult.swift              | 8 ++++++--
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/Sources/mas/Formatters/AppInfoFormatter.swift b/Sources/mas/Formatters/AppInfoFormatter.swift
index 710a7b5db..0482fb747 100644
--- a/Sources/mas/Formatters/AppInfoFormatter.swift
+++ b/Sources/mas/Formatters/AppInfoFormatter.swift
@@ -18,7 +18,7 @@ enum AppInfoFormatter {
         let headline = [
             "\(app.trackName)",
             "\(app.version)",
-            "[\(app.formattedPrice)]",
+            "[\(app.displayPrice)]",
         ]
         .joined(separator: " ")
 
diff --git a/Sources/mas/Formatters/SearchResultFormatter.swift b/Sources/mas/Formatters/SearchResultFormatter.swift
index c0bb51345..7e919fca0 100644
--- a/Sources/mas/Formatters/SearchResultFormatter.swift
+++ b/Sources/mas/Formatters/SearchResultFormatter.swift
@@ -29,7 +29,7 @@ enum SearchResultFormatter {
             let version = result.version
 
             if includePrice {
-                output += String(format: "%12lu  %@  (%@)  %@\n", appID, appName, version, result.formattedPrice)
+                output += String(format: "%12lu  %@  (%@)  %@\n", appID, appName, version, result.displayPrice)
             } else {
                 output += String(format: "%12lu  %@  (%@)\n", appID, appName, version)
             }
diff --git a/Sources/mas/Models/SearchResult.swift b/Sources/mas/Models/SearchResult.swift
index 0f67287cf..6c479c3e9 100644
--- a/Sources/mas/Models/SearchResult.swift
+++ b/Sources/mas/Models/SearchResult.swift
@@ -10,9 +10,9 @@ struct SearchResult: Decodable {
     var bundleId: String
     var currentVersionReleaseDate: String
     var fileSizeBytes: String
-    var formattedPrice: String
+    var formattedPrice: String?
     var minimumOsVersion: String
-    var price: Double
+    var price: Double?
     var sellerName: String
     var sellerUrl: String?
     var trackId: AppID
@@ -20,6 +20,10 @@ struct SearchResult: Decodable {
     var trackViewUrl: String
     var version: String
 
+    var displayPrice: String {
+        formattedPrice ?? "Unknown"
+    }
+
     init(
         bundleId: String = "",
         currentVersionReleaseDate: String = "",