Skip to content

Commit

Permalink
Merge pull request #4 from inouet/update-command-structure
Browse files Browse the repository at this point in the history
Update command structure
  • Loading branch information
inouet committed Jun 27, 2018
2 parents 7107084 + ae2024d commit b2f076c
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 153 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ $ go get github.com/inouet/ken-all

## コマンドの使い方

住所データをjson形式に変換
住所データをJSON形式に変換

```
$ ken-all convert -i address -o json -f /tmp/KEN_ALL.CSV
$ ken-all address /tmp/KEN_ALL.CSV -t json
{"region_id":"01101","zip":"0600000","pref_kana":"ホッカイドウ","city_kana":"サッポロシチュウオウク","town_kana":"","pref":"北海道","city":"札幌市中央区","town":"","update_status":"0","update_reason":"0","pref_code":"01"}
:
```

事業所データをjson形式に変換
事業所データをJSON形式に変換

```
$ ken-all convert -i office -o json -f /tmp/JIGYOSYO.CSV
$ ken-all office /tmp/JIGYOSYO.CSV -t json
{"jis_code":"01101","kana":"(カブ) ニホンケイザイシンブンシヤ サツポロシシヤ","name":"株式会社 日本経済新聞社 札幌支社","pref":"北海道","city":"札幌市中央区","town":"北一条西","address":"6丁目1-2アーバンネット札幌ビル2F","zip7":"0608621","zip5":"060 ","post_office":"札幌中央","type":"0","is_multi":"0","update_status":"0","pref_code":"01"}
:
Expand Down
7 changes: 4 additions & 3 deletions address/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ func (reader *Reader) Read() (record []string, err error) {

inBrackets := false

list := []int{3, 4, 5, 8}

idxTownKana := 5
idxTownName := 8

Expand All @@ -39,10 +37,13 @@ func (reader *Reader) Read() (record []string, err error) {
break
}

for _, v := range list {
for _, v := range []int{3, 4, 5, 8} {
record[v] = util.NormalizeString(record[v])
}

// zip5のスペース除去
record[1] = strings.Trim(record[1], " ")

if strings.Contains(record[idxTownName], "(") {
inBrackets = true
}
Expand Down
91 changes: 91 additions & 0 deletions cmd/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cmd

import (
"errors"
"io"
"os"

"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"

"github.com/spf13/cobra"

"github.com/inouet/ken-all/address"
"github.com/inouet/ken-all/util"
"github.com/inouet/ken-all/writer"
)

// http://text.baldanders.info/golang/using-and-testing-cobra/

func init() {
output = os.Stdout
}

func newAddressCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "address [KEN_ALL.CSV]",
Short: "Convert KEN_ALL.CSV into other format.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

inputFile := args[0]

outputType, err := cmd.Flags().GetString("type")
if err != nil {
return err
}

if !isValidOutputType(outputType) {
return errors.New("type must be json or csv or tsv")
}

err = execAddressCmd(output, inputFile, outputType)
return err
},
}

cmd.Flags().StringP("type", "t", "csv", "output type [json,csv,tsv]")

return cmd
}

func execAddressCmd(w io.Writer, inputFile, outputType string) error {

ioReader, err := os.Open(inputFile)

defer ioReader.Close()

if err != nil {
return err
}

rdr := address.NewReader(transform.NewReader(ioReader, japanese.ShiftJIS.NewDecoder()))
wtr := writer.NewWriter(w, outputType)

defer wtr.Flush()

uniq := util.NewUniq()

for {
cols, err := rdr.Read()

if err == io.EOF {
break
}

rows := address.NewRows(cols)

for _, row := range rows {
// 同じ 郵便番号で同じ住所は出力しない
key := row.Zip7 + row.Pref + row.City + row.Town
if !uniq.IsUnique(key) {
continue
}

wtr.Write(row)
}
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/convert_test.go → cmd/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBuild(t *testing.T) {
inputFile := filepath.Join(testDataDir, c.input)
outputFile := filepath.Join(testDataDir, c.output)

execConvertAddressCmd(buffer, inputFile, c.outputType)
execAddressCmd(buffer, inputFile, c.outputType)

b, err := ioutil.ReadFile(outputFile)
if err != nil {
Expand Down
142 changes: 0 additions & 142 deletions cmd/convert.go

This file was deleted.

79 changes: 79 additions & 0 deletions cmd/office.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"errors"
"io"
"os"

"golang.org/x/text/transform"
"golang.org/x/text/encoding/japanese"
"github.com/spf13/cobra"

"github.com/inouet/ken-all/office"
"github.com/inouet/ken-all/writer"
)

func init() {
output = os.Stdout
}

func newOfficeCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "office [JIGYOSYO.CSV]",
Short: "Convert JIGYOSYO.CSV into other format.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

inputFile := args[0]

outputType, err := cmd.Flags().GetString("type")
if err != nil {
return err
}

if !isValidOutputType(outputType) {
return errors.New("type must be json or csv or tsv")
}

err = execOfficeCmd(output, inputFile, outputType)

return err
},
}

cmd.Flags().StringP("type", "t", "csv", "output type [json,csv,tsv]")

return cmd
}

func execOfficeCmd(w io.Writer, inputFile, outputType string) error {

ioReader, err := os.Open(inputFile)

defer ioReader.Close()

if err != nil {
return err
}

rdr := office.NewReader(transform.NewReader(ioReader, japanese.ShiftJIS.NewDecoder()))
wtr := writer.NewWriter(w, outputType)

defer wtr.Flush()

for {
cols, err := rdr.Read()

if err == io.EOF {
break
}
row := office.NewRow(cols)
err = wtr.Write(row)
if err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit b2f076c

Please sign in to comment.