Skip to content

Kadai4 misonog#57

Open
misonog wants to merge 5 commits into
gopherdojo:masterfrom
misonog:kadai4-misonog
Open

Kadai4 misonog#57
misonog wants to merge 5 commits into
gopherdojo:masterfrom
misonog:kadai4-misonog

Conversation

@misonog
Copy link
Copy Markdown

@misonog misonog commented May 9, 2021

おみくじ API

お手数ですがレビューお願いいたします。

仕様

  • JSON 形式でおみくじの結果を返す
  • 正月(1/1-1/3)だけ大吉にする
  • ハンドラのテストを書いてみる

利用方法

setup

$ make  # テスト & ビルド

サーバーの起動

$ ./omikuji-server

おみくじを引く

$ curl "http://127.0.0.1:8080/"
> {"result":"小吉"}
$ curl "http://127.0.0.1:8080/?date=2021-01-01"
> {"result":"大吉"}

ディレクトリ構造

.
├── Makefile
├── README.md
├── go.mod
├── main.go
├── omikuji-server
├── omikuji.go
└── omikuji_test.go

Copy link
Copy Markdown

@sryoya sryoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いくつかコメントしてみました

Comment thread kadai4/misonog/omikuji.go

func (o *omikuji) Draw() {
if isShogatsu(o.date) {
o.result = daikichi
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここでreturnしてしまえば、elseはいらないですね

Comment thread kadai4/misonog/omikuji.go
if isShogatsu(o.date) {
o.result = daikichi
} else {
rand.Seed(time.Now().UnixNano())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rand.Seedみたいな共通の初期化系の処理は、init functionで最初に実行させてしまうという方法があります

https://golang.org/doc/effective_go#init

Comment thread kadai4/misonog/omikuji.go
if dateValue != "" {
date, err := time.Parse(timeForm, dateValue)
if err != nil {
log.Fatal(err)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この場合、httpのerrorとして返した方が良いと思います。特に、ユーザーが異常な値を入れたら発生する想定内なエラーだと思うので

handler(w, r)
rw := w.Result()
defer rw.Body.Close()
if rw.StatusCode != http.StatusOK {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StatusCodeも期待値に入れると、異常系のテストもできます Bad Requestのときとかも含め

Comment thread kadai4/misonog/omikuji.go
Comment on lines +22 to +25
type omikuji struct {
result string
date time.Time
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この2つのデータをstructとしてまとめて置く必要はありますか?

Comment thread kadai4/misonog/omikuji.go
Comment on lines +13 to +16
daikichi = "大吉"
)

var omikujiResults = []string{"吉", "小吉", "凶"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

daikichi以外の値も定数に定義して、daikichiも含めてこのsliceに入れてしまう方が可変性、可読性(一覧性)ともに高いと思います。

Comment thread kadai4/misonog/omikuji.go
}

_, month, date := t.Date()
if month == time.January {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここがfalseの時点で return false すれば、ifをネストしなくてよくなりますね

Comment thread kadai4/misonog/main.go
)

func main() {
http.HandleFunc("/", handler)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これをmain関数の外に置いておくことでhttpのroutingも含めてテストできるようになります

@misonog
Copy link
Copy Markdown
Author

misonog commented Jun 22, 2021

@sryoya レビューありがとうございます!別途確認いたします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants