-
Notifications
You must be signed in to change notification settings - Fork 7
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
Kadai2-dshirae #16
Open
showchan
wants to merge
2
commits into
master
Choose a base branch
from
kadai2-dshirae
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kadai2-dshirae #16
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 開発内容 | ||
|
||
## 課題1 | ||
|
||
画像変換コマンドを作ろう | ||
|
||
### 次の仕様を満たすコマンドを作って下さい | ||
- ディレクトリを指定する | ||
- 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト) | ||
- ディレクトリ以下は再帰的に処理する | ||
- 変換前と変換後の画像形式を指定できる(オプション) | ||
|
||
### 以下を満たすように開発してください | ||
- mainパッケージと分離する | ||
- 自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
- 準標準パッケージ:golang.org/x以下のパッケージ | ||
- ユーザ定義型を作ってみる | ||
- GoDocを生成してみる | ||
|
||
## 提出内容 | ||
|
||
`$ chpics dirname [-i=imgs -o=imgs]` | ||
|
||
### default動作 | ||
指定されたdirectory内に含まれる全てのjpg画像をpng画像に変換する | ||
|
||
### option | ||
- imgs : jpg/png/gif | ||
|
||
### | ||
|
||
## 課題の提出方法 | ||
|
||
``` | ||
1回目の課題を提出する場合は次のようにコードを書いて下さい。 | ||
|
||
ブランチ名をkadai1-tenntennのようにする | ||
kadai1/tenntennのようにディレクトリを作る | ||
READMEに説明や文章による課題の回答を書く | ||
PRを送る | ||
|
||
※FBには時間がかかる可能性があります。 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"./convert" | ||
) | ||
|
||
// e.g.) chimgs dir [-i=jpg/png] [-o=png/jpg] | ||
func main() { | ||
|
||
var ( | ||
src = flag.String("i", "jpeg", "string flag") | ||
dest = flag.String("o", "png", "string flag") | ||
) | ||
flag.Parse() | ||
|
||
if (flag.NArg() == 0) && (flag.NFlag() == 0) { | ||
fmt.Println("Usage: chimgs DIR [-i=imgext] [-o=imgext]") | ||
os.Exit(1) | ||
} | ||
dirname := flag.Arg(0) | ||
|
||
filepath.Walk(dirname, | ||
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. エラー処理 |
||
func(path string, info os.FileInfo, err error) error { | ||
if info.IsDir() { | ||
return nil | ||
} | ||
err = convert.PixFile(path, *src, *dest) | ||
return err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package convert | ||
|
||
/* | ||
画像変換コマンド | ||
|
||
次の仕様を満たすコマンドを作って下さい | ||
|
||
ディレクトリを指定する | ||
指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト) | ||
ディレクトリ以下は再帰的に処理する | ||
変換前と変換後の画像形式を指定できる(オプション) | ||
|
||
以下を満たすように開発してください | ||
|
||
mainパッケージと分離する | ||
自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
準標準パッケージ:golang.org/x以下のパッケージ | ||
ユーザ定義型を作ってみる | ||
GoDocを生成してみる | ||
*/ | ||
|
||
import ( | ||
"image" | ||
"image/gif" | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
) | ||
|
||
// picture file 変換関数 | ||
func PixFile(org string, src string, dest string) error { | ||
|
||
// open file | ||
file, err := os.Open(org) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
// image reading. | ||
img, format, err := image.Decode(file) | ||
if err != nil { | ||
// not image | ||
return err | ||
} | ||
|
||
// 元ファイルが指定外ならスキップ | ||
if format != src { | ||
return nil | ||
} | ||
|
||
// 出力先ファイル | ||
savefile, err := os.Create(org + "." + dest) | ||
if err != nil { | ||
return err | ||
} | ||
defer savefile.Close() | ||
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. エラー処理 |
||
|
||
switch dest { | ||
case "jpg", "jpeg": | ||
opts := &jpeg.Options{} | ||
jpeg.Encode(savefile, img, opts) | ||
case "png": | ||
png.Encode(savefile, img) | ||
case "gif": | ||
opts := &gif.Options{} | ||
gif.Encode(savefile, img, opts) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 課題2 | ||
|
||
## テストを書いてみよう | ||
|
||
### 1回目の宿題のテストを作ってみてください | ||
- テストのしやすさを考えてリファクタリングしてみる | ||
課題1に実装しなかった、`ユーザ定義型を作ってみる` を実装した | ||
- テストのカバレッジを取ってみる | ||
課題提出時コードにて80% のカバレッジ | ||
`$ go test -coverprofile=cover.out -run ''` | ||
`$ go tool cover -html=cover.out` | ||
- テーブル駆動テストを行う | ||
実装しました | ||
- テストヘルパーを作ってみる | ||
実装したが、うまく使えてない気がする。(再調査) | ||
|
||
## io.Readerとio.Writer | ||
|
||
### io.Readerとio.Writerについて調べてみよう | ||
- 標準パッケージでどのように使われているか | ||
[WIP] | ||
- io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる | ||
抽象化された事でOS間の違いが吸収される。標準出力(console)など |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"./convert" | ||
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. 相対パスにしない |
||
) | ||
|
||
// e.g.) chimgs dir [-i=jpg/png] [-o=png/jpg] | ||
func main() { | ||
|
||
var pix convert.PixConv | ||
|
||
pix.Src = flag.String("i", "jpeg", "string flag") | ||
pix.Dest = flag.String("o", "png", "string flag") | ||
flag.Parse() | ||
|
||
if (flag.NArg() == 0) && (flag.NFlag() == 0) { | ||
fmt.Println("Usage: chimgs DIR [-i=imgext] [-o=imgext]") | ||
os.Exit(1) | ||
} | ||
dirname := flag.Arg(0) | ||
|
||
filepath.Walk(dirname, | ||
func(path string, info os.FileInfo, err error) error { | ||
if info.IsDir() { | ||
return nil | ||
} | ||
pix.Path = path | ||
err = convert.PixFile(data) | ||
return err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package convert | ||
|
||
/* | ||
画像変換コマンド | ||
|
||
次の仕様を満たすコマンドを作って下さい | ||
|
||
ディレクトリを指定する | ||
指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト) | ||
ディレクトリ以下は再帰的に処理する | ||
変換前と変換後の画像形式を指定できる(オプション) | ||
|
||
以下を満たすように開発してください | ||
|
||
mainパッケージと分離する | ||
自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
準標準パッケージ:golang.org/x以下のパッケージ | ||
ユーザ定義型を作ってみる | ||
GoDocを生成してみる | ||
*/ | ||
|
||
import ( | ||
"image" | ||
"image/gif" | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
) | ||
|
||
type PixConv struct { | ||
Path string | ||
Src string | ||
Dest string | ||
} | ||
|
||
// picture file 変換関数 | ||
func PixFile(filedata PixConv) error { | ||
|
||
// open file | ||
file, err := os.Open(filedata.Path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
// image reading. | ||
img, format, err := image.Decode(file) | ||
if err != nil { | ||
// not image | ||
return err | ||
} | ||
|
||
// 元ファイルが指定外ならスキップ | ||
if format != filedata.Src { | ||
return nil | ||
} | ||
|
||
// 出力先ファイル | ||
savefile, err := os.Create(filedata.Path + "." + filedata.Dest) | ||
if err != nil { | ||
return err | ||
} | ||
defer savefile.Close() | ||
|
||
switch filedata.Dest { | ||
case "jpg", "jpeg": | ||
opts := &jpeg.Options{} | ||
jpeg.Encode(savefile, img, opts) | ||
case "png": | ||
png.Encode(savefile, img) | ||
case "gif": | ||
opts := &gif.Options{} | ||
gif.Encode(savefile, img, opts) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package convert | ||
|
||
import "testing" | ||
|
||
// picture file 変換テスト | ||
func TestPixFile(t *testing.T) { | ||
|
||
testTables := []PixConv{ | ||
{"./test.jpg", "jpeg", "png"}, | ||
{"./test.jpg", "jpeg", "gif"}, | ||
{"./test.png", "png", "jpeg"}, | ||
{"./test.png", "png", "gif"}, | ||
{"./test.gif", "gif", "jpeg"}, | ||
{"./test.gif", "gif", "png"}, | ||
} | ||
|
||
for _, testpix := range testTables { | ||
testPixFile(t, testpix) | ||
} | ||
} | ||
|
||
func testPixFile(t *testing.T, testpix PixConv) { | ||
t.Helper() | ||
err := PixFile(testpix) | ||
if err != nil { | ||
t.Fatalf("failed test %#v", err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
標準エラー出力にだす