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
ちょこっとdive gorm.DB.Updates()
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
aroon
December 15, 2024
0
160
ちょこっとdive gorm.DB.Updates()
第56回kyoto.goのLT枠で発表した際の資料。
ちょこっとgormの中を探検する感じの発表でした。
aroon
December 15, 2024
Tweet
Share
More Decks by aroon
See All by aroon
goにおける コネクションプールの仕組み を軽く掘って見た
aronokuyama
0
180
Featured
See All Featured
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.6k
My Coaching Mixtape
mlcsv
0
55
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
140
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4k
How to make the Groovebox
asonas
2
2k
Embracing the Ebb and Flow
colly
88
5k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
330
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
840
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
390
30 Presentation Tips
portentint
PRO
1
230
Transcript
ちょこっとdive gorm.DB.Updates() aroon
予防線(???) • 実はこんなバグがあって・・・・! • 実はこんな挙動があって・・・・! ではないです。 多分見つけてたらタイトルが もうちょい違います😇😇😇
自己紹介 • aroon • Gopher暦 半年くらい
クイズです!!
下記3つの選択肢のうち、ageがnullに更新される書き方はど れでしょうか?
正解は・・・・ 2と3です!
gormのUpdatesメソッド with null 仕様 nullableカラムに対してnullで更新しようとする場合・・・ • structを渡した場合、基本nullや0は無視される • mapであれば更新対象にしてくれる • structを利用する場合、Selectで指定すれば更新対象
にしてくれる
さらにクイズです!!
gormはどのようにして そんな仕様を実現してるんで しょうか?
Thinking Time
僕なりの回答は・・・・(というか今日の内容は) • reflectパッケージの各種メソッドの叡智を 用いて • ユーザー入力の値を丹念に場合わけし • その結果をベースにクエリを構築している
ちょっとコードを読んで 「どこでそんな処理してんの?」 を見にいきます!
Let’s GO !!
というわけでまずはUpdatesにGo !! 罠である
こんな感じ processor構造体のポインタを返している だけである。
Executeについても・・・・
読み進めていくと・・・ p.fnsからメソッド取り出して適用してる・・・!?
どのタイミングでp.fns構造体に詰めたんだ・・・?
そう。Openメソッドです。 というわけで Openを読み進めていくと、 コールバックを登録するよ!みたいなメソッドにたどり着く。
Updateメソッドがいるぞ・・・!
上から読み進めると・・・ • ConvertToAssignmentsにてついにお目当てのコードに辿り着く・・・! • ここからはリアルコードに行きます・・・! ◦ https://github.com/go-gorm/gorm/blob/v1.25.12/callbacks/update.go#L260
まとめ • gormはOpenメソッドで ◦ 各種構造体を初期化している ◦ Update, Queryなどの関数を登録している • Updateメソッド内部の
◦ ConvertToAssignmentsメソッドにて ▪ mapか構造体かでsetするかどうかを決定している ▪ structの際は、Nilや0の場合にsetしない ◦ と判定している