Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Pkl Satoshi SAKAO えるLT Vol.31 2024-04-17 1
Slide 2
Slide 2 text
話すひと 2 🏢 インフォコム株式会社 サービスマネジメント室 👨🔧 ソフトウェアエンジニア 🛠 Node.js / AWS / IoT / iOS (Swift) / Linux 💖 猫,B'z,テクテクライフ(ランク: 29) Satoshi SAKAO @ottijp.com 𝕏 @ottijp
Slide 3
Slide 3 text
Now playing... 3 https://bsky.app/pro fi le/ottijp.com/post/3kms7pxk2a326
Slide 4
Slide 4 text
Pkl • 「ぴっくる」 • OSS (since 2024-02-01) by Apple • Con fi guration as Code • 静的設定ファイル < Pkl < プログラミング言語 4
Slide 5
Slide 5 text
ユースケース 5 Pkl JSON YAML XML Pkl ●●● > pkl eval ●●● > pkl-gen-* App 1. 静的設定ファイルの生成 2. 設定ファイルとして利用 source code
Slide 6
Slide 6 text
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 localhost 80 https://example.com/api/hoge secret example.pkl pkl eval -f yaml example.pkl pkl eval -f xml example.pkl 再利用,テンプレート,抽象化による 複雑さの低減と保守性の向上
Slide 7
Slide 7 text
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)
Slide 8
Slide 8 text
データタイプ(一部) 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" } }
Slide 9
Slide 9 text
式(一部) 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 }
Slide 10
Slide 10 text
テンプレート 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
Slide 11
Slide 11 text
バリデーション(型制約) 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
Slide 12
Slide 12 text
制約に違反している場合 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]
"
Slide 13
Slide 13 text
所感 • Con fi guration as Code便利 • デコードやバリデーションの実装の手間がなくなる • 制約をテンプレート自体に表現でき,ドキュメント化できる • 設定ミスを予防できる • 公式のチュートリアルは説明不足でちょっとわかりにくい 13
Slide 14
Slide 14 text
14 Appendix
Slide 15
Slide 15 text
提供されているツール • 実行可能ファイル (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
Slide 16
Slide 16 text
プラグイン • VSCode, IntelliJ, vim • LSPも予定されている 16
Slide 17
Slide 17 text
Duration, DataSize • ビルドインサポートのフォーマットではそのまま出力できない • output.renderer.converters を定義すれば出力できる 17
Slide 18
Slide 18 text
Refs • 公式ページ https://pkl-lang.org/ 18