Skip to content

tsuchinaga / 課題1 - #3

Open
tsuchinaga wants to merge 13 commits into
masterfrom
kadai1-tsuchinaga
Open

tsuchinaga / 課題1#3
tsuchinaga wants to merge 13 commits into
masterfrom
kadai1-tsuchinaga

Conversation

@tsuchinaga

@tsuchinaga tsuchinaga commented Jul 6, 2020

Copy link
Copy Markdown

課題1のPRです。レビューをお願いします。

@tsuchinaga tsuchinaga changed the title 課題1 - tsuchinaga 課題1 / tsuchinaga Jul 6, 2020
@tsuchinaga tsuchinaga changed the title 課題1 / tsuchinaga tsuchinaga / 課題1 Jul 6, 2020
Comment thread kadai1/tsuchinaga/.idea/$CACHE_FILE$ Outdated
<component name="NodePackageJsonFileManager">
<packageJsonPaths />
</component>
</project> No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

.ideaディレクトリはIDEの設定ファイル郡なので、個人的にはignoreした方が良いかと思いました!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ありがとうございます

.ideaに.gitignoreがはいってたのでうまいことやってくれるもんだと思ってました:sweat_smile:

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
"path/filepath"
)

var validFileTypes = []string{"jpeg", "png"}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mapを使った方がシンプルになりますね。

var validFileTypes = map[string]bool{"jpeg": true, "png": true}

func IsValidFileType(fileType string) bool {
        return validFileTypes[fileType]
}

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
// IsDir - pathがディレクトリかどうか
func IsDir(path string) bool {
_, err := ioutil.ReadDir(path)
return err == nil

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

これではディレクトリかどうかは判断できませんね。
読み込めないエラーには単にストレージが壊れていたりする場合も含まれますので。
os.Statとか使ってos.FileInfoを取得してIsDirメソッドで確認した方がいいです。

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
}

// ExecConvert - 変換の実行
func ExecConvert(dir, src, dest string) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

conv.ExecConvertとなり、意味が二重になります。
Goは他のパッケージの関数などを呼ぶ際は必ず(ほぼ)、パッケージ名も合わせて記載するため、それも込みで名前をつける必要があります。
imgconv.Doとかであれえば、意味の重複もなく、十分意味が伝わるでしょう。

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated

files, err := ioutil.ReadDir(dirPath)
if err != nil {
log.Printf("ディレクトリ: %sが読み込めなかったためスキップします\n", dirPath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ライブラリとして使うパッケージが標準出力や標準エラー出力にメッセージを出すのはやりすぎかなと思います。
読み込めない場合はちゃんとエラーとして呼び出し元に返し、呼び出し元でどうするか判断してもらったほうが良いです。

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
func (c converter) convert(path string) {
f, err := os.Open(path)
if err != nil { // 開けない
log.Printf("ファイル: %sが開けなかったためスキップします\n", path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

上述と同様

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
log.Printf("変換後ファイル: %sが作成できなかったためスキップします\n", newFilePath)
return
}
defer o.Close()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Createは書き込みなのでCloseのエラー処理をする

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
}
defer o.Close()

err = nil

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

case句でそれぞれreturnした方が分かりやすいかと思います

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
if err != nil {
log.Printf("ファイル: %sの変換に失敗しました\n", path)
} else {
log.Printf("%s => %s\n", path, newFilePath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

logパッケージはデフォルトでは標準エラー出力に出力を行います。
そのため、エラーではない進捗などは標準出力に出力すべきです。
なお、ライブラリなので、進捗の出力はmainパッケージに任せたほうがよいでしょう。
ライブラリ側は進捗が出力できるように工夫するほうが良いです。

Comment thread kadai1/tsuchinaga/conv/converter.go Outdated
// getFileType - 画像ファイルの型を得る
func getFileType(path string) string {
f, err := os.Open(path)
if err != nil { // 開けない

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

エラーは適切にハンドルしてください。
エラーはすでに値なのでエラーを空文字で表現せずに、そのまま多値でreturnしてください。

Comment thread kadai1/tsuchinaga/main.go Outdated

import (
"flag"
"github.com/gopherdojo/dojo8/kadai1/tsuchinaga/conv"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

goimportsを使いましょう。

Comment thread kadai1/tsuchinaga/main.go Outdated

// validation
if dir == "" {
log.Fatalln("dirの指定は必須です")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

log.Fatal系は内部でos.Exit(1)を呼んでいます。
終了コードは自分で設定すべきだと思うので、fmt.Fprintlnとos.Exitを使うようにしましょう。

Comment thread kadai1/tsuchinaga/main.go Outdated
}

// 変換実行
conv.ExecConvert(dir, src, dest)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

変換を行っているのにエラー処理がmain関数にないのは不自然です。
エラー処理は必ず呼び出し元が行うようにしましょう。

@tenntenn tenntenn added kadai1 課題1 reviewed レビュー済 labels Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kadai1 課題1 reviewed レビュー済

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants