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 #12 from ryokdy/v0.8
Browse files Browse the repository at this point in the history
V0.8
  • Loading branch information
ydambara authored Jan 26, 2017
2 parents 711bd59 + 4b97470 commit 175d36a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 25 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.7
0.8

## How to Build

Expand Down
13 changes: 13 additions & 0 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func makeColumns(fields map[string]*kintone.FieldInfo) (Columns) {
continue
}
if val.Type == kintone.FT_SUBTABLE {
// record id for subtable
column := &Column{Code: val.Code, Type: val.Type}
columns = append(columns, column)

for _, subField := range val.Fields {
column := &Column{Code: subField.Code, Type: subField.Type, IsSubField: true, Table: val.Code}
columns = append(columns, column)
Expand All @@ -109,6 +113,10 @@ func makePartialColumns(fields map[string]*kintone.FieldInfo, partialFields []st
continue
}
if column.Type == kintone.FT_SUBTABLE {
// record id for subtable
column := &Column{Code: column.Code, Type: column.Type}
columns = append(columns, column)

// append all sub fields
field := fields[val]

Expand Down Expand Up @@ -217,6 +225,11 @@ func writeCsv(app *kintone.App, _writer io.Writer) error {
fmt.Fprintf(writer, "\"%d\"", record.Id())
} else if f.Code == "$revision" {
fmt.Fprintf(writer, "\"%d\"", record.Revision())
} else if f.Type == kintone.FT_SUBTABLE {
table := record.Fields[f.Code].(kintone.SubTableField)
if j < len(table) {
fmt.Fprintf(writer, "\"%d\"", table[j].Id())
}
} else if f.IsSubField {
table := record.Fields[f.Table].(kintone.SubTableField)
if j < len(table) {
Expand Down
21 changes: 12 additions & 9 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"testing"
"encoding/csv"
"github.com/ryokdy/go-kintone"
"github.com/kintone/go-kintone"
)

func makeTestData(app *kintone.App) error {
Expand Down Expand Up @@ -148,10 +148,13 @@ func TestExport2(t *testing.T) {
if row[3] != "number" {
t.Error("Invalid field code")
}
if row[4] != "table_single_line_text" {
if row[4] != "table" {
t.Error("Invalid field code")
}
if row[5] != "table_multi_line_text" {
if row[5] != "table_single_line_text" {
t.Error("Invalid field code")
}
if row[6] != "table_multi_line_text" {
t.Error("Invalid field code")
}

Expand All @@ -171,10 +174,10 @@ func TestExport2(t *testing.T) {
if row[3] != "12345" {
t.Error("Invalid 4th field value of row 1")
}
if row[4] != "table single line1" {
if row[5] != "table single line1" {
t.Error("Invalid 5th field value of row 1")
}
if row[5] != "table multi line1\nmulti line" {
if row[6] != "table multi line1\nmulti line" {
t.Error("Invalid 6th field value of row 1")
}

Expand All @@ -194,10 +197,10 @@ func TestExport2(t *testing.T) {
if row[3] != "12345" {
t.Error("Invalid 4th field value of row 2")
}
if row[4] != "table single line2" {
if row[5] != "table single line2" {
t.Error("Invalid 5th field value of row 2")
}
if row[5] != "table multi line2\nmulti line" {
if row[6] != "table multi line2\nmulti line" {
t.Error("Invalid 6th field value of row 2")
}

Expand All @@ -217,10 +220,10 @@ func TestExport2(t *testing.T) {
if row[3] != "12345" {
t.Error("Invalid 4th field value of row 3")
}
if row[4] != "" {
if row[5] != "" {
t.Error("Invalid 5th field value of row 3")
}
if row[5] != "" {
if row[6] != "" {
t.Error("Invalid 6th field value of row 3")
}

Expand Down
43 changes: 30 additions & 13 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"golang.org/x/text/transform"
)

type SubRecord struct {
Id uint64
Fields map[string]interface{}
}

func getReader(reader io.Reader) io.Reader {
encoding := getEncoding()
if (encoding == nil) {
Expand All @@ -24,15 +29,20 @@ func getReader(reader io.Reader) io.Reader {
return transform.NewReader(reader, encoding.NewDecoder())
}

func addSubField(app *kintone.App, column *Column, col string, tables map[string]map[string]interface{}) error {
if len(col) == 0 {
return nil
func getSubRecord(tableName string, tables map[string]*SubRecord) *SubRecord {
table := tables[tableName]
if table == nil {
fields := make(map[string]interface{})
table = &SubRecord{0, fields}
tables[tableName] = table
}

table := tables[column.Table]
if table == nil {
table = make(map[string]interface{})
tables[column.Table] = table
return table
}

func addSubField(app *kintone.App, column *Column, col string, table *SubRecord) error {
if len(col) == 0 {
return nil
}

if column.Type == kintone.FT_FILE {
Expand All @@ -41,12 +51,12 @@ func addSubField(app *kintone.App, column *Column, col string, tables map[string
return err
}
if field != nil {
table[column.Code] = field
table.Fields[column.Code] = field
}
} else {
field := getField(column.Type, col)
if field != nil {
table[column.Code] = field
table.Fields[column.Code] = field
}
}
return nil
Expand Down Expand Up @@ -125,21 +135,28 @@ func readCsv(app *kintone.App, _reader io.Reader) error {
record := make(map[string]interface{})

for {
tables := make(map[string]map[string]interface{})
tables := make(map[string]*SubRecord)
for i, col := range row {
column := columns[i]
if column.IsSubField {
err := addSubField(app, column, col, tables)
table := getSubRecord(column.Table, tables)
err := addSubField(app, column, col, table)
if err != nil {
return err
}
} else if column.Type == kintone.FT_SUBTABLE {
if col != "" {
subId, _ := strconv.ParseUint(col, 10, 64)
table := getSubRecord(column.Code, tables)
table.Id = subId
}
} else {
if hasTable && row[0] != "*" {
continue
}
if column.Code == "$id" {
if col != "" {
id, err = strconv.ParseUint(col, 10, 64)
id, _ = strconv.ParseUint(col, 10, 64)
}
} else if column.Code == "$revision" {

Expand Down Expand Up @@ -168,7 +185,7 @@ func readCsv(app *kintone.App, _reader io.Reader) error {
}

stf := record[key].(kintone.SubTableField)
stf = append(stf, kintone.NewRecord(table))
stf = append(stf, kintone.NewRecordWithId(table.Id, table.Fields))
record[key] = stf
}

Expand Down
2 changes: 1 addition & 1 deletion import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
//"io"
//"fmt"
"testing"
"github.com/ryokdy/go-kintone"
"github.com/kintone/go-kintone"
)

func TestImport1(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func main() {

if colNames != "" {
config.fields = strings.Split(colNames, ",")
for i, field := range config.fields {
config.fields[i] = strings.TrimSpace(field)
}
}


Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package main
import (
"os"
"strconv"
"github.com/ryokdy/go-kintone"
"github.com/kintone/go-kintone"
)

func newApp() *kintone.App {
Expand Down

0 comments on commit 175d36a

Please sign in to comment.