Skip to content

linux-china/pkl-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pkl demo

Pkl(pickle): a configuration as code language with rich validation and tooling.

Features

  • Generate any static configuration format
  • Integrated application configuration
  • Incredible IDE Integration
  • Catch errors before deployment

How to write configuration item?

name: Type(constraint1, constraint2) = value | expression
  • name:也就是配置项的名称,你使用标准的变量名即可,注意:Pkl不支持server.port这种方式,而是要调整为server{port=8080}这样
  • Type: Pkl是强类型语言,所以你需要指定类型,如UInt16,Int16,UInt32,Int32, String等。如果你不指定类型,Pkl会自动推断类型,如String等。开始你可能不习惯一个Number怎么这么多类型,搞得和Rust一样,这个主要是起到类型约束作用,保证你不会写错配置项的值。
  • constraint:配置项值的约束条件,即便有了类型,但是还是更进一步的值约束,如数值区间,字符串格式等,如UInt16(isBetween(0, 1023)),这个表示值必须在0-1023之间。 constraint主要为boolean类型函数和表达式,如isBetween(0, 1023)this > 1024等,多个约束条件用逗号分隔。
  • value: 配置项的值,可以是固定字面量,如8080,"support@example.com",也可以是表达式,如缓存时间如果是秒的话,那么24*60*60可能要比86400更直观。

Example:

name: String(length.isOdd, chars.first == chars.last)
zipCode: String(matches(Regex("\\d{5}")))
serverPort: UInt16(isBetween(0, 1023))
port: Int(this > 1000) = 80

更多的constraint函数,请访问: https://pkl-lang.org/package-docs/pkl/0.25.2/base/index.html

Java code generate

Execute mvn exec:java or mvn compile to generate the Java code from pkl.

If you use JBang you can run the following command to generate the Java code from pkl.

jbang run --main org.pkl.codegen.java.Main "org.pkl-lang:pkl-tools:0.25.2" --help

or execute jbang pkl-cli or jbang pkl-codegen with jbang-catalog.json.

References

About

Pkl Language demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published