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
250
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
220
Go1.25 リリースパーティ ~ nil pointer bug ~
sivchari
0
220
Google Developer Group - DevFest Tokyo 2025
sivchari
0
220
Who tests the Tests ?
sivchari
0
260
Go 1.26 リリースパーティ
sivchari
0
300
静的解析 x Kubernetes API Conventions = Kube API Linter ~ ベストプラクティスに準拠したカスタムリソースの作り方と運用 ~
sivchari
0
350
What's GOCACHEPROG ?
sivchari
1
660
gh_extensionsによる快適なOSS生活.pdf
sivchari
0
250
Featured
See All Featured
The Cult of Friendly URLs
andyhume
79
7k
Crafting Experiences
bethany
1
370
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
The Language of Interfaces
destraynor
162
27k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Ruling the World: When Life Gets Gamed
codingconduct
0
390
Believing is Seeing
oripsolob
1
240
Designing for humans not robots
tammielis
254
26k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
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の静的解析ツールを組み合わせることでプロダク ションの多くのコードを自動生成できる まとめ
感想待ってます