-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetadata.swift
62 lines (57 loc) · 1.82 KB
/
Metadata.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Foundation
#if canImport(lib)
import lib
#endif
public final class Metadata {
private(set) var pointer: OpaquePointer?
/// Instantiate a `Metadata` object using the underlying pointer.
///
/// - Parameters:
/// - pointer: The pointer to the underlying foreign function interface object.
///
/// - Returns: `Metadata`
///
/// - Throws: `RuntimeError`, indicates underlying pointer is invalid.
public init(pointer: OpaquePointer?) {
self.pointer = pointer
}
/// Instantiate a `Metadata` object using the json representation.
///
/// - Parameters:
/// - json: Json representation as `String`.
///
/// - Returns: `Metadata`
///
/// - Throws: `RuntimeError`, indicates underlying pointer is invalid.
public init(json: String) throws {
var errorCode: Int32 = -1
let jsonPointer = UnsafeMutablePointer<Int8>(mutating: (json as NSString).utf8String)
let result = withUnsafeMutablePointer(to: &errorCode, { error in
metadata_from_json(jsonPointer, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in ShareStore")
}
pointer = result
}
/// Serialize to json
///
/// - Returns:`String`
///
/// - Throws: `RuntimeError`, indicates underlying pointer is invalid.
public func export() throws -> String {
var errorCode: Int32 = -1
let result = withUnsafeMutablePointer(to: &errorCode, { error in
metadata_to_json(pointer, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in ShareStore")
}
let value = String.init(cString: result!)
string_free(result)
return value
}
deinit {
metadata_free(pointer)
}
}