kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.67k stars 118 forks source link

equivalent to cue export #1671

Open fchastanet opened 1 month ago

fchastanet commented 1 month ago

General Question

Hello,

Maybe I didn't get the aim of your tool. I found the way to validate my yaml file

---
vars:
  AA: test
binData:
  commands:
    default:
      mainFile: null
      definitionFiles:
        - file: "test"
          order: 1
        - file: "test2"
          order: 1

with this k file

import regex

schema BinFileSchema:
  vars?: VarsSchema
  compilerConfig?: CompilerConfigSchema
  binData: BinDataSchema
  test: str = "toto"

schema BinDataSchema:
  commands: CommandsSchema

schema CompilerConfigSchema:
  test?: str = "toto"

schema DefaultCommandSchema:
  mainFile?: str
  definitionFiles?: [DefinitionFilesSchema]
  check:
    definitionFiles and isunique([ int(_x["order"]) \
      for _, _x in definitionFiles]) if definitionFiles, \
      "definitionFiles: order property value should be unique, check for duplicates"

schema CommandsSchema:
  default: DefaultCommandSchema

schema DefinitionFilesSchema:
  file: str
  order: int
  check:
    order > 0, "order must >0"

schema VarsSchema:
  [attr: str]: str
  check:
    regex.match(attr, r'^[A-Z0-9_]+$')

Using the validation command: kcl vet testsKcl/data.yaml binFile.k --format yaml

it works very well

but I didn't find the right command or ability to import my yaml file to be processed by the k file in order to interpolate the default properties that would be missing from my yaml file (eg: the test properties of the k file).

I tried several commands but no success to generate any viable output: kcl run data.yaml binFile.k --format yaml

Here I get an empty output {}

The command kcl -Y data.yaml binFile.k --format yaml gives {} as well

what am I missing ?

kind regards

fchastanet commented 1 month ago

I finally fix the issue by slightly changing my k file to accept a yaml file as option that I can read

import regex
import yaml
import file

configFile = option(key="configFile", type='str', required=True, help="load config file")

schema BinFileSchema:
  vars?: VarsSchema
  compilerConfig?: CompilerConfigSchema
  binData: BinDataSchema
  test: str = "toto"

schema BinDataSchema:
  commands: CommandsSchema

schema CompilerConfigSchema:
  test: str = "toto"

schema DefaultCommandSchema:
  mainFile?: str
  definitionFiles?: [DefinitionFilesSchema]
  check:
    definitionFiles and isunique([ int(_x["order"]) \
      for _, _x in definitionFiles]) if definitionFiles, \
      "definitionFiles: order property value should be unique, check for duplicates"

schema CommandsSchema:
  default: DefaultCommandSchema

schema DefinitionFilesSchema:
  file: str
  order: int
  check:
    order > 0, "order must >0"

schema VarsSchema:
  [attr: str]: str
  check:
    regex.match(attr, r'^[A-Z0-9_]+$')

configYaml:BinFileSchema = yaml.decode(file.read(configFile))

yaml.encode(configYaml)

Then the command kcl binFile.k --format yaml -D configFile=data.yaml

is it the right way to go ? is there a simple way ? Can I suggest you to provide a help page for the ones that are migrating from cue or other tools with recipes to migrate.

Peefy commented 3 weeks ago

Because KCL is not completely power-law like CUE, it does not support directly inputting YAML/JSON files at the command line. It is more like using CUE's @embed to input YAML/JSON in KCL to use file.read. Providing help with migrating from CUE to KCL or a concept comparison document is a good suggestion 👍.