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
protoc pluginのはじめかた
Search
sivchari
December 19, 2024
0
22
protoc pluginのはじめかた
sivchari
December 19, 2024
Tweet
Share
More Decks by sivchari
See All by sivchari
What's GOCACHEPROG ?
sivchari
1
120
gh_extensionsによる快適なOSS生活.pdf
sivchari
0
6
Visualization Go scheduler by gosched-simulator
sivchari
1
350
What's context package
sivchari
0
18
Dive into arena package ~ Go 1.20 release party ~
sivchari
0
41
GopherCon 2023 recap
sivchari
0
21
Go 1.22 range over func/range over int
sivchari
0
31
Deep dive into runtime features provided by Go1.22
sivchari
0
20
Go 1.22で追加予定 だった zeroの紹介 Go Conference mini
sivchari
0
300
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.1k
Facilitating Awesome Meetings
lara
52
6.2k
Fireside Chat
paigeccino
34
3.2k
Building Your Own Lightsaber
phodgson
104
6.2k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Side Projects
sachag
452
42k
Six Lessons from altMBA
skipperchong
27
3.6k
Documentation Writing (for coders)
carmenintech
67
4.6k
4 Signs Your Business is Dying
shpigford
182
22k
Producing Creativity
orderedlist
PRO
344
39k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
420
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Transcript
protoc pluginのはじめかた 10/24/2024 sivchari CyberAgent The Go gopher was designed
by Renée French.
自己紹介 protoc pluginとは protoc pluginを使用するメリット extensions protoc pluginの書き方 まとめ
自己紹介
• Takuma Shibuya ◦ X/GitHub sivchari • CIU ◦ AKE
(Astro Kubernetes Engine) • CyberAgent Go Next Experts • Go Conference主催
protoc pluginとは
• protoc pluginはProtocol Buffersで提供されている拡張点 ◦ –go_outや--go_grpc_outなどがそう • protocはオプションとして--XXX_outを渡すとprotoc-gen-XXXを呼 び出すように設計されている ◦
gRPC x Go tutorial ref. Protocol Buffers Documentation protoc plugin
Architecture Proto files Protoc CodeGeneratorRequest CodeGeneratorResponse Write to CodeGeneratorResponse Generated
Files Protoc Plugin
protoc pluginを使うメリット
• 拡張点でのコードの自動生成 ◦ 認証認可 ◦ 自動計装 ◦ 各種SaaSの設定を差し込む ◦ validation
◦ IaC • protoにあらゆる情報を集約することができる ◦ ツールの設定情報が分散しない ◦ proto : XXXのようなものがある際の設定情報の乖離防止 protoc pluginを使うメリット
extensions
• protoc pluginが拡張点を実現するために必須の機能 • optionは様々な文法に対して書くことができる ◦ File ◦ Message ◦
Field ◦ Service ◦ etc.. extensions
rpc EchoBoyd(SimpleMessage) returns (SimpleMessage) { option (google.api.http) = { post:
“/v1/example/echo_body” body: “*” // snip } } ref. gRPC-Gateway extensions
• protoのenumに何かオプションを足したい場合に都度それぞれが Change Requestを出すのは非現実的 • extendを用いて拡張する ◦ protoのフィールドはタグ番号で識別される ◦ メッセージの拡張用のタグ番号は`extensions
N to N` ◦ 各種フィールドが用意してくれている拡張用のタグ番号に対して extendすることで拡張することができるようになる ◦ タグ番号の範囲は1~536,870,911 (enumは1000 to max) ref. Google APIs extensions
protoc pluginの書き方
• protoc pluginはCodeGeneratorRequestを受け取り、 CodeGeneratorResponseを返せばOK • 選択肢はいくつかある ◦ os.Stdinから自分でかく ◦ protobuf-go/compiler/protogenを使用する
◦ lyft/protoc-gen-starをつかう protoc pluginの書き方
• 今回はmessage fieldのoptionにdescriptionというoptionを用意 する ◦ descriptionが記載されているフィールドは ${MessageName}${FieldName}Descriptionという関数を自 動生成するようにする ◦ サンプルコードをベースにして説明していきます
ref. protoc-gen-description protoc pluginの書き方
まとめ
• protocにはpluginという自動生成用の拡張点がある • protoを静的解析して取得したASTを用いてpluginを実装することで Goのコードを生成することができる • pluginとその他Goの静的解析ツールを組み合わせることでプロダク ションの多くのコードを自動生成できる まとめ
感想待ってます