|
| 1 | +import CKclLib |
1 | 2 | import Foundation |
2 | 3 | import SwiftProtobuf |
3 | | -import CKclLib |
4 | 4 |
|
5 | 5 | public class API: Service { |
6 | | - private static let ERROR_PREFIX = "ERROR:" |
7 | | - |
8 | | - public init() {} |
9 | | - |
10 | | - // Parses a single KCL file and returns its Abstract Syntax Tree (AST) as a JSON string. |
11 | | - public func parseFile(_ args: ParseFile_Args) throws -> ParseFile_Result { |
12 | | - return try ParseFile_Result(serializedBytes: callNative(name: "KclvmService.ParseFile", args: try args.serializedBytes())) |
13 | | - } |
14 | | - |
15 | | - // Parses a KCL program and returns the Abstract Syntax Tree (AST) in JSON format. |
16 | | - public func parseProgram(_ args: ParseProgram_Args) throws -> ParseProgram_Result { |
17 | | - return try ParseProgram_Result(serializedBytes: callNative(name: "KclvmService.ParseProgram", args: try args.serializedBytes())) |
18 | | - } |
19 | | - |
20 | | - // Loads a KCL package and retrieves AST, symbol, type, and definition information. |
21 | | - public func loadPackage(_ args: LoadPackage_Args) throws -> LoadPackage_Result { |
22 | | - return try LoadPackage_Result(serializedBytes: callNative(name: "KclvmService.LoadPackage", args: try args.serializedBytes())) |
23 | | - } |
24 | | - |
25 | | - // Executes a KCL file with provided arguments. |
26 | | - public func execProgram(_ args: ExecProgram_Args) throws -> ExecProgram_Result { |
27 | | - return try ExecProgram_Result(serializedBytes: callNative(name: "KclvmService.ExecProgram", args: try args.serializedBytes())) |
28 | | - } |
29 | | - |
30 | | - // Overrides specified elements in a KCL file according to given arguments. |
31 | | - public func overrideFile(_ args: OverrideFile_Args) throws -> OverrideFile_Result { |
32 | | - return try OverrideFile_Result(serializedBytes: callNative(name: "KclvmService.OverrideFile", args: try args.serializedBytes())) |
33 | | - } |
34 | | - |
35 | | - // Lists all variables declared in a KCL file. |
36 | | - public func listVariables(_ args: ListVariables_Args) throws -> ListVariables_Result { |
37 | | - return try ListVariables_Result(serializedBytes: callNative(name: "KclvmService.ListVariables", args: try args.serializedBytes())) |
38 | | - } |
39 | | - |
40 | | - // Lists all options defined in a KCL program. |
41 | | - public func listOptions(_ args: ParseProgram_Args) throws -> ListOptions_Result { |
42 | | - return try ListOptions_Result(serializedBytes: callNative(name: "KclvmService.ListOptions", args: try args.serializedBytes())) |
43 | | - } |
44 | | - |
45 | | - // Retrieves the full schema type mapping for a KCL program. |
46 | | - public func getSchemaTypeMapping(_ args: GetSchemaTypeMapping_Args) throws -> GetSchemaTypeMapping_Result { |
47 | | - return try GetSchemaTypeMapping_Result(serializedBytes: callNative(name: "KclvmService.GetSchemaTypeMapping", args: try args.serializedBytes())) |
48 | | - } |
49 | | - |
50 | | - // Formats source code according to KCL style guidelines. |
51 | | - public func formatCode(_ args: FormatCode_Args) throws -> FormatCode_Result { |
52 | | - return try FormatCode_Result(serializedBytes: callNative(name: "KclvmService.FormatCode", args: try args.serializedBytes())) |
53 | | - } |
54 | | - |
55 | | - // Formats KCL files or directories to conform to style guidelines. |
56 | | - public func formatPath(_ args: FormatPath_Args) throws -> FormatPath_Result { |
57 | | - return try FormatPath_Result(serializedBytes: callNative(name: "KclvmService.FormatPath", args: try args.serializedBytes())) |
58 | | - } |
59 | | - |
60 | | - // Runs linting checks on KCL files and reports errors and warnings. |
61 | | - public func lintPath(_ args: LintPath_Args) throws -> LintPath_Result { |
62 | | - return try LintPath_Result(serializedBytes: callNative(name: "KclvmService.LintPath", args: try args.serializedBytes())) |
63 | | - } |
64 | | - |
65 | | - // Validates a data string against a schema defined in a KCL code string. |
66 | | - public func validateCode(_ args: ValidateCode_Args) throws -> ValidateCode_Result { |
67 | | - return try ValidateCode_Result(serializedBytes: callNative(name: "KclvmService.ValidateCode", args: try args.serializedBytes())) |
68 | | - } |
69 | | - |
70 | | - // Builds configuration from settings files. |
71 | | - public func loadSettingsFiles(_ args: LoadSettingsFiles_Args) throws -> LoadSettingsFiles_Result { |
72 | | - return try LoadSettingsFiles_Result(serializedBytes: callNative(name: "KclvmService.LoadSettingsFiles", args: try args.serializedBytes())) |
73 | | - } |
74 | | - |
75 | | - // Renames symbols across files within a KCL package. |
76 | | - public func rename(_ args: Rename_Args) throws -> Rename_Result { |
77 | | - return try Rename_Result(serializedBytes: callNative(name: "KclvmService.Rename", args: try args.serializedBytes())) |
78 | | - } |
79 | | - |
80 | | - // Renames symbols in source code without modifying files directly. |
81 | | - public func renameCode(_ args: RenameCode_Args) throws -> RenameCode_Result { |
82 | | - return try RenameCode_Result(serializedBytes: callNative(name: "KclvmService.RenameCode", args: try args.serializedBytes())) |
83 | | - } |
84 | | - |
85 | | - // Executes tests on KCL packages using specified test arguments. |
86 | | - public func test(_ args: Test_Args) throws -> Test_Result { |
87 | | - return try Test_Result(serializedBytes: callNative(name: "KclvmService.Test", args: try args.serializedBytes())) |
88 | | - } |
89 | | - |
90 | | - // Updates dependencies for a KCL project based on defined specifications. |
91 | | - public func updateDependencies(_ args: UpdateDependencies_Args) throws -> UpdateDependencies_Result { |
92 | | - return try UpdateDependencies_Result(serializedBytes: callNative(name: "KclvmService.UpdateDependencies", args: try args.serializedBytes())) |
93 | | - } |
94 | | - |
95 | | - // Retrieves version information about the KCL service. |
96 | | - public func getVersion(_ args: GetVersion_Args) throws -> GetVersion_Result { |
97 | | - return try GetVersion_Result(serializedBytes: callNative(name: "KclvmService.GetVersion", args: try args.serializedBytes())) |
98 | | - } |
99 | | - |
100 | | - private func callNative(name: String, args: Data) -> Data { |
101 | | - // Convert name to byte array |
102 | | - let nameBytes = [UInt8](name.utf8) |
103 | | - var resultBuf = [UInt8](repeating: 0, count: 2048 * 2048) |
104 | | - let resultLength = CKclLib.callNative(nameBytes, UInt(nameBytes.count), [UInt8](args), UInt(args.count), &resultBuf) |
105 | | - let result = Data(bytes: resultBuf, count: Int(resultLength)) |
106 | | - let resultString = String(bytes: result, encoding: .utf8) ?? "" |
107 | | - if resultString.isEmpty || !resultString.hasPrefix(API.ERROR_PREFIX) { |
108 | | - return result |
109 | | - } |
110 | | - fatalError(String(resultString.dropFirst(API.ERROR_PREFIX.count))) |
111 | | - } |
| 6 | + private static let ERROR_PREFIX = "ERROR:" |
| 7 | + |
| 8 | + public init() {} |
| 9 | + |
| 10 | + // Parses a single KCL file and returns its Abstract Syntax Tree (AST) as a JSON string. |
| 11 | + public func parseFile(_ args: ParseFile_Args) throws -> ParseFile_Result { |
| 12 | + return try ParseFile_Result( |
| 13 | + serializedBytes: callNative(name: "KclvmService.ParseFile", args: try args.serializedBytes())) |
| 14 | + } |
| 15 | + |
| 16 | + // Parses a KCL program and returns the Abstract Syntax Tree (AST) in JSON format. |
| 17 | + public func parseProgram(_ args: ParseProgram_Args) throws -> ParseProgram_Result { |
| 18 | + return try ParseProgram_Result( |
| 19 | + serializedBytes: callNative( |
| 20 | + name: "KclvmService.ParseProgram", args: try args.serializedBytes())) |
| 21 | + } |
| 22 | + |
| 23 | + // Loads a KCL package and retrieves AST, symbol, type, and definition information. |
| 24 | + public func loadPackage(_ args: LoadPackage_Args) throws -> LoadPackage_Result { |
| 25 | + return try LoadPackage_Result( |
| 26 | + serializedBytes: callNative( |
| 27 | + name: "KclvmService.LoadPackage", args: try args.serializedBytes())) |
| 28 | + } |
| 29 | + |
| 30 | + // Executes a KCL file with provided arguments. |
| 31 | + public func execProgram(_ args: ExecProgram_Args) throws -> ExecProgram_Result { |
| 32 | + return try ExecProgram_Result( |
| 33 | + serializedBytes: callNative( |
| 34 | + name: "KclvmService.ExecProgram", args: try args.serializedBytes())) |
| 35 | + } |
| 36 | + |
| 37 | + // Overrides specified elements in a KCL file according to given arguments. |
| 38 | + public func overrideFile(_ args: OverrideFile_Args) throws -> OverrideFile_Result { |
| 39 | + return try OverrideFile_Result( |
| 40 | + serializedBytes: callNative( |
| 41 | + name: "KclvmService.OverrideFile", args: try args.serializedBytes())) |
| 42 | + } |
| 43 | + |
| 44 | + // Lists all variables declared in a KCL file. |
| 45 | + public func listVariables(_ args: ListVariables_Args) throws -> ListVariables_Result { |
| 46 | + return try ListVariables_Result( |
| 47 | + serializedBytes: callNative( |
| 48 | + name: "KclvmService.ListVariables", args: try args.serializedBytes())) |
| 49 | + } |
| 50 | + |
| 51 | + // Lists all options defined in a KCL program. |
| 52 | + public func listOptions(_ args: ParseProgram_Args) throws -> ListOptions_Result { |
| 53 | + return try ListOptions_Result( |
| 54 | + serializedBytes: callNative( |
| 55 | + name: "KclvmService.ListOptions", args: try args.serializedBytes())) |
| 56 | + } |
| 57 | + |
| 58 | + // Retrieves the full schema type mapping for a KCL program. |
| 59 | + public func getSchemaTypeMapping(_ args: GetSchemaTypeMapping_Args) throws |
| 60 | + -> GetSchemaTypeMapping_Result |
| 61 | + { |
| 62 | + return try GetSchemaTypeMapping_Result( |
| 63 | + serializedBytes: callNative( |
| 64 | + name: "KclvmService.GetSchemaTypeMapping", args: try args.serializedBytes())) |
| 65 | + } |
| 66 | + |
| 67 | + // Formats source code according to KCL style guidelines. |
| 68 | + public func formatCode(_ args: FormatCode_Args) throws -> FormatCode_Result { |
| 69 | + return try FormatCode_Result( |
| 70 | + serializedBytes: callNative(name: "KclvmService.FormatCode", args: try args.serializedBytes()) |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + // Formats KCL files or directories to conform to style guidelines. |
| 75 | + public func formatPath(_ args: FormatPath_Args) throws -> FormatPath_Result { |
| 76 | + return try FormatPath_Result( |
| 77 | + serializedBytes: callNative(name: "KclvmService.FormatPath", args: try args.serializedBytes()) |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + // Runs linting checks on KCL files and reports errors and warnings. |
| 82 | + public func lintPath(_ args: LintPath_Args) throws -> LintPath_Result { |
| 83 | + return try LintPath_Result( |
| 84 | + serializedBytes: callNative(name: "KclvmService.LintPath", args: try args.serializedBytes())) |
| 85 | + } |
| 86 | + |
| 87 | + // Validates a data string against a schema defined in a KCL code string. |
| 88 | + public func validateCode(_ args: ValidateCode_Args) throws -> ValidateCode_Result { |
| 89 | + return try ValidateCode_Result( |
| 90 | + serializedBytes: callNative( |
| 91 | + name: "KclvmService.ValidateCode", args: try args.serializedBytes())) |
| 92 | + } |
| 93 | + |
| 94 | + // Builds configuration from settings files. |
| 95 | + public func loadSettingsFiles(_ args: LoadSettingsFiles_Args) throws -> LoadSettingsFiles_Result { |
| 96 | + return try LoadSettingsFiles_Result( |
| 97 | + serializedBytes: callNative( |
| 98 | + name: "KclvmService.LoadSettingsFiles", args: try args.serializedBytes())) |
| 99 | + } |
| 100 | + |
| 101 | + // Renames symbols across files within a KCL package. |
| 102 | + public func rename(_ args: Rename_Args) throws -> Rename_Result { |
| 103 | + return try Rename_Result( |
| 104 | + serializedBytes: callNative(name: "KclvmService.Rename", args: try args.serializedBytes())) |
| 105 | + } |
| 106 | + |
| 107 | + // Renames symbols in source code without modifying files directly. |
| 108 | + public func renameCode(_ args: RenameCode_Args) throws -> RenameCode_Result { |
| 109 | + return try RenameCode_Result( |
| 110 | + serializedBytes: callNative(name: "KclvmService.RenameCode", args: try args.serializedBytes()) |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + // Executes tests on KCL packages using specified test arguments. |
| 115 | + public func test(_ args: Test_Args) throws -> Test_Result { |
| 116 | + return try Test_Result( |
| 117 | + serializedBytes: callNative(name: "KclvmService.Test", args: try args.serializedBytes())) |
| 118 | + } |
| 119 | + |
| 120 | + // Updates dependencies for a KCL project based on defined specifications. |
| 121 | + public func updateDependencies(_ args: UpdateDependencies_Args) throws |
| 122 | + -> UpdateDependencies_Result |
| 123 | + { |
| 124 | + return try UpdateDependencies_Result( |
| 125 | + serializedBytes: callNative( |
| 126 | + name: "KclvmService.UpdateDependencies", args: try args.serializedBytes())) |
| 127 | + } |
| 128 | + |
| 129 | + // Retrieves version information about the KCL service. |
| 130 | + public func getVersion(_ args: GetVersion_Args) throws -> GetVersion_Result { |
| 131 | + return try GetVersion_Result( |
| 132 | + serializedBytes: callNative(name: "KclvmService.GetVersion", args: try args.serializedBytes()) |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + private func callNative(name: String, args: Data) -> Data { |
| 137 | + // Convert name to byte array |
| 138 | + let nameBytes = [UInt8](name.utf8) |
| 139 | + var resultBuf = [UInt8](repeating: 0, count: 2048 * 2048) |
| 140 | + let resultLength = CKclLib.callNative( |
| 141 | + nameBytes, UInt(nameBytes.count), [UInt8](args), UInt(args.count), &resultBuf) |
| 142 | + let result = Data(bytes: resultBuf, count: Int(resultLength)) |
| 143 | + let resultString = String(bytes: result, encoding: .utf8) ?? "" |
| 144 | + if resultString.isEmpty || !resultString.hasPrefix(API.ERROR_PREFIX) { |
| 145 | + return result |
| 146 | + } |
| 147 | + fatalError(String(resultString.dropFirst(API.ERROR_PREFIX.count))) |
| 148 | + } |
112 | 149 | } |
0 commit comments