Skip to content

[indexstore-db] Declare Codable conformance, to autosynthesize implementation. #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/IndexStoreDB/IndexStoreDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct PathMapping: Equatable {
}
}

public enum SymbolProviderKind: Sendable {
public enum SymbolProviderKind: Sendable, Codable {
case clang
case swift

Expand Down
2 changes: 1 addition & 1 deletion Sources/IndexStoreDB/IndexStoreDBError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import IndexStoreDB_CIndexStoreDB

import Foundation

public enum IndexStoreDBError: Error {
public enum IndexStoreDBError: Error, Codable {
case create(String)
case loadIndexStore(String)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/IndexStoreDB/Symbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@_implementationOnly
import IndexStoreDB_CIndexStoreDB

public enum IndexSymbolKind: Hashable, Sendable {
public enum IndexSymbolKind: Hashable, Sendable, Codable {
case unknown
case module
case namespace
Expand Down Expand Up @@ -45,14 +45,14 @@ public enum IndexSymbolKind: Hashable, Sendable {
case commentTag
}

public enum Language: Hashable, Sendable {
public enum Language: Hashable, Sendable, Codable {
case c
case cxx
case objc
case swift
}

public struct Symbol: Hashable, Sendable {
public struct Symbol: Hashable, Sendable, Codable {

public var usr: String
public var name: String
Expand Down
2 changes: 1 addition & 1 deletion Sources/IndexStoreDB/SymbolLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import IndexStoreDB_CIndexStoreDB
import Foundation

public struct SymbolLocation: Equatable, Sendable {
public struct SymbolLocation: Equatable, Sendable, Codable {
public var path: String
/// The date at which the unit file that contains a symbol has last been modified.
public var timestamp: Date
Expand Down
4 changes: 2 additions & 2 deletions Sources/IndexStoreDB/SymbolOccurrence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@_implementationOnly
import IndexStoreDB_CIndexStoreDB

public struct SymbolOccurrence: Equatable {
public struct SymbolOccurrence: Equatable, Codable {
public var symbol: Symbol
public var location: SymbolLocation
public var roles: SymbolRole
Expand Down Expand Up @@ -48,7 +48,7 @@ extension SymbolOccurrence: CustomStringConvertible {
}
}

public struct SymbolRelation: Equatable {
public struct SymbolRelation: Equatable, Codable {
public var symbol: Symbol
public var roles: SymbolRole

Expand Down
2 changes: 1 addition & 1 deletion Sources/IndexStoreDB/SymbolProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@_implementationOnly
import IndexStoreDB_CIndexStoreDB

public struct SymbolProperty: OptionSet, Hashable, Sendable {
public struct SymbolProperty: OptionSet, Hashable, Sendable, Codable {
public var rawValue: UInt64

public static let generic: SymbolProperty = SymbolProperty(rawValue: INDEXSTOREDB_SYMBOL_PROPERTY_GENERIC)
Expand Down
2 changes: 1 addition & 1 deletion Sources/IndexStoreDB/SymbolRole.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@_implementationOnly
import IndexStoreDB_CIndexStoreDB

public struct SymbolRole: OptionSet, Hashable, Sendable {
public struct SymbolRole: OptionSet, Hashable, Sendable, Codable {

public var rawValue: UInt64

Expand Down
42 changes: 42 additions & 0 deletions Tests/IndexStoreDBTests/IndexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,46 @@ final class IndexTests: XCTestCase {
largeType.at(ws.testLoc("LargeType:ref"), roles: .reference, symbolProvider: .swift),
])
}

// Validate automatic synthesized codable for our types
func testCodable() throws {

let encoder: JSONEncoder = JSONEncoder()
let decoder: JSONDecoder = JSONDecoder()

// C-family symbol with no properties
let largeType = Symbol(usr: "c:@CT@LargeType", name: "LargeType", kind: .concept, properties: [], language: .c)

let encodedLargeType = try encoder.encode(largeType)
let decodedLargeType = try decoder.decode(Symbol.self, from: encodedLargeType)

XCTAssertNotNil(encodedLargeType)
XCTAssertEqual(decodedLargeType, largeType)

// Swift Symbol with properties
let main = Symbol(usr: "s:4main1ayyF", name: "a()", kind: .instanceMethod, properties: [.unitTest], language: .swift)
let encodedMain = try encoder.encode(main)
let decodedMain = try decoder.decode(Symbol.self, from: encodedMain)
XCTAssertNotNil(encodedMain)
XCTAssertEqual(decodedMain, main)

guard let ws = try staticTibsTestWorkspace(name: "proj1") else { return }

let usr = "s:4main1cyyF"
let csym = Symbol(usr: usr, name: "c()", kind: .function, language: .swift)

// Symbol occurrence case
let ccanon = SymbolOccurrence(
symbol: csym,
location: SymbolLocation(ws.testLoc("c"), moduleName: "main"),
roles: [.definition, .canonical],
symbolProvider: .clang,
relations: [])

let encodedCCanon = try encoder.encode(ccanon)
let decodedCCanon = try decoder.decode(SymbolOccurrence.self, from: encodedCCanon)

XCTAssertNotNil(encodedCCanon)
XCTAssertEqual(decodedCCanon, ccanon)
}
}