11 lines
4.3 KiB
JSON
Raw Normal View History

2024-12-23 19:29:05 +00:00
{
"extension": ".swift",
"source": "import Foundation\n\n/// A comment posted by a user inside of a rant.\npublic struct Comment: Identifiable, Hashable, Sendable {\n /// The id of this comment.\n public let id: Int\n \n /// The id of the rant that the comment belongs to.\n public let rantId: Int\n \n /// The current logged in user's vote on this comment.\n public let voteState: VoteState\n \n /// The number of upvotes from other users.\n public let score: Int\n \n /// The user who wrote this comment.\n public let author: User\n \n /// The time when this comment was created.\n public let created: Date\n \n /// True if this comment is edited by the author.\n public let isEdited: Bool\n \n /// The text contents of this comment.\n public let text: String\n \n /// The URLs and user mentions inside of the text of this comment.\n public let linksInText: [Link]\n \n /// The optional image that the user has uploaded for this comment.\n public let image: AttachedImage?\n \n public init(id: Int, rantId: Int, voteState: VoteState, score: Int, author: User, created: Date, isEdited: Bool, text: String, linksInText: [Link], image: AttachedImage?) {\n self.id = id\n self.rantId = rantId\n self.voteState = voteState\n self.score = score\n self.author = author\n self.created = created\n self.isEdited = isEdited\n self.text = text\n self.linksInText = linksInText\n self.image = image\n }\n}\n\nextension Comment {\n struct CodingData: Codable {\n let id: Int\n let rant_id: Int\n let body: String\n let score: Int\n let created_time: Int\n let vote_state: Int\n let links: [Link.CodingData]?\n let user_id: Int\n let user_username: String\n let user_score: Int\n let user_avatar: User.Avatar.CodingData\n let user_avatar_lg: User.Avatar.CodingData?\n let user_dpp: Int?\n let attached_image: AttachedImage.CodingData?\n let edited: Bool?\n }\n}\n\nextension Comment.CodingData {\n var decoded: Comment {\n .init(\n id: id,\n rantId: rant_id,\n voteState: .init(rawValue: vote_state) ?? .unvoted,\n score: score,\n author: .init(\n id: user_id,\n name: user_username,\n score: user_score,\n devRantSupporter: (user_dpp ?? 0) != 0,\n avatarSmall: user_avatar.decoded,\n avatarLarge: user_avatar_lg?.decoded\n ),\n created: Date(timeIntervalSince1970: TimeInterval(created_time)),\n isEdited: edited ?? false,\n text: body,\n linksInText: links?.map(\\.decoded) ?? [],\n image: attached_image?.decoded\n )\n }\n}\n",
"review": "**7**\n\n### Bugs\n- No evident bugs in the provided code.\n\n### Optimizations\n- Use more descriptive parameter names in the `init` method of `Comment` for clarity.\n- Utilize `lazy var` for properties that are computationally expensive and are not needed immediately upon object instantiation.\n- Consider making `Comment` conform to `Codable` directly instead of adding an inner `CodingData` struct.\n\n### Good points\n- Well-structured and organized code with clear commenting, aiding readability.\n- Effective use of Swift features such as `struct`, `extension`, and computed properties.\n- Thoughtful use of encapsulation and data transformation through the `CodingData` structure.\n\n### Summary\nThe code is well-structured, leveraging Swift's language features deftly, like extensions, to maintain code clarity and manage complexity. Code comments provide a good explanation of each property\u2019s purpose, aiding future maintenance. It\u2019s ready for integration but could be optimized for clarity and performance.\n\n### Open source alternatives\n- **SwiftyJSON**: Although primarily focused on JSON parsing, SwiftyJSON could ease the decoding process.\n- **Codable** from the Swift Standard Library: Direct use of Codable can provide a cleaner approach without needing separate structs for coding/decoding purposes.",
"filename": "Comment.swift",
"path": "Sources/SwiftDevRant/Models/Comment.swift",
"directory": "Models",
"grade": 7,
"size": 2796,
"line_count": 92
}