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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
sivchari
December 19, 2024
0
93
protoc pluginのはじめかた
sivchari
December 19, 2024
Tweet
Share
More Decks by sivchari
See All by sivchari
govalid ~ Type-safe validation tool ~
sivchari
0
59
Go1.25 リリースパーティ ~ nil pointer bug ~
sivchari
0
59
Google Developer Group - DevFest Tokyo 2025
sivchari
0
61
Who tests the Tests ?
sivchari
0
61
Go 1.26 リリースパーティ
sivchari
0
60
静的解析 x Kubernetes API Conventions = Kube API Linter ~ ベストプラクティスに準拠したカスタムリソースの作り方と運用 ~
sivchari
0
130
What's GOCACHEPROG ?
sivchari
1
480
gh_extensionsによる快適なOSS生活.pdf
sivchari
0
90
Visualization Go scheduler by gosched-simulator
sivchari
1
550
Featured
See All Featured
It's Worth the Effort
3n
188
29k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Building Adaptive Systems
keathley
44
2.9k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.1k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
260
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
240
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Mobile First: as difficult as doing things right
swwweet
225
10k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
150
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.1k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
110
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の静的解析ツールを組み合わせることでプロダク ションの多くのコードを自動生成できる まとめ
感想待ってます