-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Goやってる人にレビューもらったので共有 #12
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このままだと動かないから共有だけ 🙏
kenjo, err := ioutil.ReadFile("./utils/" + "kenjo.json") | ||
if err != nil { | ||
panic(err) | ||
} | ||
util := utils.Utils{ | ||
Kenjo: kenjo, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utils/
以下のファイルを読み込むはランタイムでやってひらけなかったらパニックしたほうがいいって言われた
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
リクエストが送られた時にpanicよりはいいとのことです
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど!
実はここ、jsonファイル読み込みが入るのでビルドしたときに正しく読み込みできるか怪しいかも
今のところDockerfileも実行コンテナにはビルド済みバイナリしか渡してなかったりします
type Kenjyo struct{ | ||
Utils: utils, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ランタイムで読み込んだutilsをtypeで渡すのがいいらしい
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeってこんな風に使えるのか、知見
conversionRules, err := utils.JsonDecoder("kenjyo.json") | ||
if err != nil { | ||
return "", err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error を握り潰すなとのことです
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラー処理サボってたのが発覚してしまう回!😆
申し訳無さ🙏
@@ -1,33 +1,35 @@ | |||
package utils | |||
package converter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utils
はなんでも入れられるから使わないほうがいいらしい
今回はconvertの処理だけだから converter
パッケージにしたほうがいいって
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかに、パッと特定の役割が思い浮かばない命名は避けたほうがいいな
ディレクトリとファイル名も合わせて修正したさ
"github.com/aizu-go-kapro/keiGo/backend/models" | ||
"github.com/gin-gonic/gin" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
パッケージ? のパスの指定の仕方が違うっていわれた
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go Modulesのお作法的にGitHub上に上げるパッケージの場合は修正してもらったようなgithub.com/xxx~
の形が正しそう
"github.com/aizu-go-kapro/keiGo/backend/models" | ||
"github.com/gin-gonic/gin" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go Modulesのお作法的にGitHub上に上げるパッケージの場合は修正してもらったようなgithub.com/xxx~
の形が正しそう
const ( | ||
Teinei string = "teinei" | ||
Sonkei string = "sonkei" | ||
Kenjyo string = "kenjyo" | ||
teineiKind = "teinei" | ||
sonkeiKind = "sonkei" | ||
kenjyoKind = "kenjyo" | ||
) | ||
// Keigo is controller of keigo | ||
type Keigo struct { | ||
teinei models.Teinei | ||
sonkei models.Sonkei | ||
kenjyo models.Kenjyo | ||
} | ||
|
||
// NewKeigo create keigo controller | ||
func NewKeigo() *Keigo { | ||
return &Keigo{} | ||
} | ||
|
||
// ConvertKeigo is controller | ||
func (kc *Keigo) ConvertKeigo(c *gin.Context) { | ||
var ( | ||
request models.KeigoRequest | ||
response models.KeigoResponse | ||
) | ||
|
||
func (kc *KeigoController) ConvertKeigo(c *gin.Context) { | ||
kind := c.Query("kind") | ||
print(kind) | ||
request := models.KeigoRequest{} | ||
response := models.KeigoResponse{} | ||
if kind == "" { | ||
return c.JSON( | ||
http.StatusBadRequest, | ||
map[string]string{"status": "error", "message": "kind is empty"} | ||
// '{"status": "error", "message": "kind is empty"}' | ||
) | ||
} | ||
|
||
if err := c.BindJSON(&request); err != nil { | ||
c.Status(http.StatusBadRequest) | ||
} else { | ||
switch kind { | ||
case Teinei: | ||
teinei := models.Teinei{} | ||
response.ConvertedBody = teinei.Convert(request.Body) | ||
case Sonkei: | ||
sonkei := models.Sonkei{} | ||
response.ConvertedBody = sonkei.Convert(request.Body) | ||
case Kenjyo: | ||
kenjyo := models.Kenjyo{} | ||
response.ConvertedBody = kenjyo.Convert(request.Body) | ||
} | ||
c.JSON(http.StatusOK, response) | ||
return c.Status(http.StatusBadRequest) | ||
} | ||
|
||
switch kind { | ||
case teineiKind: | ||
response.ConvertedBody = kc.teinei.Convert(request.Body) | ||
case sonkeiKind: | ||
response.ConvertedBody = kc.sonkei.Convert(request.Body) | ||
case kenjyoKind: | ||
response.ConvertedBody = kc.kenjyo.Convert(request.Body) | ||
} | ||
|
||
return c.JSON(http.StatusOK, response) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この辺のうまい書き方わからなかったからとても参考になる!
kenjo, err := ioutil.ReadFile("./utils/" + "kenjo.json") | ||
if err != nil { | ||
panic(err) | ||
} | ||
util := utils.Utils{ | ||
Kenjo: kenjo, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど!
実はここ、jsonファイル読み込みが入るのでビルドしたときに正しく読み込みできるか怪しいかも
今のところDockerfileも実行コンテナにはビルド済みバイナリしか渡してなかったりします
kC := controllers.NewKeigo() | ||
api := router.Group("/api/v1") | ||
{ | ||
kc := new(controllers.KeigoController) | ||
api.GET("/keigo", kc.ConvertKeigo) | ||
api.GET("/keigo", kC.ConvertKeigo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここの書き方も参考になります!
type Kenjyo struct{ | ||
Utils: utils, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeってこんな風に使えるのか、知見
conversionRules, err := utils.JsonDecoder("kenjyo.json") | ||
if err != nil { | ||
return "", err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラー処理サボってたのが発覚してしまう回!😆
申し訳無さ🙏
@@ -1,33 +1,35 @@ | |||
package utils | |||
package converter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかに、パッと特定の役割が思い浮かばない命名は避けたほうがいいな
ディレクトリとファイル名も合わせて修正したさ
} | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここって、。。。
みたいに複数記号も想定なので素でbreakすると。
一文字までしか対応できない挙動になる気がする
Goやってる人に質問したらそのままいろいろ編集されちゃったんですが消すのがもったいないので一応共有します!