Skip to content

Commit

Permalink
Merge pull request #7 from Kyash/feature/get-ready-for-oss
Browse files Browse the repository at this point in the history
Feature/get ready for oss
  • Loading branch information
utzuro authored Jul 29, 2024
2 parents 792736d + 8cc52e8 commit aa5f6fe
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 95 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 Kyash Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions README.ja.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# zengin-go

**zengin-go** は、日本の銀行が金融取引を行うために一般的に使用する全銀フォーマットのテキストファイルを解析するためのGoライブラリです。

## 特徴

- 全銀フォーマットのテキストファイルを解析し、CSV形式のデータまたはすべてのフィールドを含むGo構造体として取得できます。
- UTF-8およびShift-JISの両方のエンコーディングをサポートし、他のエンコーディングもサポートする可能性があります(未テスト)。

## インターフェース

このライブラリは、全銀フォーマットのテキストファイルをCSV形式のデータまたはGo構造体に変換するために使用できます。

```go
// 全銀フォーマットファイルを解析し、すべてのフィールドを含む行を返します
func Parse(reader zengin.Reader) ([]types.Transfer, error)

// 全銀フォーマットファイルを解析し、以下のフィールド名を持つCSV形式のテーブルを返します
// SenderName,TransferDate,BankCode,BranchCode,AccountType,AccountNumber,AccountName,Amount
func ToCSV(reader zengin.Reader) ([][]string, error)

// 全銀フォーマットファイルを解析し、以下のフィールド名を持つCSV形式のテーブルを返します
// 振込名義人, 振込日, 金融機関コード, 支店コード, 科目, 口座番号, 口座名義人, 金額
func ToCSVJa(reader zengin.Reader) ([][]string, error)
```

解析可能なフィールドは [types/fields.go](./types/fields.go) にあります。


## インストール

このライブラリをインストールするには、`go get` コマンドを使用します:

```bash
go get github.com/Kyash/zengin-go
```

## 使用方法

[サンプル](./samples/main.go)を参照にしてください。

## コントリビュート

問題や機能リクエストがある場合は、イシューを作成するかプルリクエストを作ってください。

## ライセンス

このプロジェクトはMITライセンスの下でライセンスされています。詳細については [LICENSE](./LICENSE) ファイルを参照してください。
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
# zengin-go
全銀フォーマット用のライブラリ

[Japanese](./README.ja.md)

**zengin-go** is a Go library designed to parse Zengin format text files,
which are commonly used by Japanese banks to conduct financial transactions.

## Features

- Parses Zengin format text files (全銀フォーマット) and get CSV-like data or all the fields as a go struct.
- Supports both UTF-8 and Shift-JIS encodings and possibly other encodings (not tested).

## Interface

The library can be used to convert Zengin format text files into CSV-like data or Go structs.
```go
// Parse Zengin format file and return rows with all fields
func Parse(reader zengin.Reader) ([]types.Transfer, error)

// Parse Zengin format file and return a csv like table with field names as below:
// SenderName,TransferDate,BankCode,BranchCode,AccountType,AccountNumber,AccountName,Amount
func ToCSV(reader zengin.Reader) ([][]string, error)

// Parse Zengin format file and return a csv like table with field names as below:
// 振込名義人,振込日,金融機関コード,支店コード,科目,口座番号,口座名義人,金額
func ToCSVJa(reader zengin.Reader) ([][]string, error) {
```
Parsable fields can be found in [types/fields.go](./types/fields.go).
## Installation
To install the library, use the `go get` command:
```bash
go get github.com/Kyash/zengin-go
```
## Usage
See [sample](./samples/main.go)
## Contributing
If there are any issues or feature requests, please create an issue or a pull request.
## License
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
41 changes: 21 additions & 20 deletions internal/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"bufio"
"errors"
"github.com/Kyash/zengin-go/types"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
Expand All @@ -16,13 +17,13 @@ type Reader interface {
io.Reader
}

func guessEncoding(file Reader) (*bufio.Scanner, Encoding, error) {
var encoding Encoding
func guessEncoding(file Reader) (*bufio.Scanner, types.Encoding, error) {
var encoding types.Encoding
reader := bufio.NewReader(file)
peekBytes, err := reader.Peek(1024)
if err != nil && err != io.EOF {
log.Fatal("couldn't read from file", "error", err)
return nil, EncodingUndefined, err
return nil, types.EncodingUndefined, err
}

// Ignore "certain" (3rd value), as during testing it was always false, even though it correctly detects utf-8.
Expand All @@ -31,27 +32,27 @@ func guessEncoding(file Reader) (*bufio.Scanner, Encoding, error) {
var scanner *bufio.Scanner
switch name {
case "utf-8":
encoding = EncodingUTF8
encoding = types.EncodingUTF8
scanner = bufio.NewScanner(reader)
// Shift-JIS can't be reliably detected, so we'll assume it's Shift-JIS if it's not UTF-8.
default:
encoding = EncodingShiftJIS
encoding = types.EncodingShiftJIS
scanner = bufio.NewScanner(transform.NewReader(reader, japanese.ShiftJIS.NewDecoder()))
}

return scanner, encoding, nil
}

func parseCategoryCode(categoryCode string) (CategoryCode, error) {
func parseCategoryCode(categoryCode string) (types.CategoryCode, error) {
switch categoryCode {
case "21":
return CategoryCodeCombination, nil
return types.CategoryCodeCombination, nil
case "11", "71":
return CategoryCodePayment, nil
return types.CategoryCodePayment, nil
case "12", "72":
return CategoryCodeBonus, nil
return types.CategoryCodeBonus, nil
default:
return CategoryCodeUndefined, errors.New("unknown category code: " + categoryCode)
return types.CategoryCodeUndefined, errors.New("unknown category code: " + categoryCode)
}
}

Expand All @@ -62,29 +63,29 @@ func parseSenderCode(senderCode string) (string, error) {
return senderCode, nil
}

func parseAccountType(accountType string) (AccountType, error) {
func parseAccountType(accountType string) (types.AccountType, error) {
switch accountType {
case "1":
return AccountTypeRegular, nil
return types.AccountTypeRegular, nil
case "2":
return AccountTypeChecking, nil
return types.AccountTypeChecking, nil
case "4":
return AccountTypeSavings, nil
return types.AccountTypeSavings, nil
default:
return AccountTypeUndefined, errors.New("invalid account type: " + accountType)
return types.AccountTypeUndefined, errors.New("invalid account type: " + accountType)
}
}

func parseNewCode(accountType string) (NewCode, error) {
func parseNewCode(accountType string) (types.NewCode, error) {
switch accountType {
case "1":
return CodeFirstTransfer, nil
return types.CodeFirstTransfer, nil
case "2":
return CodeUpdateTransfer, nil
return types.CodeUpdateTransfer, nil
case "0":
return CodeOther, nil
return types.CodeOther, nil
default:
return CodeUndefined, errors.New("invalid account type: " + accountType)
return types.CodeUndefined, errors.New("invalid account type: " + accountType)
}
}

Expand Down
Loading

0 comments on commit aa5f6fe

Please sign in to comment.