Skip to content

chore: update and format faq documents #401

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

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference/lang/tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ assert x.upper() == "LENGTH"

There are 2 different ways to format a string: to use the `"{}".format()` built-in function, or to specify the variable between the curly braces and use a `${}` mark to tell KCL to extract its value. This is called **string interpolation** in KCL. In following example, both `a` and `b` will be assigned to string `"hello world"`.

Besides, the variable to serialized can be extracted in special data format, such as YAML or JSON. In this case, a `#yaml` or `#json` can be included within the curly braces.
Besides, the variable to serialized can be extracted in special data format, such as YAML or JSON. In this case, a `#yaml` or `#json` can be included within the curly braces.

Note that if we don't want to interpolate variables, we can add the `\` character before `$`.

Expand Down
84 changes: 42 additions & 42 deletions docs/user_docs/guides/working-with-konfig/3-quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ spec:
app.k8s.io/component: sampleappdev
spec:
containers:
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:1.7.8
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
requests:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
volumeMounts: []
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:1.7.8
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
requests:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
volumeMounts: []
---
apiVersion: v1
kind: Namespace
Expand All @@ -100,9 +100,9 @@ metadata:
namespace: sampleapp
spec:
ports:
- nodePort: 30201
port: 80
targetPort: 80
- nodePort: 30201
port: 80
targetPort: 80
selector:
app.kubernetes.io/name: sampleapp
app.kubernetes.io/env: dev
Expand Down Expand Up @@ -157,24 +157,24 @@ spec:
app.k8s.io/component: sampleappdev
spec:
containers:
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:latest
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
requests:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
volumeMounts: []
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:latest
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
requests:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
volumeMounts: []
---
apiVersion: v1
kind: Namespace
Expand All @@ -188,9 +188,9 @@ metadata:
namespace: sampleapp
spec:
ports:
- nodePort: 30201
port: 80
targetPort: 80
- nodePort: 30201
port: 80
targetPort: 80
selector:
app.kubernetes.io/name: sampleapp
app.kubernetes.io/env: dev
Expand Down
12 changes: 6 additions & 6 deletions docs/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -2650,17 +2650,17 @@ After you set the `KCL_CACHE_PATH`, when you run any KCL commands, the `.kclvm`

## 65. How to join a list to a string in KCL?

If we want to join a given list L = ['a', 'b', 'c'] into a string with some sepeartor (like a comma ",").
If we want to join a given list L = ['a', 'b', 'c'] into a string with some separator (like a comma ",").

```bash
```python
S = ",".join(['a', 'b', 'c'])
```

## 66. Is it possible to support schema lambda (class methods) in KCL?

KCL supports defining member functions for schemas and can omit them. An example KCL code is shown below for the same:

```KCL
```python
schema Person:
firstName: str
lastName: str
Expand All @@ -2677,7 +2677,7 @@ fullName = p.getFullName()

The above KCL code gives the output:

```bash
```yaml
p:
firstName: Alice
lastName: White
Expand All @@ -2688,7 +2688,7 @@ fullName: Alice White

You need to add specifically the type into the schema. An example code shown below:

```KCL
```python
schema FooBar:
mixin [
FooBarMixin
Expand All @@ -2709,7 +2709,7 @@ foobar = _c.foobar

returns the output:

```bash
```yaml
foobar: foo.bar
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ image: /img/biweekly-newsletter.png
- 三方库 victoria-metrics-operator 升级到 0.45.1 版本,新增了一些检查语句的提示信息。
- 三方库 istio 升级到 1.21.2 版本,调整了一些 check 规则,新增了文档。
- 三方库 jsonpatch 升级到 0.0.5 版本,支持 rfc6901Decode。

**🏄 语言更新**

- 增加了 centos7 amd64 的构建环境。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ schema Deployment[priority]:
_cpu = 1024
else:
_cpu = 2048

cpu: int = _cpu
memory: int = _cpu * 2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public class PluginTest {
API.registerPlugin("my_plugin", Collections.singletonMap("add", (args, kwArgs) -> {
return (int) args[0] + (int) args[1];
}));

API api = new API();
ExecProgram_Result result = api
.execProgram(ExecProgram_Args.newBuilder().addKFilenameList("test.k").build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ spec:
app.k8s.io/component: sampleappdev
spec:
containers:
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:1.7.8
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
requests:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
volumeMounts: []
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:1.7.8
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
requests:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
volumeMounts: []
---
apiVersion: v1
kind: Namespace
Expand All @@ -100,9 +100,9 @@ metadata:
namespace: sampleapp
spec:
ports:
- nodePort: 30201
port: 80
targetPort: 80
- nodePort: 30201
port: 80
targetPort: 80
selector:
app.kubernetes.io/name: sampleapp
app.kubernetes.io/env: dev
Expand All @@ -122,9 +122,9 @@ metadata:
namespace: sampleapp
spec:
ports:
- nodePort: 30201
port: 80
targetPort: 80
- nodePort: 30201
port: 80
targetPort: 80
selector:
app.kubernetes.io/name: sampleapp
app.kubernetes.io/env: prod
Expand Down Expand Up @@ -179,24 +179,24 @@ spec:
app.k8s.io/component: sampleappdev
spec:
containers:
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:latest
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
requests:
cpu: '100m'
memory: '100Mi'
ephemeral-storage: '1Gi'
volumeMounts: []
- env:
- name: MY_ENV
value: MY_VALUE
image: nginx:latest
name: main
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
requests:
cpu: "100m"
memory: "100Mi"
ephemeral-storage: "1Gi"
volumeMounts: []
---
apiVersion: v1
kind: Namespace
Expand All @@ -210,9 +210,9 @@ metadata:
namespace: sampleapp
spec:
ports:
- nodePort: 30201
port: 80
targetPort: 80
- nodePort: 30201
port: 80
targetPort: 80
selector:
app.kubernetes.io/name: sampleapp
app.kubernetes.io/env: dev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,72 @@ export KCL_CACHE_PATH=/tmp # 或者更改为您想要的路径
setx KCL_CACHE_PATH "C:\temp" /M
```

## 65. 如何在 KCL 中使用 "import another-module" ? 包名中包含横线 "-"
## 65. 如何在 KCL 中将列表连接成字符串?

如果我们想要将给定的列表 `L = ['a', 'b', 'c']` 用特定的分隔符(如逗号 ",")连接成一个字符串,可以使用以下 KCL 代码:

```python
S = ",".join(['a', 'b', 'c'])
```

## 66. 在 KCL 中是否支持 schema lambda(类方法)?

KCL 支持为 schema 使用 lambda 定义成员函数。下面是一个相关的KCL示例代码:

```python
schema Person:
firstName: str
lastName: str
getFullName: () -> str = lambda {
firstName + " " + lastName
}

p = Person{
firstName = "Alice"
lastName = "White"
}
fullName = p.getFullName()
```

上述 KCL 代码产生的输出:

```yaml
p:
firstName: Alice
lastName: White
fullName: Alice White
```

## 67. 在 mixin 外部使用混合属性是否需要转换为 any 类型?

需要将类型明确地添加到 schema 中即可使用混合属性。以下是一个示例代码:

```python
schema FooBar:
mixin [
FooBarMixin
]
foo: str = 'foo'
bar: str = 'bar'

protocol FooBarProtocol:
foo: str
bar: str

mixin FooBarMixin for FooBarProtocol:
foobar: str = "${foo}.${bar}" # 带有类型注解的属性可以在模式外部访问。

_c = FooBar {}
foobar = _c.foobar
```

输出为:

```yaml
foobar: foo.bar
```

## 68. 如何在 KCL 中使用 "import another-module" ? 包名中包含横线 "-"

在 KCL 中,import 语句中使用的模块名称中只支持`_`,kcl.mod 中的包名同时支持 `-` 和 `_`,KCL 编译器会自动将包名中的 `-` 替换为 `_`。

Expand Down
Loading
Loading