Skip to content
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

[FAQ] What is the equivalent of cue export in KCL #482

Open
fchastanet opened this issue Oct 2, 2024 · 2 comments
Open

[FAQ] What is the equivalent of cue export in KCL #482

fchastanet opened this issue Oct 2, 2024 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@fchastanet
Copy link

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
Copy link
Author

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
Copy link
Contributor

Peefy commented Oct 10, 2024

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 👍.

@Peefy Peefy added enhancement New feature or request question Further information is requested labels Oct 10, 2024
@Peefy Peefy transferred this issue from kcl-lang/kcl Nov 6, 2024
@Peefy Peefy changed the title equivalent to cue export [FAQ] What is the equivalent of cue export in KCL Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants