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
50
独自ファイル形式にStructTagで立ち向かう
atsushi-ishibashi
June 10, 2019
Tweet
Share
More Decks by atsushi-ishibashi
See All by atsushi-ishibashi
コンテナ運用におけるアプリケーション設定管理
atsushi51
0
420
モダンなアーキテクチャでゼロから作る証券基盤
atsushi51
1
2.4k
Goによるスキーマ駆動開発
atsushi51
1
1.7k
AWSにおけるクラウドネイティブでセキュアな証券システムの運用
atsushi51
0
120
クラウドインフラの権限
atsushi51
0
48
AWSとGo言語とコンテナによる証券プラットフォーム
atsushi51
0
63
StepFunctionsフルコース
atsushi51
0
78
DevSecOps実践
atsushi51
0
110
Other Decks in Technology
See All in Technology
データ分析基盤のためにS3を深堀りする~アーキテクチャ設計の考え方のヒントに~
nrinetcom
PRO
1
760
外部カンファレンスで登壇しよう! 〜「強い」エンジニアへの一歩を踏み出す〜
logica0419
4
130
GPSデバイスを使った簡易位置案内システムの構築をしてみた話。/jawsfesta2024
kwada
0
220
まだ間に合う! 生成AIトレンド一挙おさらい & AWSのBedrockに入門しよう
minorun365
PRO
4
190
LINE-ChatGPT 倫理問題を整理する全力肯定彼氏くん [LuC4]に訪れたサービス開始以来の最大の危機
o_ob
2
300
SageMaker学習のツボ / The Key Points of Learning SageMaker
cmhiranofumio
0
280
Delta Commit…の最近...
akuwano
2
110
なぜ Rack を理解すべきかプレトーク / Why should you understand Rack - Pre-talk
hogelog
0
220
いまいまMySQL2024 @ OSC Nagaoka
sakaik
3
290
Rist_Meetup_Kaggleは業務の役にたつ - ビジネスコンテンツ情報を活用する BtoB 事業編 - / rist-meetup-20241012
taro_masuda
0
300
さくっと実践!Postmanを活用した高品質で持続可能なAPI管理
yokawasa
5
360
今日から始める技術的負債の解消
leveragestech
3
440
Featured
See All Featured
Navigating Team Friction
lara
183
14k
Optimising Largest Contentful Paint
csswizardry
31
2.9k
Producing Creativity
orderedlist
PRO
341
39k
Documentation Writing (for coders)
carmenintech
65
4.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Adopting Sorbet at Scale
ufuk
73
9k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
How to Ace a Technical Interview
jacobian
275
23k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
250
Teambox: Starting and Learning
jrom
132
8.7k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
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ͰΔ͜ ͱ͡Όͳ͍ • ࡞Βͳͯ͘ྑ͍ʹӽͨ͜͠ͱ ͳ͍ ·ͱΊ