Slide 1

Slide 1 text

Copyright©HRBrain, Inc. All Rights Reserved golang.tokyo#20 LT⼤会 Goでのモデル 取扱説明書

Slide 2

Slide 2 text

Copyright©HRBrain, Inc. All Rights Reserved ⾃⼰紹介 Tamura Yukichi CyberAgent, Inc 2014 〜 HRBrain, Inc 2018/8 〜 サーバーサイド。 たまにReact。 趣味でVue (Nuxt) 。 Twitter, Github: uqichi

Slide 3

Slide 3 text

Copyright©HRBrain, Inc. All Rights Reserved HRBrainとは 煩雑で不透明になりがちだった⽬標・評価管理のプロセスを ⼀気通貫で効率化・⾒える化するクラウド型ソフトウェア 「⼈事評価を最もカンタン・シンプルに」

Slide 4

Slide 4 text

アプリケーションのレイヤー間
 で登場する構造体たち

Slide 5

Slide 5 text

Copyright©HRBrain, Inc. All Rights Reserved データ(構造体)定義の例 UseCase Repository Handler .PEFM &OUJUZ 3FRVFTU 3FTQPOTF DB Table JSON JSON JSON Web APIを例 *OQVU 0VUQVU

Slide 6

Slide 6 text

Copyright©HRBrain, Inc. All Rights Reserved Data Transfer Object Entity

Slide 7

Slide 7 text

Copyright©HRBrain, Inc. All Rights Reserved Data Transfer Object Domain Model

Slide 8

Slide 8 text

Copyright©HRBrain, Inc. All Rights Reserved Data Transfer Object Response (Output)

Slide 9

Slide 9 text

Copyright©HRBrain, Inc. All Rights Reserved Data Transfer Object Request (Input)

Slide 10

Slide 10 text

Copyright©HRBrain, Inc. All Rights Reserved データ(構造体)定義の例 UseCase Repository Handler .PEFM &OUJUZ DB Table JSON JSON JSON Web APIを例 3FRVFTU 3FTQPOTF *OQVU 0VUQVU

Slide 11

Slide 11 text

どうやって変換する?

Slide 12

Slide 12 text

Copyright©HRBrain, Inc. All Rights Reserved 構造体の変換 golang.org/pkg/encoding

Slide 13

Slide 13 text

Copyright©HRBrain, Inc. All Rights Reserved 構造体の変換 golang.org/pkg/encoding/gob Package gob manages streams of gobs - binary values exchanged between an Encoder (transmitter) and a Decoder (receiver). A typical use is transporting arguments and results of remote procedure calls (RPCs) such as those provided by package "net/ rpc". The implementation compiles a custom codec for each data type in the stream and is most efficient when a single Encoder is used to transmit a stream of values, amortizing the cost of compilation.

Slide 14

Slide 14 text

Copyright©HRBrain, Inc. All Rights Reserved golang.org/pkg/encoding/gob 構造体の変換

Slide 15

Slide 15 text

Copyright©HRBrain, Inc. All Rights Reserved godoc.org/github.com/golang/protobuf/proto 構造体の変換

Slide 16

Slide 16 text

Copyright©HRBrain, Inc. All Rights Reserved Marshaler, Unmarshalerを実装して変換結果をカスタマイズする

Slide 17

Slide 17 text

Copyright©HRBrain, Inc. All Rights Reserved おまけ:Marshaler, Unmarshalerの活⽤ 空の構造体はnullを返すようにする

Slide 18

Slide 18 text

Copyright©HRBrain, Inc. All Rights Reserved • type aliasで定義する • 定数をキーに⽂字列値のマップを定義する • jsonのencoder/decoderを通さなくても変 換できるようにメソッドは分けておく おまけ:Marshaler, Unmarshalerの活⽤ アプリケーション定数
 バックエンドは数値、クライアントは⽂字 列で定数を扱えるようにする

Slide 19

Slide 19 text

全レイヤー、モデルで解決したい

Slide 20

Slide 20 text

Copyright©HRBrain, Inc. All Rights Reserved データ(構造体)定義の例 UseCase Repository Handler .PEFM &OUJUZ DB Table JSON JSON JSON Web APIを例 3FRVFTU 3FTQPOTF *OQVU 0VUQVU

Slide 21

Slide 21 text

Copyright©HRBrain, Inc. All Rights Reserved データ(構造体)定義の例 UseCase Repository Handler .PEFM DB Table JSON JSON JSON Web APIを例 3FRVFTU *OQVU

Slide 22

Slide 22 text

Copyright©HRBrain, Inc. All Rights Reserved All in Model Rails Active Record Active Recordとは、MVCで⾔うところのM、つまりモデルに相当するものであ り、ビジネスデータとビジネスロジックを表すシステムの階層です。Active Recordは、データベースに恒久的に保存される必要のあるビジネスオブジェク トの作成と利⽤を円滑に⾏なえるようにします。Active Recordは、ORM (オブ ジェクトリレーショナルマッピング) システムに記述されている「Active Record パターン」を実装したものであり、同じ名前が付けられています。

Slide 23

Slide 23 text

ぴったりの ORM があります

Slide 24

Slide 24 text

Copyright©HRBrain, Inc. All Rights Reserved Active Record に影響を受けて作られたGo製ORM github.com/gobuffalo/pop A Tasty Treat For All Your Database Needs

Slide 25

Slide 25 text

Copyright©HRBrain, Inc. All Rights Reserved Active Record に影響を受けて作られたGo製ORM • PostgreSQL (>= 9.3) • CockroachDB (>= 1.1.1) • MySQL (>= 5.7) • SQLite3 (>= 3.x) github.com/gobuffalo/pop

Slide 26

Slide 26 text

Copyright©HRBrain, Inc. All Rights Reserved DEMO https://github.com/uqichi/goffee-server

Slide 27

Slide 27 text

Copyright©HRBrain, Inc. All Rights Reserved • Active Recordスキーマにおける名前付けルール • データベースのマイグレーション • 検証(validation) • コールバック • Associationが強⼒ • クライアントツール付属(SODA) gobuffalo/pop

Slide 28

Slide 28 text

Copyright©HRBrain, Inc. All Rights Reserved gobuffalo/pop Association

Slide 29

Slide 29 text

Copyright©HRBrain, Inc. All Rights Reserved gobuffalo/pop Association • Eager(fields string) • Load(fields string)

Slide 30

Slide 30 text

Copyright©HRBrain, Inc. All Rights Reserved gobuffalo/pop Association • Eager(fields string) • Load(fields string)

Slide 31

Slide 31 text

Copyright©HRBrain, Inc. All Rights Reserved soda CLI create Creates databases for you drop Drops databases for you fix Brings pop, soda, and fizz files in line with the latest APIs generate Generates config, model, and migrations files. migrate Runs migrations against your database. reset Drop, then recreate databases schema Tools for working with your database schema gobuffalo/pop/soda

Slide 32

Slide 32 text

Copyright©HRBrain, Inc. All Rights Reserved gobuffalo/pop/soda uuid validate callback soda generate model coffee

Slide 33

Slide 33 text

終了