-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Content * Make ChatContent public * Rename ChatContent -> MessageContent * Update ChatMessage * Fix init * Add init for converting image data to base64 * Return emtpy string in case of no text
- Loading branch information
1 parent
61812ff
commit cf2f607
Showing
7 changed files
with
355 additions
and
7 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
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,90 @@ | ||
// | ||
// ChatContent.swift | ||
// | ||
// | ||
// Created by Ronald Mannak on 4/12/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum MessageContent: Hashable { | ||
case text(String) | ||
case imageUrl(URLDetail) | ||
} | ||
|
||
extension MessageContent { | ||
public enum ContentType: String, Codable, Hashable { | ||
case text | ||
case imageUrl = "image_url" | ||
} | ||
|
||
public struct URLDetail: Codable, Equatable, Hashable { | ||
|
||
public enum Detail: String, Codable { | ||
case low, high, auto | ||
} | ||
|
||
let url: String | ||
let detail: Detail? | ||
|
||
public init(url: String, detail: Detail? = nil) { | ||
self.url = url | ||
self.detail = detail | ||
} | ||
|
||
public init(url: URL, detail: Detail? = nil) { | ||
self.init(url: url.absoluteString, detail: detail) | ||
} | ||
|
||
public init(imageData: Data, detail: Detail? = nil) { | ||
let base64 = imageData.base64EncodedString() | ||
self.init(url: "data:image/jpeg;base64,\(base64)", detail: detail) | ||
} | ||
} | ||
} | ||
|
||
extension MessageContent: Codable { | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case type, text, imageUrl | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let type = try container.decode(ContentType.self, forKey: .type) | ||
|
||
switch type { | ||
case .text: | ||
let text = try container.decode(String.self, forKey: .text) | ||
self = .text(text) | ||
case .imageUrl: | ||
let imageUrl = try container.decode(URLDetail.self, forKey: .imageUrl) | ||
self = .imageUrl(imageUrl) | ||
} | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch self { | ||
case .text(let text): | ||
try container.encode(ContentType.text.rawValue, forKey: .type) | ||
try container.encode(text, forKey: .text) | ||
case .imageUrl(let urlDetail): | ||
try container.encode(ContentType.imageUrl.rawValue, forKey: .type) | ||
try container.encode(urlDetail, forKey: .imageUrl) | ||
} | ||
} | ||
} | ||
|
||
extension MessageContent: Equatable { | ||
public static func == (lhs: MessageContent, rhs: MessageContent) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.text(let lhsText), .text(let rhsText)): | ||
return lhsText == rhsText | ||
case (.imageUrl(let lhsUrlDetail), .imageUrl(let rhsUrlDetail)): | ||
return lhsUrlDetail == rhsUrlDetail | ||
default: | ||
return false | ||
} | ||
} | ||
} |
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,24 @@ | ||
// | ||
// MessageContentTests.swift | ||
// | ||
// | ||
// Created by Ronald Mannak on 4/12/24. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import CleverBird | ||
|
||
class MessageContentTests: XCTestCase { | ||
|
||
func testURL() { | ||
let content = MessageContent.URLDetail(url: URL(string: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg")!) | ||
XCTAssertEqual(content.url, "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg") | ||
} | ||
|
||
func testBase64() { | ||
let data = "Hello, world".data(using: .utf8)! | ||
let content = MessageContent.URLDetail(imageData: data) | ||
XCTAssertEqual(content.url, "data:image/jpeg;base64,SGVsbG8sIHdvcmxk") | ||
} | ||
} |
Oops, something went wrong.