Skip to content

Commit b9fcfff

Browse files
committed
Refactors generator data source
1 parent 2011b62 commit b9fcfff

File tree

8 files changed

+74
-88
lines changed

8 files changed

+74
-88
lines changed

Sources/TecoCommonErrorGenerator/builder.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ func buildCommonErrorStructDecl(from errors: [CommonError]) -> StructDeclSyntax
4545
""")
4646

4747
for error in errors {
48-
let summary = error.description.joined(separator: "\n")
49-
let solution = error.solution.map(formatErrorSolution)
50-
5148
VariableDeclSyntax("""
52-
\(raw: buildDocumentation(summary: summary, discussion: solution))
49+
\(raw: buildDocumentation(summary: error.description, discussion: error.solution))
5350
public static var \(raw: error.identifier): TCCommonError {
5451
TCCommonError(.\(raw: error.identifier))
5552
}

Sources/TecoCommonErrorGenerator/data.swift

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
@_implementationOnly import OrderedCollections
2+
13
// https://cloud.tencent.com/document/product/213/30435#.E5.85.AC.E5.85.B1.E9.94.99.E8.AF.AF.E7.A0.81
2-
let tcCommonErrors: [String : String] = [
4+
private let tcCommonErrors: [String : String] = [
35
"ActionOffline": "接口已下线。",
46
"AuthFailure.InvalidAuthorization": "请求头部的`Authorization`不符合腾讯云标准。",
57
"AuthFailure.InvalidSecretId": "密钥非法(不是云API密钥类型)。",
@@ -41,7 +43,7 @@ let tcCommonErrors: [String : String] = [
4143
]
4244

4345
// https://www.tencentcloud.com/document/product/213/33281#common-error-codes
44-
let tcIntlCommonErrors: [String : String] = [
46+
private let tcIntlCommonErrors: [String : String] = [
4547
"ActionOffline": "This API has been deprecated.",
4648
"AuthFailure.InvalidAuthorization": "`Authorization` in the request header is invalid.",
4749
"AuthFailure.InvalidSecretId": "Invalid key (not a TencentCloud API key type).",
@@ -82,32 +84,14 @@ let tcIntlCommonErrors: [String : String] = [
8284
"UnsupportedRegion": "API does not support the requested region.",
8385
]
8486

85-
struct APIError: Codable {
86-
let productShortName: String
87-
let productVersion: String
88-
let code: String
89-
let description: String?
90-
private let _solution: String
91-
let productCNName: String?
92-
93-
var solution: String? {
94-
switch self._solution {
95-
case "", "暂无", "占位符":
96-
return nil
97-
case "业务正在更新中,请您耐心等待。":
98-
return nil
99-
default:
100-
return self._solution.trimmingCharacters(in: .whitespacesAndNewlines)
101-
.replacingOccurrences(of: "\r\n", with: "\n")
102-
}
103-
}
87+
private func getCommonError(from code: String, apiErrors: [APIError] = []) -> CommonError {
88+
let description = [tcIntlCommonErrors[code], tcCommonErrors[code]].compactMap { $0 }.joined(separator: "\n")
89+
precondition(description.isEmpty == false)
90+
let solution = apiErrors.first(where: { $0.code == code })?.solution.map(formatErrorSolution)
91+
return CommonError(code: code, description: description, solution: solution)
92+
}
10493

105-
enum CodingKeys: String, CodingKey {
106-
case productShortName = "productName"
107-
case productVersion
108-
case code
109-
case description
110-
case _solution = "solution"
111-
case productCNName
112-
}
94+
func getCommonErrors(with apiErrors: [APIError] = []) -> [CommonError] {
95+
let codes = OrderedSet(tcCommonErrors.keys).union(tcIntlCommonErrors.keys).sorted()
96+
return codes.map { getCommonError(from: $0, apiErrors: apiErrors) }
11397
}

Sources/TecoCommonErrorGenerator/generator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ struct TecoCommonErrorGenerator: ParsableCommand {
1313
var errorFile: URL?
1414

1515
func run() throws {
16-
let codes = getErrorCodes()
1716
let apiErrors = try getAPIErrors(from: errorFile)
18-
let errors = getCommonErrors(from: codes, apiErrors: apiErrors)
17+
let errors = getCommonErrors(with: apiErrors)
1918

2019
let sourceFile = SourceFileSyntax {
2120
buildCommonErrorStructDecl(from: errors)

Sources/TecoCommonErrorGenerator/helpers.swift

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
import ArgumentParser
22
import Foundation
3-
@_implementationOnly import OrderedCollections
43

54
struct CommonError {
65
let code: String
7-
var identifier: String { self.code.lowerFirst().replacingOccurrences(of: ".", with: "_") }
8-
let description: [String]
6+
let description: String
97
let solution: String?
8+
9+
var identifier: String { self.code.lowerFirst().replacingOccurrences(of: ".", with: "_") }
1010
}
1111

12-
func getErrorCodes() -> [String] {
13-
return OrderedSet(tcCommonErrors.keys).union(tcIntlCommonErrors.keys).sorted()
12+
struct APIError: Codable {
13+
let product: String
14+
let code: String
15+
private let _solution: String
16+
17+
var solution: String? {
18+
switch self._solution {
19+
case "", "暂无", "占位符":
20+
return nil
21+
case "业务正在更新中,请您耐心等待。":
22+
return nil
23+
default:
24+
return self._solution.trimmingCharacters(in: .whitespacesAndNewlines)
25+
.replacingOccurrences(of: "\r\n", with: "\n")
26+
}
27+
}
28+
29+
enum CodingKeys: String, CodingKey {
30+
case product = "productName"
31+
case code
32+
case _solution = "solution"
33+
}
1434
}
1535

1636
func getAPIErrors(from file: URL?) throws -> [APIError] {
1737
if let file {
1838
return try JSONDecoder().decode([APIError].self, from: .init(contentsOf: file))
19-
.filter { $0.productShortName == "PLATFORM" }
39+
.filter { $0.product == "PLATFORM" }
2040
} else {
2141
return []
2242
}
2343
}
2444

25-
func getCommonError(from code: String, apiErrors: [APIError] = []) -> CommonError {
26-
let description = [tcIntlCommonErrors[code], tcCommonErrors[code]].compactMap { $0 }
27-
precondition(description.isEmpty == false)
28-
let solution = apiErrors.first(where: { $0.code == code })?.solution
29-
return CommonError(code: code, description: description, solution: solution)
30-
}
31-
32-
func getCommonErrors(from codes: [String], apiErrors: [APIError] = []) -> [CommonError] {
33-
return codes.map { getCommonError(from: $0, apiErrors: apiErrors) }
34-
}
35-
3645
func formatErrorSolution(_ solution: String) -> String {
3746
var lines = solution.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline)
3847
guard let index = lines.firstIndex(where: \.isEmpty) else {

Sources/TecoRegionDataGenerator/builder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ func buildRegionExpr(id: String, names: OrderedSet<String>, trailingComma: Bool
66
precondition(names.isEmpty == false)
77
let valueExpr = FunctionCallExprSyntax(callee: ExprSyntax("Region")) {
88
TupleExprElementSyntax(label: "id", expression: ExprSyntax("\(literal: id)"))
9-
TupleExprElementSyntax(label: "localizedNames", expression: ExprSyntax("\(literal: Array(names))"))
9+
TupleExprElementSyntax(label: "localized", expression: ExprSyntax("\(literal: Array(names))"))
1010
}
1111
return ArrayElementSyntax(expression: valueExpr, trailingComma: trailingComma ? .commaToken() : nil)
1212
}

Sources/TecoRegionDataGenerator/generator.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ struct TecoRegionDataGenerator: AsyncParsableCommand {
2121
let intlMap = getRegionMap(from: try await client.with(language: .en_US).describeRegions(for: product))
2222

2323
let sourceFile = SourceFileSyntax {
24-
StructDeclSyntax("""
24+
VariableDeclSyntax("""
2525
// THIS FILE IS AUTOMATICALLY GENERATED by TecoRegionDataGenerator.
2626
// DO NOT EDIT.
27-
28-
struct Region: Hashable {
29-
let id: String
30-
let localizedNames: [String]
31-
}
27+
28+
let regions: [Region] = \(buildRegionListExpr(from: intlMap, map))
3229
""")
33-
34-
VariableDeclSyntax("let regions = \(buildRegionListExpr(from: intlMap, map))")
3530
}
3631
try sourceFile.save(to: self.output)
3732
}
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
// THIS FILE IS AUTOMATICALLY GENERATED by TecoRegionDataGenerator.
22
// DO NOT EDIT.
33

4-
struct Region: Hashable {
5-
let id: String
6-
7-
let localizedNames: [String]
8-
}
9-
10-
let regions = [
11-
Region(id: "ap-guangzhou", localizedNames: ["South China(Guangzhou)", "华南地区(广州)"]),
12-
Region(id: "ap-shanghai", localizedNames: ["East China(Shanghai)", "华东地区(上海)"]),
13-
Region(id: "ap-nanjing", localizedNames: ["East China(Nanjing)", "华东地区(南京)"]),
14-
Region(id: "ap-hongkong", localizedNames: ["Hong Kong, Macau and Taiwan (China)(Hong Kong, China)", "港澳台地区(中国香港)"]),
15-
Region(id: "na-toronto", localizedNames: ["North America(Toronto)", "北美地区(多伦多)"]),
16-
Region(id: "ap-beijing", localizedNames: ["North China region(Beijing)", "华北地区(北京)"]),
17-
Region(id: "ap-singapore", localizedNames: ["Southeast Asia(Singapore)", "亚太东南(新加坡)"]),
18-
Region(id: "ap-bangkok", localizedNames: ["Southeast Asia(Bangkok)", "亚太东南(曼谷)"]),
19-
Region(id: "ap-jakarta", localizedNames: ["Southeast Asia(Jakarta)", "亚太东南(雅加达)"]),
20-
Region(id: "na-siliconvalley", localizedNames: ["US West(Silicon Valley)", "美国西部(硅谷)"]),
21-
Region(id: "ap-chengdu", localizedNames: ["Southwest China(Chengdu)", "西南地区(成都)"]),
22-
Region(id: "ap-chongqing", localizedNames: ["Southwest China(Chongqing)", "西南地区(重庆)"]),
23-
Region(id: "eu-frankfurt", localizedNames: ["Europe(Frankfurt)", "欧洲地区(法兰克福)"]),
24-
Region(id: "eu-moscow", localizedNames: ["Europe(Northeastern Europe)", "欧洲地区(莫斯科)"]),
25-
Region(id: "ap-seoul", localizedNames: ["Northeast Asia(Seoul)", "亚太东北(首尔)"]),
26-
Region(id: "ap-tokyo", localizedNames: ["Northeast Asia(Tokyo)", "亚太东北(东京)"]),
27-
Region(id: "ap-mumbai", localizedNames: ["South Asia(Mumbai)", "亚太南部(孟买)"]),
28-
Region(id: "na-ashburn", localizedNames: ["US East(Virginia)", "美国东部(弗吉尼亚)"]),
29-
Region(id: "sa-saopaulo", localizedNames: ["South America(São Paulo)", "南美地区(圣保罗)"]),
4+
let regions: [Region] = [
5+
Region(id: "ap-guangzhou", localized: ["South China(Guangzhou)", "华南地区(广州)"]),
6+
Region(id: "ap-shanghai", localized: ["East China(Shanghai)", "华东地区(上海)"]),
7+
Region(id: "ap-nanjing", localized: ["East China(Nanjing)", "华东地区(南京)"]),
8+
Region(id: "ap-hongkong", localized: ["Hong Kong, Macau and Taiwan (China)(Hong Kong, China)", "港澳台地区(中国香港)"]),
9+
Region(id: "na-toronto", localized: ["North America(Toronto)", "北美地区(多伦多)"]),
10+
Region(id: "ap-beijing", localized: ["North China region(Beijing)", "华北地区(北京)"]),
11+
Region(id: "ap-singapore", localized: ["Southeast Asia(Singapore)", "亚太东南(新加坡)"]),
12+
Region(id: "ap-bangkok", localized: ["Southeast Asia(Bangkok)", "亚太东南(曼谷)"]),
13+
Region(id: "ap-jakarta", localized: ["Southeast Asia(Jakarta)", "亚太东南(雅加达)"]),
14+
Region(id: "na-siliconvalley", localized: ["US West(Silicon Valley)", "美国西部(硅谷)"]),
15+
Region(id: "ap-chengdu", localized: ["Southwest China(Chengdu)", "西南地区(成都)"]),
16+
Region(id: "ap-chongqing", localized: ["Southwest China(Chongqing)", "西南地区(重庆)"]),
17+
Region(id: "eu-frankfurt", localized: ["Europe(Frankfurt)", "欧洲地区(法兰克福)"]),
18+
Region(id: "eu-moscow", localized: ["Europe(Northeastern Europe)", "欧洲地区(莫斯科)"]),
19+
Region(id: "ap-seoul", localized: ["Northeast Asia(Seoul)", "亚太东北(首尔)"]),
20+
Region(id: "ap-tokyo", localized: ["Northeast Asia(Tokyo)", "亚太东北(东京)"]),
21+
Region(id: "ap-mumbai", localized: ["South Asia(Mumbai)", "亚太南部(孟买)"]),
22+
Region(id: "na-ashburn", localized: ["US East(Virginia)", "美国东部(弗吉尼亚)"]),
23+
Region(id: "sa-saopaulo", localized: ["South America(São Paulo)", "南美地区(圣保罗)"]),
3024
]

Sources/TecoRegionGenerator/helpers.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
extension Region {
1+
struct Region: Hashable {
2+
let id: String
3+
let localizedNames: [String]
4+
25
var description: String { self.localizedNames.joined(separator: " / ") }
36
var identifier: String { self.id.replacingOccurrences(of: "-", with: "_") }
47
var kind: String? {
@@ -10,6 +13,11 @@ extension Region {
1013
}
1114
return nil
1215
}
16+
17+
init(id: String, localized: [String]) {
18+
self.id = id
19+
self.localizedNames = localized
20+
}
1321
}
1422

1523
extension Region: Comparable {

0 commit comments

Comments
 (0)