Skip to content

kcl输出格式仅支持json和yaml,期望可以灵活扩展 #2051

Description

@yunyezi1

Enhancement

当前kcl执行仅支持输出json和yaml格式,目前项目中遇到期望输出jsonschema, xml等多种类型格式,当前开源代码存在如下问题:

  1. 不管是否使用json或yaml格式,每次执行时必定会进行翻译生成,即使只用其中一种格式,也会生成输出,浪费了计算成本。
  2. 当前协议接口中固定了输出格式为json和yaml两种,无法进行灵活扩展。即使对runtime中扩展了新的格式生成器也无法直接使用。

对社区建议:

  1. kcl命令行支持--format命令,用于指定编译kcl源码执行后生成格式,使用示例如下:
kcl [run] --format [yaml|json|...] hello.k

针对--format描述通过kcl run -h展示

--format string           Specify the output format (default "yaml")
  1. kcl的前后端通信协议扩展gpyrpc.proto中,对ExecProgram_Result结构进行改造,支持扩展兼容:
    将result_yaml和result_json修改为out_format和out_message
    当--format=json时,out_format输出json, out_message输出对应json内容,当--format=yaml时,out_format输出yaml, out_message输出对应yaml内容
// Message for execute program response.
message ExecProgram_Result {
	// Result in Yaml/Json/Jsonschema/Xml format.
	string out_format = 1;
	// Out message from execution.
	string out_message = 2;
	// Log message from execution.
	string log_message = 3;
	// Error message from execution.
	string err_message = 4;
}

举例如下:
输出json格式:

[11:19:43]ci@~/kcl-tool> kcl --format json ../tmp/compiler/test/test_schema.k
{
    "server": {
        "image": "nginx:1.20",
        "replicas": 3,
        "labels": {
            "app": "web",
            "env": "production"
        },
        "port": 8080
    },
    "database": {
        "host": "localhost",
        "port": 5432,
        "name": "myapp",
        "ssl": true
    },
    "app_name": "my-application",
    "version": "1.0.0",
    "debug": false
}

输出yaml格式:

[12:06:52]ci@~/kcl-tool> kcl --format yaml ../tmp/compiler/test/test_schema.k
server:
  image: nginx:1.20
  replicas: 3
  labels:
    app: web
    env: production
  port: 8080
database:
  host: localhost
  port: 5432
  name: myapp
  ssl: true
app_name: my-application
version: '1.0.0'
debug: false

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttoolIssues or PRs related to kcl tools inlucding format, lint, validation, document tools, etc.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions