Skip to content

Commit

Permalink
feat: enhance get schema ty API with more information.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Aug 1, 2023
1 parent 17c4cbe commit abfd7e2
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 32 deletions.
18 changes: 14 additions & 4 deletions internal/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ message ExecProgram_Args {

// yaml/json: sort keys
bool sort_keys = 12;
// include schema type path in JSON/YAML result
bool include_schema_type_path = 13;

// -E --external : external packages path
repeated CmdExternalPkgSpec external_pkgs = 14;
repeated CmdExternalPkgSpec external_pkgs = 13;
}

message ExecProgram_Result {
string json_result = 1;
string yaml_result = 2;
Expand Down Expand Up @@ -281,7 +280,7 @@ message KeyValuePair {
}

// ----------------------------------------------------------------------------
// JSON Schema Lit
// KCL Type Structure
// ----------------------------------------------------------------------------

message KclType {
Expand All @@ -300,6 +299,11 @@ message KclType {
int32 line = 10;

repeated Decorator decorators = 11; // schema decorators

string filename = 12; // `filename` represents the absolute path of the file name where the attribute is located.
string pkg_path = 13; // `pkg_path` represents the path name of the package where the attribute is located.
string description = 14; // `description` represents the document of the attribute.
map<string, Example> examples = 15; // A map object to hold examples, the key is the example name.
}

message Decorator {
Expand All @@ -308,6 +312,12 @@ message Decorator {
map<string, string> keywords = 3;
}

message Example {
string summary = 1; // Short description for the example.
string description = 2; // Long description for the example.
google.protobuf.Any value = 3; // Embedded literal example.
}

// ----------------------------------------------------------------------------
// END
// ----------------------------------------------------------------------------
5 changes: 3 additions & 2 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
fn test_c_api<A, R, F>(svc_name: &str, input: &str, output: &str, wrapper: F)
where
A: Message + DeserializeOwned,
R: Message + Default + PartialEq + DeserializeOwned + serde::Serialize,
R: Message + Default + PartialEq + DeserializeOwned + serde::Serialize + ?Sized,
F: Fn(&mut R),
{
let _test_lock = TEST_MUTEX.lock().unwrap();
Expand All @@ -122,6 +122,7 @@ where
let result = unsafe { CStr::from_ptr(result_ptr) };

let mut result = R::decode(result.to_bytes()).unwrap();
let result_json = serde_json::to_string(&result).unwrap();
let except_result_path = Path::new(TEST_DATA_PATH).join(output);
let except_result_json = fs::read_to_string(&except_result_path).unwrap_or_else(|_| {
panic!(
Expand All @@ -132,7 +133,7 @@ where
let mut except_result = serde_json::from_str::<R>(&except_result_json).unwrap();
wrapper(&mut result);
wrapper(&mut except_result);
assert_eq!(result, except_result);
assert_eq!(result, except_result, "result json is {}", result_json);
unsafe {
kclvm_service_delete(serv);
kclvm_service_free_string(result_ptr);
Expand Down
1 change: 0 additions & 1 deletion kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ impl KclvmServiceImpl {
&FormatOptions {
recursively,
is_stdout: false,
..Default::default()
},
)?;
Ok(FormatPathResult { changed_paths })
Expand Down
10 changes: 8 additions & 2 deletions kclvm/api/src/service/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub(crate) fn kcl_schema_ty_to_pb_ty(schema_ty: &SchemaType) -> KclType {
..Default::default()
})
.collect(),
filename: schema_ty.filename.clone(),
pkg_path: schema_ty.pkgpath.clone(),
description: schema_ty.doc.clone(),
..Default::default()
}
}
Expand All @@ -62,6 +65,9 @@ fn get_schema_ty_attributes(schema_ty: &SchemaType, line: &mut i32) -> HashMap<S
if key != SCHEMA_SETTINGS_ATTR_NAME {
let mut ty = kcl_ty_to_pb_ty(&attr.ty);
ty.line = *line;
ty.description = attr.doc.clone().unwrap_or_default();
ty.filename = schema_ty.filename.clone();
ty.pkg_path = schema_ty.pkgpath.clone();
type_mapping.insert(key.to_string(), ty);
*line += 1
}
Expand All @@ -79,8 +85,8 @@ fn get_schema_ty_required_attributes(schema_ty: &SchemaType) -> Vec<String> {
Vec::new()
};
let mut attr_set = IndexSet::new();
for (key, _) in &schema_ty.attrs {
if key != SCHEMA_SETTINGS_ATTR_NAME {
for (key, attr) in &schema_ty.attrs {
if key != SCHEMA_SETTINGS_ATTR_NAME && !attr.is_optional {
attr_set.insert(key.to_string());
}
}
Expand Down
2 changes: 1 addition & 1 deletion kclvm/api/src/testdata/get-schema-type-mapping.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"file": "schema.k",
"code": "schema Person:\n name: str\n age: int\n\nperson = Person {\n name = \"Alice\"\n age = 18\n}\n"
"code": "schema Person:\n '''Schema documents'''\n name: str\n age?: int\n\nperson = Person {\n name = \"Alice\"\n age = 18\n}\n"
}
58 changes: 40 additions & 18 deletions kclvm/api/src/testdata/get-schema-type-mapping.response.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"union_types": [],
"default": "",
"schema_name": "Person",
"schema_doc": "",
"schema_doc": "Schema documents",
"properties": {
"age": {
"type": "int",
Expand All @@ -16,7 +16,11 @@
"properties": {},
"required": [],
"line": 2,
"decorators": []
"decorators": [],
"filename": "schema.k",
"pkg_path": "__main__",
"description": "",
"examples": {}
},
"name": {
"type": "str",
Expand All @@ -27,52 +31,70 @@
"properties": {},
"required": [],
"line": 1,
"decorators": []
"decorators": [],
"filename": "schema.k",
"pkg_path": "__main__",
"description": "",
"examples": {}
}
},
"required": [
"name",
"age"
"name"
],
"line": 0,
"decorators": []
"decorators": [],
"filename": "schema.k",
"pkg_path": "__main__",
"description": "Schema documents",
"examples": {}
},
"Person": {
"type": "schema",
"union_types": [],
"default": "",
"schema_name": "Person",
"schema_doc": "",
"schema_doc": "Schema documents",
"properties": {
"name": {
"type": "str",
"age": {
"type": "int",
"union_types": [],
"default": "",
"schema_name": "",
"schema_doc": "",
"properties": {},
"required": [],
"line": 1,
"decorators": []
"line": 2,
"decorators": [],
"filename": "schema.k",
"pkg_path": "__main__",
"description": "",
"examples": {}
},
"age": {
"type": "int",
"name": {
"type": "str",
"union_types": [],
"default": "",
"schema_name": "",
"schema_doc": "",
"properties": {},
"required": [],
"line": 2,
"decorators": []
"line": 1,
"decorators": [],
"filename": "schema.k",
"pkg_path": "__main__",
"description": "",
"examples": {}
}
},
"required": [
"name",
"age"
"name"
],
"line": 0,
"decorators": []
"decorators": [],
"filename": "schema.k",
"pkg_path": "__main__",
"description": "Schema documents",
"examples": {}
}
}
}
2 changes: 0 additions & 2 deletions kclvm/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ pub struct ExecProgramArgs {
pub debug: i32,
// yaml/json: sort keys
pub sort_keys: bool,
// include schema type path in JSON/YAML result
pub include_schema_type_path: bool,
// plugin_agent is the address of plugin.
#[serde(skip)]
pub plugin_agent: u64,
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runner/src/test_datas/exec_prog_args/default.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"work_dir":null,"k_filename_list":[],"external_pkgs":[],"k_code_list":[],"args":[],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false,"include_schema_type_path":false}
{"work_dir":null,"k_filename_list":[],"external_pkgs":[],"k_code_list":[],"args":[],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false}
2 changes: 1 addition & 1 deletion kclvm/runner/src/test_datas/settings_file/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"work_dir":null,"k_filename_list":["../main.k","./before/base.k","./main.k","./sub/sub.k"],"external_pkgs":[],"k_code_list":[],"args":[{"name":"app-name","value":"\"kclvm\""},{"name":"image","value":"\"kclvm:v0.0.1\""}],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false,"include_schema_type_path":false}
{"work_dir":null,"k_filename_list":["../main.k","./before/base.k","./main.k","./sub/sub.k"],"external_pkgs":[],"k_code_list":[],"args":[{"name":"app-name","value":"\"kclvm\""},{"name":"image","value":"\"kclvm:v0.0.1\""}],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false}

0 comments on commit abfd7e2

Please sign in to comment.