kcl-lang / kcl-lang.io

Source of the KCL Website. https://kcl-lang.io
https://kcl-lang.github.io
Apache License 2.0
12 stars 33 forks source link

How to loop through the properties of a schema? #393

Closed kubernegit closed 2 weeks ago

kubernegit commented 2 weeks ago

I want to use kcl as single source of truth to generate some configuration files that can be consumed by other processes. When consuming these json files with python it would be good to have type information of the structure of the data available in python. For that I want to generate a python file that has the same structure as a kcl schema. This way the python linter knows which data to expect and runtime errors can be prevented.

For example this .k-file:

schema MySchema: 
    mykey: str

should be converted to this python code:

from typing import TypedDict

class MySchema(TypedDict):
    mykey: str

My question: Is there a kcl-native way to loop through the properties of a schema in order to generate code of the python class?

Peefy commented 2 weeks ago

For example in KCL Python SDK: https://github.com/kcl-lang/lib/pull/95

Then you can use the kcl type to generate any type in any language.

kubernegit commented 2 weeks ago

@Peefy Thank you! :)