Comments (26)
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.
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.
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.
That’s excellent; thank you to the KCL team for your hard work.
from kcl-go.
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.
Additionally, could you please provide me with more specific scenarios and use cases? So that we can identify some best practices.
from kcl-go.
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.
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.
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
- CLI: kcl doc gen
- API Document API or lower level Type API
from kcl-go.
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
CLI: kcl doc gen
API Document API or lower level Type API
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.
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
CLI: kcl doc gen
API Document API or lower level Type API
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.
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
CLI: kcl doc gen
API Document API or lower level Type API
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.
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.
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](https://private-user-images.githubusercontent.com/53162954/313367319-ec911979-3fc0-4a07-b6d4-4abd971aac8f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjExNDg1MzksIm5iZiI6MTcyMTE0ODIzOSwicGF0aCI6Ii81MzE2Mjk1NC8zMTMzNjczMTktZWM5MTE5NzktM2ZjMC00YTA3LWI2ZDQtNGFiZDk3MWFhYzhmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE2VDE2NDM1OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTFjNGY5YTczOTFjYjBiY2MxMjM0OTI0OGNjMDViMzQzOGYzOTQ2OTcyYjNiYTdlMjg1MWE0M2VkZWFiMTAzM2UmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.2IGbtD2frK_VttAuYYw2jRA5atjL2JaOl3q5D7nYHyk)
from kcl-go.
Sorry, we are working hard on lighter KCL. 🙏 kcl-lang/kcl#291
from kcl-go.
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](https://private-user-images.githubusercontent.com/53162954/313899225-4278e7b4-f6c5-4c34-83dc-44ce391e8b14.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjExNDg1MzksIm5iZiI6MTcyMTE0ODIzOSwicGF0aCI6Ii81MzE2Mjk1NC8zMTM4OTkyMjUtNDI3OGU3YjQtZjZjN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Q3OTgwNDk1MWM5Y2ZhMTBhN2NjNmE1YzdlNWRkNzExN2EwZjY5ZWFlMTVhYzgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.pJT9Am8NVtscyBJWxgn0yPbTmoGdQAyVhuo8uFPAU1Q)
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.
Sorry, can you run rm -r ~/Library/Preferences/bin
and try again?
from kcl-go.
Sorry, can you run
rm -r ~/Library/Preferences/bin
and try again?
![image](https://private-user-images.githubusercontent.com/53162954/313902086-f6127d10-ef26-4474-b170-a7eb33ec0998.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjExNDg1MzksIm5iZiI6MTcyMTE0ODIzOSwicGF0aCI6Ii81MzE2Mjk1NC8zMTM5MDIwODYtZjYxMjdkMTAtZWYyNi00NDc0LWIxNzAtYTdlYjMzZWMwOTk4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE2VDE2NDM1OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTExNjlkYjAyMmEzM2M5NzJjNDRmMTViZDZmY2NjOTg0NDUwOWVlNTQ5NTA0OTVmZmJhMGQxNDAyY2MyNTM1ZDAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.e3yVWIrr7SMCy9ChDzeROIcgbJWfFFS7ajp8_RVpSWk)
from kcl-go.
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.
I noticed that I replied under a topic that has already been closed. Do I need to create a new issue?
from kcl-go.
Just here, although this should be an installation issue.
from kcl-go.
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.
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.
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.
The simple demo package is 90MB in size, which seems a bit large.
from kcl-go.
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)
- WithSettings should merge options instead of overwriting HOT 5
- [Enhancement] KCL File Generate API for GitClickOps
- [Enhancement] Add AllOf validations for KCL jsonschema generations HOT 4
- Import tool use wrong mode and generate schema with error
- KCL Go Plugin API and documents HOT 1
- error get depends HOT 2
- Disable auto block pr option of contributor-assistant/github-action HOT 1
- Request: allow disabling shared lib download behavior at build time HOT 8
- Inconsistent native and service KCL api `ParseFile` behaviour HOT 2
- version miss match HOT 2
- ‘_’ seems to be a bit unexpected. HOT 3
- [Enhancement] kcl import muti yaml resource HOT 3
- how to give a dict var HOT 7
- Get locally depended .k files of a given .k file (from imports)
- [Enhancement] Gen KCL config from toml config. HOT 14
- [Enhancement] Gen KCL schema from protobuf message HOT 5
- [Enhancement] Gen KCL config from textproto and KCL type/protobuf message
- [Enhancement] Import multiple documents in single YAML file into one KCL file with the `manifests.yaml_stream` function calling.
- [Enhancement] KCL Import supports importing the entire Go package into KCL schema HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kcl-go.