-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.vscode/ | ||
.idea/ | ||
.idea/ | ||
|
||
vendor/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,57 @@ | ||
package v1 | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"keigo/models" | ||
"net/http" | ||
) | ||
|
||
type KeigoController struct{} | ||
"github.com/aizu-go-kapro/keiGo/backend/models" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
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) | ||
} | ||
Comment on lines
10
to
57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この辺のうまい書き方わからなかったからとても参考になる! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
controllers "github.com/aizu-go-kapro/keiGo/backend/controllers/v1" | ||
"github.com/aizu-go-kapro/keiGo/backend/utils" | ||
"github.com/gin-gonic/gin" | ||
controllers "keigo/controllers/v1" | ||
) | ||
|
||
// go fmt ... | ||
|
||
func main() { | ||
kenjo, err := ioutil.ReadFile("./utils/" + "kenjo.json") | ||
if err != nil { | ||
panic(err) | ||
} | ||
util := utils.Utils{ | ||
Kenjo: kenjo, | ||
} | ||
Comment on lines
+14
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. なるほど! 実はここ、jsonファイル読み込みが入るのでビルドしたときに正しく読み込みできるか怪しいかも |
||
router := gin.Default() | ||
kC := controllers.NewKeigo() | ||
api := router.Group("/api/v1") | ||
{ | ||
kc := new(controllers.KeigoController) | ||
api.GET("/keigo", kc.ConvertKeigo) | ||
api.GET("/keigo", kC.ConvertKeigo) | ||
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここの書き方も参考になります! |
||
} | ||
router.Run(":3000") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,30 +2,36 @@ package models | |
|
||
import ( | ||
"fmt" | ||
"keigo/utils" | ||
"strings" | ||
|
||
"github.com/aizu-go-kapro/keiGo/backend/utils" | ||
|
||
"github.com/ikawaha/kagome/tokenizer" | ||
) | ||
|
||
type Kenjyo struct{} | ||
type Kenjyo struct{ | ||
Utils: utils, | ||
} | ||
|
||
Comment on lines
+12
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. typeってこんな風に使えるのか、知見 |
||
|
||
func (k *Kenjyo) Convert(body string) string { | ||
func (k *Kenjyo) Convert(body string) (string, error) { | ||
kagome := Kagome{} | ||
tokens := kagome.MorphologicalAnalysis(body) | ||
|
||
utils := utils.Utils{} | ||
conversionRules := utils.JsonDecoder("kenjyo.json") | ||
conversionRules, err := utils.JsonDecoder("kenjyo.json") | ||
if err != nil { | ||
return "", err | ||
} | ||
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. エラー処理サボってたのが発覚してしまう回!😆 |
||
|
||
var convertedBody = "" | ||
|
||
endOfSentenceTokenIndex := len(tokens) - 1 | ||
for { | ||
if tokens[endOfSentenceTokenIndex].Features()[0] == "記号" { | ||
endOfSentenceTokenIndex-- | ||
} else { | ||
break | ||
} | ||
} | ||
break | ||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここって、 |
||
} | ||
|
||
for _, token := range tokens { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
package utils | ||
package converter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. たしかに、パッと特定の役割が思い浮かばない命名は避けたほうがいいな |
||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
) | ||
|
||
type ( | ||
Utils struct{} | ||
Utils struct { | ||
Path string // json ファイルのパス | ||
} | ||
|
||
ConversionRule struct { | ||
Original string `json:"original"` | ||
Converted string `json:"converted"` | ||
} | ||
) | ||
|
||
func (u *Utils) JsonDecoder(filename string) []ConversionRule { | ||
func (u *Utils) JsonDecoder(filename string) ([]ConversionRule, error) { | ||
// File openは実行時パスから見た相対パスになる | ||
bytes, err := ioutil.ReadFile("./utils/" + filename) | ||
if err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
|
||
// JSONデコード | ||
var conversionRules []ConversionRule | ||
if err := json.Unmarshal(bytes, &conversionRules); err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
|
||
return conversionRules | ||
return conversionRules, nil | ||
} | ||
|
||
func (u *Utils) FindConvertedFromConversionRule(conversionRules []ConversionRule, original string) string { | ||
|
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~
の形が正しそう