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
210
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
protoc pluginのはじめかた
sivchari
December 19, 2024
More Decks by sivchari
See All by sivchari
go test を速くする
sivchari
4
1.5k
govalid ~ Type-safe validation tool ~
sivchari
0
180
Go1.25 リリースパーティ ~ nil pointer bug ~
sivchari
0
170
Google Developer Group - DevFest Tokyo 2025
sivchari
0
180
Who tests the Tests ?
sivchari
0
220
Go 1.26 リリースパーティ
sivchari
0
260
静的解析 x Kubernetes API Conventions = Kube API Linter ~ ベストプラクティスに準拠したカスタムリソースの作り方と運用 ~
sivchari
0
270
What's GOCACHEPROG ?
sivchari
1
620
gh_extensionsによる快適なOSS生活.pdf
sivchari
0
210
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
Making Projects Easy
brettharned
120
6.7k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Skip the Path - Find Your Career Trail
mkilby
1
220
Believing is Seeing
oripsolob
1
210
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
190
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
[SF Ruby Conf 2025] Rails X
palkan
2
1.3k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
590
Paper Plane (Part 1)
katiecoart
PRO
1
11k
How GitHub (no longer) Works
holman
316
150k
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の静的解析ツールを組み合わせることでプロダク ションの多くのコードを自動生成できる まとめ
感想待ってます