Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Commit 7372d51

Browse files
committed
Convert Signature into a structure
Conform Signature to Codable
1 parent 7b38204 commit 7372d51

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

Sources/Git/Signature.swift

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@ import Foundation
44
/**
55
An action signature (e.g. for committers, taggers, etc).
66
*/
7-
public class Signature /*: internal RawRepresentable*/ {
7+
public struct Signature /*: internal RawRepresentable*/ {
88
var rawValue: git_signature
9-
var managed: Bool = false
109

1110
init(rawValue: git_signature) {
1211
self.rawValue = rawValue
1312
}
1413

15-
deinit {
16-
guard managed else { return }
17-
git_signature_free(&rawValue)
18-
}
19-
2014
public static func `default`(for repository: Repository) throws -> Signature {
2115
let pointer = UnsafeMutablePointer<UnsafeMutablePointer<git_signature>?>.allocate(capacity: 1)
2216
defer { pointer.deallocate() }
@@ -36,17 +30,16 @@ public class Signature /*: internal RawRepresentable*/ {
3630
- time: The time at which the action occurred.
3731
- timeZone: The time's corresponding time zone.
3832
*/
39-
public convenience init(name: String,
40-
email: String,
41-
time: Date = Date(),
42-
timeZone: TimeZone = TimeZone.current) throws
33+
public init(name: String,
34+
email: String,
35+
time: Date = Date(),
36+
timeZone: TimeZone = TimeZone.current) throws
4337
{
4438
var pointer: UnsafeMutablePointer<git_signature>?
4539
let offset = Int32(timeZone.secondsFromGMT(for: time) / 60)
4640
let time = git_time_t(time.timeIntervalSince1970)
4741
try attempt { git_signature_new(&pointer, name, email, time, offset) }
4842
self.init(rawValue: pointer!.pointee)
49-
// managed = true
5043
}
5144

5245
/// The name of the signer.
@@ -88,3 +81,32 @@ extension Signature: Hashable {
8881
hasher.combine(rawValue.when.offset)
8982
}
9083
}
84+
85+
// MARK: - Codable
86+
87+
extension Signature: Codable {
88+
private enum CodingKeys: String, CodingKey {
89+
case name
90+
case email
91+
case time
92+
case timeZone
93+
}
94+
95+
public init(from decoder: Decoder) throws {
96+
let container = try decoder.container(keyedBy: CodingKeys.self)
97+
let name = try container.decode(String.self, forKey: .name)
98+
let email = try container.decode(String.self, forKey: .email)
99+
let time = try container.decode(Date.self, forKey: .time)
100+
let timeZone = try container.decode(TimeZone.self, forKey: .timeZone)
101+
102+
try self.init(name: name, email: email, time: time, timeZone: timeZone)
103+
}
104+
105+
public func encode(to encoder: Encoder) throws {
106+
var container = encoder.container(keyedBy: CodingKeys.self)
107+
try container.encode(name, forKey: .name)
108+
try container.encode(email, forKey: .email)
109+
try container.encode(time, forKey: .time)
110+
try container.encode(timeZone, forKey: .timeZone)
111+
}
112+
}

0 commit comments

Comments
 (0)