Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
protoc pluginのはじめかた
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
sivchari
December 19, 2024
290
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
protoc pluginのはじめかた
sivchari
December 19, 2024
Transcript
19 slides
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の静的解析ツールを組み合わせることでプロダク ションの多くのコードを自動生成できる まとめ
感想待ってます
More Decks by sivchari
See All by sivchari
GoLab 2026
sivchari
0
2
Big/Tiny GOTOOLCHAIN
sivchari
0
1
go test を速くする
sivchari
4
1.6k
govalid ~ Type-safe validation tool ~
sivchari
0
260
Go1.25 リリースパーティ ~ nil pointer bug ~
sivchari
0
270
Google Developer Group - DevFest Tokyo 2025
sivchari
0
260
Who tests the Tests ?
sivchari
0
310
Go 1.26 リリースパーティ
sivchari
0
350
静的解析 x Kubernetes API Conventions = Kube API Linter ~ ベストプラクティスに準拠したカスタムリソースの作り方と運用 ~
sivchari
0
400
Featured
See All Featured
How GitHub (no longer) Works
holman
316
150k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
4
1.3k
Mind Mapping
helmedeiros
1
440
Faster Mobile Websites
deanohume
310
32k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
900
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
121
130k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
70
69k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.5k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
How STYLIGHT went responsive
nonsquared
100
6.4k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
340