-
Notifications
You must be signed in to change notification settings - Fork 16
Lua: initial implementation of a service based approach #295
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| local io = require("io") | ||
|
|
||
| local protoc = require("protoc") | ||
|
|
||
| local function main() | ||
| local p = protoc.new() | ||
| p.proto3_optional = true | ||
| local fh = assert(io.open("../spec/spec.proto", "r")) | ||
| local content = fh:read("*a") | ||
| fh:close() | ||
| local dump = p:compile(content, "spec.proto") | ||
| fh = assert(io.open("./kcl_lib/schema.lua", "w")) | ||
| assert(fh:write("return ")) | ||
| assert(fh:write(string.format("%q", dump))) | ||
| fh:close() | ||
| end | ||
|
|
||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| local api = require("kcl_lib.raw_api") | ||
| local types = require("kcl_lib.types") | ||
|
|
||
| ---@class kcl_lib.API | ||
| ---@field private raw kcl_lib.RawAPI The object to access the raw API. | ||
| local API = {} | ||
|
|
||
| ---Create a new API object. | ||
| ---@return kcl_lib.RawAPI | ||
| function API:new() | ||
| local o = { | ||
| raw = api, | ||
| } | ||
| setmetatable(o, self) | ||
| self.__index = self | ||
| o.overrides = {} | ||
| return o | ||
| end | ||
|
|
||
| ---Run a KCL program or set of programs and its output. | ||
| ---@param file string|string[] The file or list of files to run. | ||
| ---@return kcl_lib.types.RunResponse | ||
| function API:run(file) | ||
| if type(file) == "string" then | ||
| file = { file } | ||
| end | ||
| local args = { | ||
| k_filename_list = file, | ||
| } | ||
| local res = self.raw:exec_program(args) | ||
| return types.RunResponse:from_exec_program_result(res) | ||
| end | ||
|
|
||
| return API:new() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| 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.exec_program = | ||
| add_method("KclService.ExecProgram", "ExecProgramArgs", "ExecProgramResult") | ||
|
|
||
| RawAPI.format_path = | ||
| add_method("KclService.FormatPath", "FormatPathArgs", "FormatPathResult") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additional functions can be added by a single line like this in the future. I will do this in a second PR if this is accepted to support all the functions in the SDK. |
||
|
|
||
| return RawAPI:new() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I use very defensive programming here. This might not be desired if you want to propagate errors to the users. I currently implemented this so that the errors are easier to pin-point.