-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathraw_api.lua
More file actions
122 lines (99 loc) · 3.2 KB
/
raw_api.lua
File metadata and controls
122 lines (99 loc) · 3.2 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
local kcl_lib = require("kcl_lib")
local schema = require("kcl_lib.schema")
---@class kcl_lib.RawAPI
local RawAPI = {}
---Create a new API object.
---@return kcl_lib.RawAPI
function RawAPI:new()
local pb = require("pb")
pb.clear()
assert(pb.load(schema))
local o = {
pb = pb,
client = assert(kcl_lib.new_client(), "failed to create native KCL client"),
}
setmetatable(o, self)
self.__index = self
o.overrides = {}
return o
end
---Add a method to the raw API.
---@param name string The KCL service function name to call.
---@param arg_name string The name of the argument type that the method accepts.
---@param return_name string The name of the return type that the method returns.
---@return function
local function add_method(name, arg_name, return_name)
return function(self, args)
local arg_type = ".com.kcl.api." .. arg_name
args = assert(
self.pb.encode(arg_type, args),
"failed to encode argument into " .. arg_type
)
local res = assert(
self.client:call(name, args),
"failed to perform native call for method " .. name
)
local return_type = ".com.kcl.api." .. return_name
return assert(
self.pb.decode(return_type, res),
"failed to decode buffer into " .. return_type
)
end
end
RawAPI.ping = add_method("KclService.Ping", "PingArgs", "PingResult")
RawAPI.get_version =
add_method("KclService.GetVersion", "GetVersionArgs", "GetVersionResult")
RawAPI.parse_program = add_method(
"KclService.ParseProgram",
"ParseProgramArgs",
"ParseProgramResult"
)
RawAPI.parse_file =
add_method("KclService.ParseFile", "ParseFileArgs", "ParseFileResult")
RawAPI.load_package =
add_method("KclService.LoadPackage", "LoadPackageArgs", "LoadPackageResult")
RawAPI.list_options =
add_method("KclService.ListOptions", "ParseProgramArgs", "ListOptionsResult")
RawAPI.list_variables = add_method(
"KclService.ListVariables",
"ListVariablesArgs",
"ListVariablesResult"
)
RawAPI.exec_program =
add_method("KclService.ExecProgram", "ExecProgramArgs", "ExecProgramResult")
RawAPI.format_code =
add_method("KclService.FormatCode", "FormatCodeArgs", "FormatCodeResult")
RawAPI.format_path =
add_method("KclService.FormatPath", "FormatPathArgs", "FormatPathResult")
RawAPI.lint_path =
add_method("KclService.LintPath", "LintPathArgs", "LintPathResult")
RawAPI.override_file = add_method(
"KclService.OverrideFile",
"OverrideFileArgs",
"OverrideFileResult"
)
RawAPI.get_schema_type_mapping = add_method(
"KclService.GetSchemaTypeMapping",
"GetSchemaTypeMappingArgs",
"GetSchemaTypeMappingResult"
)
RawAPI.validate_code = add_method(
"KclService.ValidateCode",
"ValidateCodeArgs",
"ValidateCodeResult"
)
RawAPI.load_settings_files = add_method(
"KclService.LoadSettingsFiles",
"LoadSettingsFilesArgs",
"LoadSettingsFilesResult"
)
RawAPI.rename = add_method("KclService.Rename", "RenameArgs", "RenameResult")
RawAPI.rename_code =
add_method("KclService.RenameCode", "RenameCodeArgs", "RenameCodeResult")
RawAPI.test = add_method("KclService.Test", "TestArgs", "TestResult")
RawAPI.update_dependencies = add_method(
"KclService.UpdateDependencies",
"UpdateDependenciesArgs",
"UpdateDependenciesResult"
)
return RawAPI:new()