Git Product home page Git Product logo

Comments (26)

Peefy avatar Peefy commented on July 17, 2024 2

All right. Actually, we already have such an implementation within KCL at present https://github.com/kcl-lang/kcl/blob/main/kclvm/runtime/src/api/kclvm.rs#L270 However, due to bandwidth limitations, we have not yet exposed higher level APIs, and perhaps we may support such API calls next week.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024 1

I am developing a template system to process yaml files in our release process. The current tech stack is python+jinjia2. First, a jinjia template is defined, and then we use the following code to get the variables in the template:

def getjinja2vars(context):
    '''
    Get variables of type jinja2 str. To be extended.
    @param context:
    @return:
    '''
    # Common {{ abc }} => abc
    pattern1 = r'\{\{ ([\w\s]+) \}\}'
    var1 = re.findall(pattern1, context)
    # {% if abc == 2 %} => abc
    pattern2 = r'\{\%\s*if\s+(\S+)\s+'
    var2 = re.findall(pattern2, context)

    var1.extend(var2)
    return list(set(var1))

The code above gets the keys of the variables in jinja, and then completes the assignment of the keys through some logic, and finally renders the template and submits it. Therefore, if I want to switch to kcl, I need kcl to provide the same capability to get the variables defined in the template.

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024 1

This is the correct logic. The option function is not mandatory by default, and you can set additional parameters for it. e.g.

a = option("key1", default="value1", help="this is the message for key1")

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024 1

That’s excellent; thank you to the KCL team for your hard work.

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Hi @reckless-huang

At present, it is not possible to obtain the value of prod for all options mixed in the code snippet. However, we construct a schema to represent all variables through the code structure, similar to this

Schema Params:
  aa: int=1
  bb: int=2

params: Params = option("params")

Then obtain the document of the Params schema through the kcl doc tool or the document API.

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Additionally, could you please provide me with more specific scenarios and use cases? So that we can identify some best practices.

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

I see. In our current template abstraction system, we always define a Params schema to standardize user input, which allows us to enjoy the benefits of KCL type systems rather than just extracting regular expressions. Some examples are what we did in the KRM-KCL specification. https://github.com/kcl-lang/krm-kcl

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

How can I get the variable information defined in the schema in the KCL model, just like I use regular filtering of variable names in jinjia?

I see. In our current template abstraction system, we always define a Params schema to standardize user input, which allows us to enjoy the benefits of KCL type systems rather than just extracting regular expressions. Some examples are what we did in the KRM-KCL specification. https://github.com/kcl-lang/krm-kcl

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

How can I get the variable information defined in the schema in the KCL model, just like I use regular filtering of variable names in jinjia?

There are two ways

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

How can I get the variable information defined in the schema in the KCL model, just like I use regular filtering of variable names in jinjia?

There are two ways

Both of these methods seem to have a high threshold. I understand that parsing variables from templates is something that must be done before rendering, but why don't cue and kcl provide direct APIs for accessing them?

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

How can I get the variable information defined in the schema in the KCL model, just like I use regular filtering of variable names in jinjia?

There are two ways

Both of these methods seem to have a high threshold. I understand that parsing variables from templates is something that must be done before rendering, but why don't cue and kcl provide direct APIs for accessing them?

Make sense. For example, in KCL, if you want to directly use a certain parameter or API, such as ListOptions, to obtain a list of all parameters, right?

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

How can I get the variable information defined in the schema in the KCL model, just like I use regular filtering of variable names in jinjia?

There are two ways

Both of these methods seem to have a high threshold. I understand that parsing variables from templates is something that must be done before rendering, but why don't cue and kcl provide direct APIs for accessing them?

Make sense. For example, in KCL, if you want to directly use a certain parameter or API, such as ListOptions, to obtain a list of all parameters, right?

yes !
exp
ListOptions(file, code string) ([]args, err )

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

We've release kcl-go v0.8.1 See this function https://github.com/kcl-lang/kcl-go/blob/main/pkg/loader/options.go#L12 and examples https://github.com/kcl-lang/kcl-go/blob/main/pkg/loader/options_test.go#L8 to get all params (function option calling) from the kcl file or code.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

We've release kcl-go v0.8.1 See this function https://github.com/kcl-lang/kcl-go/blob/main/pkg/loader/options.go#L12 and examples https://github.com/kcl-lang/kcl-go/blob/main/pkg/loader/options_test.go#L8 to get all params (function option calling) from the kcl file or code.

image too large ,go get timeout

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Sorry, we are working hard on lighter KCL. 🙏 kcl-lang/kcl#291

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

We've release kcl-go v0.8.1 See this function https://github.com/kcl-lang/kcl-go/blob/main/pkg/loader/options.go#L12 and examples https://github.com/kcl-lang/kcl-go/blob/main/pkg/loader/options_test.go#L8 to get all params (function option calling) from the kcl file or code.

image image
	ops, err := loader.ListFileOptions("hello.k")
	if err != nil {
		panic(err)
	}
	for _, op := range ops {
		fmt.Println(op.Name)
	}

Eroor func not exist!

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Sorry, can you run rm -r ~/Library/Preferences/bin and try again?

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

Sorry, can you run rm -r ~/Library/Preferences/bin and try again?

image I tried deleting these files, but the regenerated files are still incorrect after running. Do I need to update my local kcl? The displayed version is still 8.0.0.

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Make sense. I suggest deleting all KCL related legacy files including which kcl and ~/Library/Preferences/bin in your local area and then running the go code again.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

I noticed that I replied under a topic that has already been closed. Do I need to create a new issue?

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Just here, although this should be an installation issue.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

It worked after I deleted the local kcl. It seems that there is logic that prioritizes using the kcl within the path.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024
	ops, err := loader.ListFileOptions("option.k")
	if err != nil {
		panic(err)
	}
	for _, op := range ops {
		//fmt.Println(op.Name)
		//fmt.Println(op.Type)
		//fmt.Println(op.Required)
		//fmt.Println(op.DefaultValue)
		fmt.Printf("变量名为%s类型是%v默认值为%v是否必填%v\n", op.Name, op.Type, op.DefaultValue, op.Required)
	}

kclcode

a = option("key1")
b = option("key2")
config = {
    metadata.name = option("name", type="str")
}

output
变量名为key1类型是默认值为是否必填false
变量名为key2类型是默认值为是否必填false
变量名为name类型是str默认值为是否必填false

The default value, type, and required value should not all be empty. Its right show?

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Yes, the default logic uses KCL in PATH, but it can be set through additional environment variables. I am refactoring this logic to make it easier to control through code. See the issue: #257

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

The simple demo package is 90MB in size, which seems a bit large.

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

Sorry, we are working hard on lighter KCL. 🙏 kcl-lang/kcl#291 It is expected that the size will be reduced by half by then.

This is because we used LLVM for AOT compilation for performance and compilation caching, which resulted in unfriendly cross platform, and therefore included the binary size of all platforms in KCL Go SDK.

from kcl-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.