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
独自ファイル形式にStructTagで立ち向かう
Search
atsushi-ishibashi
June 10, 2019
Technology
0
41
独自ファイル形式にStructTagで立ち向かう
atsushi-ishibashi
June 10, 2019
Tweet
Share
More Decks by atsushi-ishibashi
See All by atsushi-ishibashi
コンテナ運用におけるアプリケーション設定管理
atsushi51
0
390
モダンなアーキテクチャでゼロから作る証券基盤
atsushi51
1
2.2k
Goによるスキーマ駆動開発
atsushi51
1
1.6k
AWSにおけるクラウドネイティブでセキュアな証券システムの運用
atsushi51
0
99
クラウドインフラの権限
atsushi51
0
37
AWSとGo言語とコンテナによる証券プラットフォーム
atsushi51
0
60
StepFunctionsフルコース
atsushi51
0
69
DevSecOps実践
atsushi51
0
97
Other Decks in Technology
See All in Technology
関数型DDDの理論と実践:「決定を遅らせる」を先につくり、 ビジネスの機動力と価値をあげる
knih
2
470
Getting started with controlling LEGO using Swift
hcrane
0
130
複数の LLM モデルを扱う上で直面した辛みまとめ
kazuyaseki
1
230
KTC_DBRE.pdf
_awache
0
290
.NETの非同期戦略とUnityとの相互運用
neuecc
2
2.4k
エバンジェリスト活動を7年やってきて見えてきた、コミュニティとエバンジェリストの関係
soracom
PRO
1
200
期待しすぎずに取り組む両面 TypeScript
shozawa
2
290
プッシュ型子育てサービスを、先行プロジェクト実施自治体において開始します
govtechtokyo
0
240
データ品質をコード化! LINEヤフーのMLOpsを最適化する "ACP Data Quality" の紹介
lycorptech_jp
PRO
1
140
Vos logs méritent mieux que la config par défaut
lyrixx
2
250
2023 Japan AWS Jr.Championsに選出されての振り返りとこれから
hiropy877
1
130
なんで私に登壇依頼が?! ~頼られるエンジニアになるためには~ /
mixi_engineers
PRO
2
200
Featured
See All Featured
Embracing the Ebb and Flow
colly
78
4.1k
Automating Front-end Workflow
addyosmani
1353
200k
Design by the Numbers
sachag
274
18k
JazzCon 2018 Closing Keynote - Leadership for the Reluctant Leader
reverentgeek
178
11k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
5
1.4k
Statistics for Hackers
jakevdp
789
220k
Thoughts on Productivity
jonyablonski
57
3.8k
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
Being A Developer After 40
akosma
56
580k
No one is an island. Learnings from fostering a developers community.
thoeni
14
2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
226
16k
Testing 201, or: Great Expectations
jmmastey
27
6.3k
Transcript
ಠࣗϑΝΠϧܗࣜʹ StructTagͰཱ͔ͪ͏ Finatext ੴڮ ३ࢤ
Finatext
None
None
ࣗݾհ - ੴڮ३ࢤ • ࡏֶதʹFinatextʹؔΘΔ • εϚʔτϓϥεͷূ݊γεςϜͷઃܭɾߏஙΛ ୲ • ࠒ͍ͬͯΔݴޠɿGoݴޠ
• terraform-provider-awsʹͦͦ͜͜ߩݙ • झຯɿαφ • @bashi0501
StructTagͬͯ·͔͢ʁ
• json, xml • form, query • db • validate
(gopkg.in/go- playground/validator.v9) Α͘͏λά
• ͋ΔϑΥʔϚοτͷΤϯίʔυ/ σίʔυΛָ͍ͨ͠ • ΞϊςʔγϣϯͰॲཧ༰Λઃఆ ͍ͨ͠ ༻్
TagΛࣗ࡞ͨ͜͠ͱ͋Δਓ!
ͲΜͳϑΝΠϧʁ • SHIFT_JIS • ݻఆ
encoding/jsonύοέʔδʹ฿ͬͯ 2ͭͷؔΛ࣮ package custom func Unmarshal(p []byte, v interface{}) error
func Marshal(v interface{}) ([]byte, error)
func Unmarshal(p []byte, v interface{}) error { //reflect.Value tt :=
reflect.ValueOf(v) // reflect.Kind // ΈࠐΈܕ,Ptr,Interface,StructͳͲͷܕΛද͢enum if tt.Kind() != reflect.Ptr { return error } // PtrͷnilνΣοΫ if tt.IsNil() { return error } // Elem returns the value that interface contains or that pointer points to elem := tt.Elem() fmt.Println(elem.Kind()) // struct … Unmarshal
func Unmarshal(p []byte, v interface{}) error { … elem :=
tt.Elem() //reflect.Type // t := elem.Type() //֤fieldʹରͯ͠ॲཧΛ͍ͯ͘͠ for i := 0; i < t.NumField(); i++ { //reflect.StructField //Field໊ܕɺTagͳͲͷใΛࢀরͰ͖Δ field := t.Field(i) fmt.Println(field.Name) //field໊ fmt.Println(field.Type.Kind()) //reflect.Kindͱͯ͠ͷܕ fmt.Println(field.Type.Name()) //typeએݴΛࣝผ͍ͨ͠ͱ͖ tag, ok := field.Tag.Lookup(“tagName”) //͋ͱtagΛύʔεͯ͠p []byte͔Β͏·͍͜ͱσʔλऔ͖ͬͯͯ elem.Field(i).SetXXX(foo(p)) } }
func Marshal(v interface{}) ([]byte, error) { tt := reflect.ValueOf(v) switch
rv.Kind() { case reflect.Ptr, reflect.Interface: if rv.IsNil() { return nil, error } tt = tt.Elem() case reflect.Struct: default: return nil, error } t := tt.Type() for i := 0; i < t.NumField(); i++ { field := rt.Field(i) tag, ok := field.Tag.Lookup(“tagName”) // do something ttf := tt.Field(i) switch ttf.Kind() { case reflect.String: stringValue := ttf.String() // ग़ྗ͢Δ[]byteʹରͯ͠ૢ࡞ Marshal
type Hoge struct { ID string `custom:”1,5”` // 1byte͔Β5byte Count
int64 `custom:”6,2”` IsFlag bool `custom:"8,1"` Price float64 `custom:"9,5"` Abs uint64 `custom:"14,3"` } p := []byte(`abcde101002.3543`) var hoge Hoge Unmarshal(p, &hoge) hoge.ID // abcde hoge.Count // 10 hoge.IsFlag // true hoge.Price // 2.3 hoge.Abs // 543 Unmarshalͷྫ
type Hoge struct { ID string `custom:”1,5"` Count int64 `custom:”6,2”`
IsFlag bool `custom:"8,1"` Price float64 `custom:"9,5"` Abs uint64 `custom:"14,3"` } hoge := Hoge{ ID: “abcde”, Count: “10”, IsFlag: true, Price: 2.3, Abs: 543, } p, err := Marshal(hoge) fmt.Println(string(p)) // abcde101002.3543 Marshalͷྫ
ಠ༷ࣗͷϑΝΠϧ Ͱͷ࿈ܞ ಠࣗϑΥʔϚοτ Ͱͷ௨৴ ؆୯ʹѻ͑Δ
͍ɺ ͦͦಠࣗϑΥʔϚοτ ࠷ޙͷखஈ$
• ৭Μͳ͜ͱग़དྷͦ͏ͳؾʹͳΔ ͕ɺͦΕͨͿΜTagͰΔ͜ ͱ͡Όͳ͍ • ࡞Βͳͯ͘ྑ͍ʹӽͨ͜͠ͱ ͳ͍ ·ͱΊ