Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from ryokdy/v0.7
Browse files Browse the repository at this point in the history
Release v0.7
  • Loading branch information
ushirosako authored Sep 14, 2016
2 parents d83cc8c + 50f71e0 commit ccad996
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cli-kintone is a command line utility for kintone.

## Version

0.6
0.7

## How to Build

Expand Down
32 changes: 25 additions & 7 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ func getRecords(app *kintone.App, fields []string, offset int64) ([]*kintone.Rec
}
}

func getWriter() io.Writer {
func getWriter(writer io.Writer) io.Writer {
encoding := getEncoding()
if (encoding == nil) {
return os.Stdout
return writer
}
return transform.NewWriter(os.Stdout, encoding.NewEncoder())
return transform.NewWriter(writer, encoding.NewEncoder())
}

func writeJson(app *kintone.App) error {
func writeJson(app *kintone.App, _writer io.Writer) error {
i := 0
offset := int64(0)
writer := getWriter()
writer := getWriter(_writer)

fmt.Fprint(writer, "{\"records\": [\n")
for ;;offset += EXPORT_ROW_LIMIT {
Expand Down Expand Up @@ -148,10 +148,10 @@ func hasSubTable(columns []*Column) bool {
return false
}

func writeCsv(app *kintone.App) error {
func writeCsv(app *kintone.App, _writer io.Writer) error {
i := uint64(0)
offset := int64(0)
writer := getWriter()
writer := getWriter(_writer)
var columns Columns

// retrieve field list
Expand Down Expand Up @@ -349,6 +349,10 @@ func getType(f interface{}) string {
return kintone.FT_DATETIME
case kintone.UserField:
return kintone.FT_USER
case kintone.OrganizationField:
return kintone.FT_ORGANIZATION
case kintone.GroupField:
return kintone.FT_GROUP
case kintone.CategoryField:
return kintone.FT_CATEGORY
case kintone.StatusField:
Expand Down Expand Up @@ -451,6 +455,20 @@ func toString(f interface{}, delimiter string) string {
users = append(users, user.Code)
}
return strings.Join(users, delimiter)
case kintone.OrganizationField:
organizationField := f.(kintone.OrganizationField)
organizations := make([]string, 0, len(organizationField))
for _, organization := range organizationField {
organizations = append(organizations, organization.Code)
}
return strings.Join(organizations, delimiter)
case kintone.GroupField:
groupField := f.(kintone.GroupField)
groups := make([]string, 0, len(groupField))
for _, group := range groupField {
groups = append(groups, group.Code)
}
return strings.Join(groups, delimiter)
case kintone.AssigneeField:
assigneeField := f.(kintone.AssigneeField)
users := make([]string, 0, len(assigneeField))
Expand Down
231 changes: 231 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@

package main

import (
"bytes"
"io"
"fmt"
"testing"
"encoding/csv"
"github.com/ryokdy/go-kintone"
)

func makeTestData(app *kintone.App) error {
err := deleteRecords(app, "")
if err != nil {
return err
}
records := make([]*kintone.Record, 0)

record := make(map[string]interface{})
record["single_line_text"] = kintone.SingleLineTextField("single line1")
record["multi_line_text"] = kintone.SingleLineTextField("multi line1\nmulti line")
record["number"] = kintone.DecimalField("12345")
table := make([]*kintone.Record, 0)
sub := make(map[string]interface{})
sub["table_single_line_text"] = kintone.SingleLineTextField("table single line1")
sub["table_multi_line_text"] = kintone.SingleLineTextField("table multi line1\nmulti line")
table = append(table, kintone.NewRecord(sub))
sub = make(map[string]interface{})
sub["table_single_line_text"] = kintone.SingleLineTextField("table single line2")
sub["table_multi_line_text"] = kintone.SingleLineTextField("table multi line2\nmulti line")
table = append(table, kintone.NewRecord(sub))
record["table"] = kintone.SubTableField(table)

records = append(records, kintone.NewRecord(record))

record = make(map[string]interface{})
record["single_line_text"] = kintone.SingleLineTextField("single line2")
record["multi_line_text"] = kintone.SingleLineTextField("multi line2\nmulti line")
record["number"] = kintone.DecimalField("12345")
records = append(records, kintone.NewRecord(record))

_, err = app.AddRecords(records)

return err
}

func TestExport1(t *testing.T) {
buf := &bytes.Buffer{}

app := newApp()
makeTestData(app)

config.fields = []string{"single_line_text", "multi_line_text", "number"}
config.query = "order by record_number asc"
err := writeCsv(app, buf)
if err != nil {
t.Error(err)
}

//output := buf.String()
//fmt.Printf(output)
fmt.Printf("\n")

reader := csv.NewReader(buf)

row, err := reader.Read()
if err != nil {
t.Error(err)
}
//fmt.Printf(row[0])
if row[0] != "single_line_text" {
t.Error("Invalid field code")
}
if row[1] != "multi_line_text" {
t.Error("Invalid field code")
}
if row[2] != "number" {
t.Error("Invalid field code")
}

row, err = reader.Read()
if err != nil {
t.Error(err)
}
if row[0] != "single line1" {
t.Error("Invalid 1st field value of row 1")
}
if row[1] != "multi line1\nmulti line" {
t.Error("Invalid 2nd field value of row 1")
}
if row[2] != "12345" {
t.Error("Invalid 3rd field value of row 1")
}

row, err = reader.Read()
if err != nil {
t.Error(err)
}
if row[0] != "single line2" {
t.Error("Invalid 1st field value of row 2")
}
if row[1] != "multi line2\nmulti line" {
t.Error("Invalid 2nd field value of row 2")
}
if row[2] != "12345" {
t.Error("Invalid 3rd field value of row 2")
}

row, err = reader.Read()
if err != io.EOF {
t.Error("Invalid record count")
}
}

func TestExport2(t *testing.T) {
buf := &bytes.Buffer{}

app := newApp()
makeTestData(app)

config.fields = []string{"single_line_text", "multi_line_text", "number", "table"}
config.query = "order by record_number asc"
err := writeCsv(app, buf)
if err != nil {
t.Error(err)
}

//output := buf.String()
//fmt.Printf(output)

reader := csv.NewReader(buf)

row, err := reader.Read()
if err != nil {
t.Error(err)
}
//fmt.Printf(row[0])
if row[0] != "*" {
t.Error("Invalid field code")
}
if row[1] != "single_line_text" {
t.Error("Invalid field code")
}
if row[2] != "multi_line_text" {
t.Error("Invalid field code")
}
if row[3] != "number" {
t.Error("Invalid field code")
}
if row[4] != "table_single_line_text" {
t.Error("Invalid field code")
}
if row[5] != "table_multi_line_text" {
t.Error("Invalid field code")
}

row, err = reader.Read()
if err != nil {
t.Error(err)
}
if row[0] != "*" {
t.Error("Invalid 1st field value of row 1")
}
if row[1] != "single line1" {
t.Error("Invalid 2nd field value of row 1")
}
if row[2] != "multi line1\nmulti line" {
t.Error("Invalid 3rd field value of row 1")
}
if row[3] != "12345" {
t.Error("Invalid 4th field value of row 1")
}
if row[4] != "table single line1" {
t.Error("Invalid 5th field value of row 1")
}
if row[5] != "table multi line1\nmulti line" {
t.Error("Invalid 6th field value of row 1")
}

row, err = reader.Read()
if err != nil {
t.Error(err)
}
if row[0] != "" {
t.Error("Invalid 1st field value of row 2")
}
if row[1] != "single line1" {
t.Error("Invalid 2nd field value of row 2")
}
if row[2] != "multi line1\nmulti line" {
t.Error("Invalid 3rd field value of row 2")
}
if row[3] != "12345" {
t.Error("Invalid 4th field value of row 2")
}
if row[4] != "table single line2" {
t.Error("Invalid 5th field value of row 2")
}
if row[5] != "table multi line2\nmulti line" {
t.Error("Invalid 6th field value of row 2")
}

row, err = reader.Read()
if err != nil {
t.Error(err)
}
if row[0] != "*" {
t.Error("Invalid 1st field value of row 3")
}
if row[1] != "single line2" {
t.Error("Invalid 2nd field value of row 3")
}
if row[2] != "multi line2\nmulti line" {
t.Error("Invalid 3rd field value of row 3")
}
if row[3] != "12345" {
t.Error("Invalid 4th field value of row 3")
}
if row[4] != "" {
t.Error("Invalid 5th field value of row 3")
}
if row[5] != "" {
t.Error("Invalid 6th field value of row 3")
}

row, err = reader.Read()
if err != io.EOF {
t.Error("Invalid record count")
}
}
36 changes: 25 additions & 11 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"golang.org/x/text/transform"
)

func getReader(file *os.File) io.Reader {
func getReader(reader io.Reader) io.Reader {
encoding := getEncoding()
if (encoding == nil) {
return file
return reader
}
return transform.NewReader(file, encoding.NewDecoder())
return transform.NewReader(reader, encoding.NewDecoder())
}

func addSubField(app *kintone.App, column *Column, col string, tables map[string]map[string]interface{}) error {
Expand Down Expand Up @@ -52,14 +52,9 @@ func addSubField(app *kintone.App, column *Column, col string, tables map[string
return nil
}

func readCsv(app *kintone.App, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
func readCsv(app *kintone.App, _reader io.Reader) error {

reader := csv.NewReader(getReader(file))
reader := csv.NewReader(getReader(_reader))

head := true
recordsInsert := make([]*kintone.Record, 0, IMPORT_ROW_LIMIT)
Expand Down Expand Up @@ -98,7 +93,7 @@ func readCsv(app *kintone.App, filePath string) error {
row = *peeked
peeked = nil
}
//fmt.Printf("%#v", row)
//fmt.Printf("%#v\n", row)
if head && columns == nil {
columns = make([]*Column, 0)
for _, col := range row {
Expand Down Expand Up @@ -280,6 +275,7 @@ func uploadFile(app *kintone.App, filePath string) (string, error) {

func insert(app *kintone.App, recs []*kintone.Record) error {
var err error

_, err = app.AddRecords(recs)

return err
Expand Down Expand Up @@ -416,6 +412,24 @@ func getField(fieldType string, value string) interface{} {
}
}
return ret
case kintone.FT_ORGANIZATION:
organizations := strings.Split(value, "\n")
var ret kintone.OrganizationField = []kintone.Organization{}
for _, organization := range organizations {
if len(strings.TrimSpace(organization)) > 0 {
ret = append(ret, kintone.Organization{Code: organization})
}
}
return ret
case kintone.FT_GROUP:
groups := strings.Split(value, "\n")
var ret kintone.GroupField = []kintone.Group{}
for _, group := range groups {
if len(strings.TrimSpace(group)) > 0 {
ret = append(ret, kintone.Group{Code: group})
}
}
return ret
case kintone.FT_CATEGORY:
return nil
case kintone.FT_STATUS:
Expand Down
Loading

0 comments on commit ccad996

Please sign in to comment.