可编程 可扩展 且 安全的 配置
bird.pkl
name = "Swallow"
job {
title = "Sr. Nest Maker"
company = "Nests R Us"
yearsOfExperience = 2
}
↓
{
"name": "Swallow",
"job": {
"title": "Sr. Nest Maker",
"company": "Nests R Us",
"yearsOfExperience": 2
}
}
name: Swallow
job:
title: Sr. Nest Maker
company: Nests R Us
yearsOfExperience: 2
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Swallow</string>
<key>job</key>
<dict>
<key>title</key>
<string>Sr. Nest Maker</string>
<key>company</key>
<string>Nests R Us</string>
<key>yearsOfExperience</key>
<integer>2</integer>
</dict>
</dict>
</plist>
name = Swallow
job.title = Sr. Nest Maker
job.company = Nests R Us
job.yearsOfExperience = 2
生成任何静态配置格式
将所有数据定义为Pkl,并生成JSON、YAML、属性列表和其他配置格式的输出。
应用集成配置
将 Pkl 嵌入到您的应用程序中以进行运行时配置,并接收 Java、Kotlin、Swift 和 Go 的代码生成。
module example.MyAppConfig
/// The hostname for the application
host: String
/// The port to listen on
port: UInt16(this > 1000)
package example;
public final class MyAppConfig {
/**
* The hostname for the application
*/
public final @NonNull String host;
/**
* The port to listen on
*/
public final int port;
public MyAppConfig(
@Named("host") @NonNull String host,
@Named("port") int port) {
this.host = host;
this.port = port;
}
public MyAppConfig withHost(@NonNull String host) { /*...*/ }
public MyAppConfig withPort(int port) { /*...*/ }
@Override
public boolean equals(Object obj) { /*...*/ }
@Override
public int hashCode() { /*...*/ }
@Override
public String toString() { /*...*/ }
}
package example
data class MyAppConfig(
/**
* The hostname for the application
*/
val host: String,
/**
* The port to listen on
*/
val port: Int
)
// Code generated from Pkl module `example.myAppConfig`. DO NOT EDIT.
enum MyAppConfig {}
extension MyAppConfig {
struct Module {
/// The hostname for the application
let host: String
/// The port to listen on
let port: UInt16
}
}
// Code generated from Pkl module `example.myAppConfig`. DO NOT EDIT.
package myappconfig
import (
"context"
"github.com/apple/pkl-go/pkl"
)
type MyAppConfig struct {
// The hostname for the application
Host string `pkl:"host"`
// The port to listen on
Port uint16 `pkl:"port"`
}
令人难以置信的IDE集成
为以与静态类型语言相同的简便性编写 Pkl 提供了出色的工具。我们为 IntelliJ、Visual Studio Code 和 Neovim 提供插件和扩展,并且还提供 PKL Language Server ,以便您可以轻松地进行自己的编辑器集成。
部署前捕捉错误
通过丰富的类型和验证系统,在部署应用程序之前捕获配置错误。
email: String = "dev-team@company.com"
port: Int(this > 1000) = 80
↓
–– Pkl Error ––
Type constraint `this > 1000` violated.
Value: 80
3 | port: Int(this > 1000) = 80
^^^^^^^^^^^
at config#port (config.pkl, line 3)
3 | port: Int(this > 1000) = 80
^^
at config#port (config.pkl, line 3)
106 | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^