Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pkl/2024-04-17-llt31
Search
Satoshi SAKAO
April 17, 2024
Programming
0
65
Pkl/2024-04-17-llt31
社内のLTイベント「えるLT Vol.31 オンライン」で発表した資料です
Satoshi SAKAO
April 17, 2024
Tweet
Share
More Decks by Satoshi SAKAO
See All by Satoshi SAKAO
Testcontainers/2024-11-20-llt32
ottijp
0
28
JavaScriptのデバッグ/2023-09-04-llt30
ottijp
0
130
CDK for TerraformでAzureリソースをデプロイする/2023-05-15-llt29
ottijp
1
210
TWELITEへの誘い/2022-12-27-llt28
ottijp
0
130
ビルドツールBazelを触ってみた/2022-09-28-llt27
ottijp
0
140
HashiCorp Vaultを使ったシークレットのセキュアな一元管理 〜Ansibleを添えて〜/2022-07-12-llt26
ottijp
0
130
AWSインフラのデプロイをCDKでカイゼンする/2022-03-23-llt25
ottijp
0
73
Amazon Timestreamでデータ補間/2021-12-27-llt24
ottijp
0
88
ncurses/2021-05-12-llt21
ottijp
0
190
Other Decks in Programming
See All in Programming
Djangoにおける複数ユーザー種別認証の設計アプローチ@DjangoCongress JP 2025
delhi09
PRO
2
110
Conform を推す - Advocating for Conform
mizoguchicoji
3
690
Ruby on cygwin 2025-02
fd0
0
150
Amazon Q Developer Proで効率化するAPI開発入門
seike460
PRO
0
110
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
230
Amazon S3 TablesとAmazon S3 Metadataを触ってみた / 20250201-jawsug-tochigi-s3tables-s3metadata
kasacchiful
0
170
Writing documentation can be fun with plugin system
okuramasafumi
0
120
1年目の私に伝えたい!テストコードを怖がらなくなるためのヒント/Tips for not being afraid of test code
push_gawa
0
210
なぜイベント駆動が必要なのか - CQRS/ESで解く複雑系システムの課題 -
j5ik2o
12
4.1k
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
楽しく向き合う例外対応
okutsu
0
150
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
850
Featured
See All Featured
It's Worth the Effort
3n
184
28k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
960
Practical Orchestrator
shlominoach
186
10k
A Tale of Four Properties
chriscoyier
158
23k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Thoughts on Productivity
jonyablonski
69
4.5k
Why Our Code Smells
bkeepers
PRO
336
57k
How to Ace a Technical Interview
jacobian
276
23k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Transcript
Pkl Satoshi SAKAO えるLT Vol.31 2024-04-17 1
話すひと 2 🏢 インフォコム株式会社 サービスマネジメント室 👨🔧 ソフトウェアエンジニア 🛠 Node.js /
AWS / IoT / iOS (Swift) / Linux 💖 猫,B'z,テクテクライフ(ランク: 29) Satoshi SAKAO @ottijp.com 𝕏 @ottijp
Now playing... 3 https://bsky.app/pro fi le/ottijp.com/post/3kms7pxk2a326
Pkl • 「ぴっくる」 • OSS (since 2024-02-01) by Apple •
Con fi guration as Code • 静的設定ファイル < Pkl < プログラミング言語 4
ユースケース 5 Pkl JSON YAML XML Pkl ••• > pkl
eval ••• > pkl-gen-* App 1. 静的設定ファイルの生成 2. 設定ファイルとして利用 source code
1. 静的設定ファイルの生成 6 host = "localhost" port = 80 api
{ url = "https://example.com/api/hoge" token = "secret" } host: localhost port: 80 api: url: https://example.com/api/hoge token: secret <?xml version="1.0" encoding="UTF-8"?> <root> <host>localhost</host> <port>80</port> <api> <url>https://example.com/api/hoge</url> <token>secret</token> </api> </root> example.pkl pkl eval -f yaml example.pkl pkl eval -f xml example.pkl 再利用,テンプレート,抽象化による 複雑さの低減と保守性の向上
2. 設定ファイルとして利用 7 host: String port: UInt8 api: Api class
Api { url: String token: String } // Code generated from Pkl module `example01-template`. DO NOT EDIT. import PklSwift public enum example01Template {} extension example01Template { public struct Module: PklRegisteredType, Decodable, Hashable { public static var registeredIdentifier: String = "example01-template" public var host: String pkl-gen-swift template.pkl App ビルド ランタイムに読込 template.pkl 言語とのバインディングによる 簡単さと安全さの実現 host = "localhost" port = 80 api { url = "https://example.com/api/hoge" token = "secret" } example.pkl 準拠(amends)
データタイプ(一部) 8 // string name = "Rei" // number age
= 5 // boolean cute = true // object food { name = "ROYAL CANIN" price = 5470 } // class (typed object) class Cat { name: String age: UInt8 } rei = (Cat) { name = "Rei" age = 5 } // Duration duration = 28.d // DataSize size = 42.kb // union env: "dev" | "prod" = "prod" // list toys = new Listing { "けりぐるみ" "ねこじゃらし" "爪とぎ" } // map hospitals = new Mapping { ["primary"] { doctor = "Doctor X" tel = "03-xxxx-xxxx" } ["secondary"] { doctor = "Doctor Y" tel = "03-yyyy-yyyy" } }
式(一部) 9 // if age = 20 type = if
(age >= 20) "adult" else "child" // for ids = List(0, 1, 2) machines { for (_id in ids) { new { name = "machine_\(_id)" } } } // type test floor = 5 isNum = floor is Number // Regex postCode = "100-0001" pcRe = Regex(#"[0-9]{3}-?[0-9]{4}"#) isPCValid = postCode.matches(pcRe) // spread syntax entry1 { type = "book" } entry2 { ...entry1 }
テンプレート 10 module Configuration name: String(length > 0) email: String(isEmailValid)
local isEmailValid = (s) -> s.matches(Regex(#".+@.+"#)) emailSub: String(isEmailValid, this != email) amends "./Configuration.pkl" name = "Hoge" email = "
[email protected]
" emailSub = "
[email protected]
" Configuration.pkl me.pkl
バリデーション(型制約) 11 module Configuration name: String(length > 0) email: String(isEmailValid)
local isEmailValid = (s) -> s.matches(Regex(#".+@.+"#)) emailSub: String(isEmailValid, this != email) amends "./Configuration.pkl" name = "Hoge" email = "
[email protected]
" emailSub = "
[email protected]
" Configuration.pkl me.pkl
制約に違反している場合 12 $ pkl eval me.pkl –– Pkl Error ––
Type constraint `this != email` violated. Value: "
[email protected]
" 6 | emailSub: String(isEmailValid, this != email) ^^^^^^^^^^^^^ at Configuration#emailSub (file:///Users/hoge/Dropbox/presentation/2024-04-17-llt31/learning/ example04/Configuration.pkl, line 6) 5 | emailSub = "
[email protected]
" ^^^^^^^^^^^^^^^^^^ at me#emailSub (file:///Users/hoge/Dropbox/presentation/2024-04-17-llt31/learning/example04/ me.pkl, line 5) 106 | text = renderer.renderDocument(value) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.3/stdlib/ base.pkl#L106) amends "./Configuration.pkl" name = "Hoge" email = "
[email protected]
" emailSub = "
[email protected]
"
所感 • Con fi guration as Code便利 • デコードやバリデーションの実装の手間がなくなる •
制約をテンプレート自体に表現でき,ドキュメント化できる • 設定ミスを予防できる • 公式のチュートリアルは説明不足でちょっとわかりにくい 13
14 Appendix
提供されているツール • 実行可能ファイル (pkl) • macOS native, linux native, JAVA
• YAML, JSON, XML, Property List, Jsonnet, Pcf, (Java) Properties, Custom Renderers • ライブラリ,ソースファイル生成 (pkl-gen-*) • Java, Kotlin, Swift, and Go 15
プラグイン • VSCode, IntelliJ, vim • LSPも予定されている 16
Duration, DataSize • ビルドインサポートのフォーマットではそのまま出力できない • output.renderer.converters を定義すれば出力できる 17
Refs • 公式ページ https://pkl-lang.org/ 18