Skip to content
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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.vscode/
.idea/
.idea/

vendor/
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ RUN go build -o /keigo_server
FROM alpine:latest as production-stage
COPY --from=build-stage /keigo_server .
EXPOSE 3000
ENTRYPOINT ["./keigo_server"]
ENTRYPOINT ["./keigo_server"]
69 changes: 44 additions & 25 deletions backend/controllers/v1/keigo.go
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"
Comment on lines +6 to +7
Copy link
Author

Choose a reason for hiding this comment

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

パッケージ? のパスの指定の仕方が違うっていわれた

Copy link
Contributor

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)
}
Comment on lines 10 to 57
Copy link
Contributor

Choose a reason for hiding this comment

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

この辺のうまい書き方わからなかったからとても参考になる!

4 changes: 2 additions & 2 deletions backend/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module keigo
module github.com/aizu-go-kapro/keiGo/backend

go 1.13
go 1.14

require (
github.com/gin-gonic/gin v1.6.3
Expand Down
18 changes: 15 additions & 3 deletions backend/main.go
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
Copy link
Author

Choose a reason for hiding this comment

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

utils/ 以下のファイルを読み込むはランタイムでやってひらけなかったらパニックしたほうがいいって言われた

Copy link
Author

Choose a reason for hiding this comment

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

リクエストが送られた時にpanicよりはいいとのことです

Copy link
Contributor

Choose a reason for hiding this comment

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

なるほど!

実はここ、jsonファイル読み込みが入るのでビルドしたときに正しく読み込みできるか怪しいかも
今のところDockerfileも実行コンテナにはビルド済みバイナリしか渡してなかったりします

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
Copy link
Contributor

Choose a reason for hiding this comment

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

ここの書き方も参考になります!

}
router.Run(":3000")
}
20 changes: 13 additions & 7 deletions backend/models/kenjyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

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

ランタイムで読み込んだutilsをtypeで渡すのがいいらしい

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

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

error を握り潰すなとのことです

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

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

ここって、。。。みたいに複数記号も想定なので素でbreakすると一文字までしか対応できない挙動になる気がする

}

for _, token := range tokens {
Expand Down
5 changes: 2 additions & 3 deletions backend/models/sonkei.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package models

import (
"fmt"
"keigo/utils"
"strings"

"github.com/aizu-go-kapro/keiGo/backend/utils"

"github.com/ikawaha/kagome/tokenizer"
)

Expand All @@ -14,9 +15,7 @@ func (s *Sonkei) Convert(body string) string {
kagome := Kagome{}
tokens := kagome.MorphologicalAnalysis(body)

utils := utils.Utils{}
conversionRules := utils.JsonDecoder("sonkei.json")

var convertedBody = ""

endOfSentenceTokenIndex := len(tokens) - 1
Expand Down
16 changes: 9 additions & 7 deletions backend/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package utils
package converter
Copy link
Author

Choose a reason for hiding this comment

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

utils はなんでも入れられるから使わないほうがいいらしい
今回はconvertの処理だけだから converter パッケージにしたほうがいいって

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down