Susumu Yata
null+****@clear*****
Mon Sep 11 16:23:28 JST 2017
Susumu Yata 2017-09-11 16:23:28 +0900 (Mon, 11 Sep 2017) New Revision: 0558cbfeea4dcdab0ec679531bb0b780afce9681 https://github.com/groonga/grnci/commit/0558cbfeea4dcdab0ec679531bb0b780afce9681 Message: Simplify interfaces of commands returning bool. Ref: #42 Modified files: v2/db.go v2/libgrn/db_test.go Modified: v2/db.go (+80 -77) =================================================================== --- v2/db.go 2017-08-03 15:26:22 +0900 (b48d5ae) +++ v2/db.go 2017-09-11 16:23:28 +0900 (6fc1c6a) @@ -22,22 +22,25 @@ func NewDB(h Handler) *DB { } // recvBool reads the bool result from resp. -func (db *DB) recvBool(resp Response) (bool, Response, error) { +func (db *DB) recvBool(resp Response) error { defer resp.Close() + if resp.Err() != nil { + return resp.Err() + } jsonData, err := ioutil.ReadAll(resp) if err != nil { - return false, resp, err + return err } var result bool if err := json.Unmarshal(jsonData, &result); err != nil { - if resp.Err() != nil { - return false, resp, nil - } - return false, resp, NewError(ResponseError, "json.Unmarshal failed.", map[string]interface{}{ + return NewError(ResponseError, "json.Unmarshal failed.", map[string]interface{}{ "error": err.Error(), }) } - return result, resp, nil + if !result { + return NewError(ResponseError, "Operation failed.", nil) + } + return nil } // recvInt reads the int result from resp. @@ -95,17 +98,17 @@ func (db *DB) CacheLimit(max int) (int, Response, error) { } // ColumnCopy executes column_copy. -func (db *DB) ColumnCopy(from, to string) (bool, Response, error) { +func (db *DB) ColumnCopy(from, to string) error { i := strings.IndexByte(from, '.') if i == -1 { - return false, nil, NewError(CommandError, "The from must contain a dot.", map[string]interface{}{ + return NewError(CommandError, "The from must contain a dot.", map[string]interface{}{ "from": from, }) } fromTable := from[:i] fromName := from[i+1:] if i = strings.IndexByte(to, '.'); i == -1 { - return false, nil, NewError(CommandError, "The to must contain a dot.", map[string]interface{}{ + return NewError(CommandError, "The to must contain a dot.", map[string]interface{}{ "to": to, }) } @@ -118,16 +121,16 @@ func (db *DB) ColumnCopy(from, to string) (bool, Response, error) { "to_name": toName, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // ColumnCreate executes column_create. -func (db *DB) ColumnCreate(name, typ string, flags []string) (bool, Response, error) { +func (db *DB) ColumnCreate(name, typ string, flags []string) error { i := strings.IndexByte(name, '.') if i == -1 { - return false, nil, NewError(CommandError, "The name must contain a dot.", map[string]interface{}{ + return NewError(CommandError, "The name must contain a dot.", map[string]interface{}{ "name": name, }) } @@ -156,7 +159,7 @@ func (db *DB) ColumnCreate(name, typ string, flags []string) (bool, Response, er } resp, err := db.Invoke("column_create", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -255,10 +258,10 @@ func (db *DB) ColumnList(tbl string) ([]DBColumn, Response, error) { } // ColumnRemove executes column_remove. -func (db *DB) ColumnRemove(name string) (bool, Response, error) { +func (db *DB) ColumnRemove(name string) error { i := strings.IndexByte(name, '.') if i == -1 { - return false, nil, NewError(CommandError, "The name must contain a dot.", map[string]interface{}{ + return NewError(CommandError, "The name must contain a dot.", map[string]interface{}{ "name": name, }) } @@ -267,22 +270,22 @@ func (db *DB) ColumnRemove(name string) (bool, Response, error) { "name": name[i+1:], }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // ColumnRename executes column_rename. -func (db *DB) ColumnRename(name, newName string) (bool, Response, error) { +func (db *DB) ColumnRename(name, newName string) error { i := strings.IndexByte(name, '.') if i == -1 { - return false, nil, NewError(CommandError, "The name must contain a dot.", map[string]interface{}{ + return NewError(CommandError, "The name must contain a dot.", map[string]interface{}{ "name": name, }) } if j := strings.IndexByte(newName, '.'); j != -1 { if i != j || name[:i] != newName[:i] { - return false, nil, NewError(CommandError, "The names have different table names.", map[string]interface{}{ + return NewError(CommandError, "The names have different table names.", map[string]interface{}{ "name": name, "newName": newName, }) @@ -295,18 +298,18 @@ func (db *DB) ColumnRename(name, newName string) (bool, Response, error) { "new_name": newName, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // ConfigDelete executes config_delete. -func (db *DB) ConfigDelete(key, value string) (bool, Response, error) { +func (db *DB) ConfigDelete(key, value string) error { resp, err := db.Invoke("config_delete", map[string]interface{}{ "key": key, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -323,58 +326,58 @@ func (db *DB) ConfigGet(key string) (string, Response, error) { } // ConfigSet executes config_set. -func (db *DB) ConfigSet(key, value string) (bool, Response, error) { +func (db *DB) ConfigSet(key, value string) error { resp, err := db.Invoke("config_set", map[string]interface{}{ "key": key, "value": value, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // DatabaseUnmap executes database_unmap. -func (db *DB) DatabaseUnmap() (bool, Response, error) { +func (db *DB) DatabaseUnmap() error { resp, err := db.Invoke("delete", nil, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // DeleteByID executes delete. -func (db *DB) DeleteByID(tbl string, id int) (bool, Response, error) { +func (db *DB) DeleteByID(tbl string, id int) error { resp, err := db.Invoke("delete", map[string]interface{}{ "table": tbl, "id": id, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // DeleteByKey executes delete. -func (db *DB) DeleteByKey(tbl string, key interface{}) (bool, Response, error) { +func (db *DB) DeleteByKey(tbl string, key interface{}) error { resp, err := db.Invoke("delete", map[string]interface{}{ "table": tbl, "key": key, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // DeleteByFilter executes delete. -func (db *DB) DeleteByFilter(tbl, filter string) (bool, Response, error) { +func (db *DB) DeleteByFilter(tbl, filter string) error { resp, err := db.Invoke("delete", map[string]interface{}{ "table": tbl, "filter": filter, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -435,7 +438,7 @@ func NewDBIOFlushOptions() *DBIOFlushOptions { } // IOFlush executes io_flush. -func (db *DB) IOFlush(options *DBIOFlushOptions) (bool, Response, error) { +func (db *DB) IOFlush(options *DBIOFlushOptions) error { if options == nil { options = NewDBIOFlushOptions() } @@ -448,7 +451,7 @@ func (db *DB) IOFlush(options *DBIOFlushOptions) (bool, Response, error) { } resp, err := db.Invoke("io_flush", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -569,7 +572,7 @@ func (db *DB) LoadRows(tbl string, rows interface{}, options *DBLoadOptions) (in } // LockAcquire executes lock_acquire. -func (db *DB) LockAcquire(target string) (bool, Response, error) { +func (db *DB) LockAcquire(target string) error { var params map[string]interface{} if target != "" { params = map[string]interface{}{ @@ -578,13 +581,13 @@ func (db *DB) LockAcquire(target string) (bool, Response, error) { } resp, err := db.Invoke("lock_acquire", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // LockClear executes lock_clear. -func (db *DB) LockClear(target string) (bool, Response, error) { +func (db *DB) LockClear(target string) error { var params map[string]interface{} if target != "" { params = map[string]interface{}{ @@ -593,13 +596,13 @@ func (db *DB) LockClear(target string) (bool, Response, error) { } resp, err := db.Invoke("lock_clear", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // LockRelease executes lock_release. -func (db *DB) LockRelease(target string) (bool, Response, error) { +func (db *DB) LockRelease(target string) error { var params map[string]interface{} if target != "" { params = map[string]interface{}{ @@ -608,39 +611,39 @@ func (db *DB) LockRelease(target string) (bool, Response, error) { } resp, err := db.Invoke("lock_release", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // LogLevel executes log_level. -func (db *DB) LogLevel(level string) (bool, Response, error) { +func (db *DB) LogLevel(level string) error { resp, err := db.Invoke("log_level", map[string]interface{}{ "level": level, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // LogPut executes log_put. -func (db *DB) LogPut(level, msg string) (bool, Response, error) { +func (db *DB) LogPut(level, msg string) error { resp, err := db.Invoke("log_put", map[string]interface{}{ "level": level, "message": msg, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // LogReopen executes log_reopen. -func (db *DB) LogReopen() (bool, Response, error) { +func (db *DB) LogReopen() error { resp, err := db.Invoke("log_reopen", nil, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -925,7 +928,7 @@ func NewDBLogicalTableRemoveOptions() *DBLogicalTableRemoveOptions { } // LogicalTableRemove executes logical_table_remove. -func (db *DB) LogicalTableRemove(logicalTable, shardKey string, options *DBLogicalTableRemoveOptions) (bool, Response, error) { +func (db *DB) LogicalTableRemove(logicalTable, shardKey string, options *DBLogicalTableRemoveOptions) error { params := map[string]interface{}{ "logical_table": logicalTable, "shard_key": shardKey, @@ -945,7 +948,7 @@ func (db *DB) LogicalTableRemove(logicalTable, shardKey string, options *DBLogic params["force"] = options.Force resp, err := db.Invoke("logical_table_remove", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -1016,12 +1019,12 @@ func (db *DB) NormalizerList() ([]DBNormalizer, Response, error) { } // ObjectExist executes object_exist. -func (db *DB) ObjectExist(name string) (bool, Response, error) { +func (db *DB) ObjectExist(name string) error { resp, err := db.Invoke("object_exist", map[string]interface{}{ "name": name, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -1253,50 +1256,50 @@ func (db *DB) ObjectList() (map[string]*DBObject, Response, error) { } // ObjectRemove executes object_remove. -func (db *DB) ObjectRemove(name string, force bool) (bool, Response, error) { +func (db *DB) ObjectRemove(name string, force bool) error { resp, err := db.Invoke("object_remove", map[string]interface{}{ "name": name, "force": force, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // PluginRegister executes plugin_register. -func (db *DB) PluginRegister(name string) (bool, Response, error) { +func (db *DB) PluginRegister(name string) error { resp, err := db.Invoke("plugin_register", map[string]interface{}{ "name": name, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // PluginUnregister executes plugin_unregister. -func (db *DB) PluginUnregister(name string) (bool, Response, error) { +func (db *DB) PluginUnregister(name string) error { resp, err := db.Invoke("plugin_unregister", map[string]interface{}{ "name": name, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // Quit executes quit. -func (db *DB) Quit() (bool, Response, error) { +func (db *DB) Quit() error { resp, err := db.Invoke("quit", nil, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // Reindex executes reindex. -func (db *DB) Reindex(target string) (bool, Response, error) { +func (db *DB) Reindex(target string) error { var params map[string]interface{} if target != "" { params = map[string]interface{}{ @@ -1305,7 +1308,7 @@ func (db *DB) Reindex(target string) (bool, Response, error) { } resp, err := db.Invoke("reindex", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -2081,10 +2084,10 @@ func (db *DB) SelectRows(tbl string, rows interface{}, options *DBSelectOptions) } // Shutdown executes shutdown. -func (db *DB) Shutdown() (bool, Response, error) { +func (db *DB) Shutdown() error { resp, err := db.Invoke("shutdown", nil, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -2172,13 +2175,13 @@ func (db *DB) Status() (*DBStatus, Response, error) { } // TableCopy executes table_copy. -func (db *DB) TableCopy(from, to string) (bool, Response, error) { +func (db *DB) TableCopy(from, to string) error { resp, err := db.Invoke("table_copy", map[string]interface{}{ "from": from, "to": to, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -2200,7 +2203,7 @@ func NewDBTableCreateOptions() *DBTableCreateOptions { } // TableCreate executes table_create. -func (db *DB) TableCreate(name string, options *DBTableCreateOptions) (bool, Response, error) { +func (db *DB) TableCreate(name string, options *DBTableCreateOptions) error { if options == nil { options = NewDBTableCreateOptions() } @@ -2214,12 +2217,12 @@ func (db *DB) TableCreate(name string, options *DBTableCreateOptions) (bool, Res switch flag { case "TABLE_NO_KEY": if keyFlag != "" { - return false, nil, NewError(CommandError, "The combination of flags is wrong.", map[string]interface{}{ + return NewError(CommandError, "The combination of flags is wrong.", map[string]interface{}{ "flags": flags, }) } if options.KeyType != "" { - return false, nil, NewError(CommandError, "TABLE_NO_KEY denies key_type.", map[string]interface{}{ + return NewError(CommandError, "TABLE_NO_KEY denies key_type.", map[string]interface{}{ "flags": flags, "key_type": options.KeyType, }) @@ -2227,12 +2230,12 @@ func (db *DB) TableCreate(name string, options *DBTableCreateOptions) (bool, Res keyFlag = flag case "TABLE_HASH_KEY", "TABLE_PAT_KEY", "TABLE_DAT_KEY": if keyFlag != "" { - return false, nil, NewError(CommandError, "The combination of flags is wrong.", map[string]interface{}{ + return NewError(CommandError, "The combination of flags is wrong.", map[string]interface{}{ "flags": flags, }) } if options.KeyType == "" { - return false, nil, NewError(CommandError, fmt.Sprintf("%s requires key_type.", flag), map[string]interface{}{ + return NewError(CommandError, fmt.Sprintf("%s requires key_type.", flag), map[string]interface{}{ "flags": flags, "key_type": options.KeyType, }) @@ -2269,7 +2272,7 @@ func (db *DB) TableCreate(name string, options *DBTableCreateOptions) (bool, Res } resp, err := db.Invoke("table_create", params, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -2362,25 +2365,25 @@ func (db *DB) TableList() ([]DBTable, Response, error) { } // TableRemove executes table_remove. -func (db *DB) TableRemove(name string, dependent bool) (bool, Response, error) { +func (db *DB) TableRemove(name string, dependent bool) error { resp, err := db.Invoke("table_remove", map[string]interface{}{ "name": name, "dependent": dependent, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } // TableRename executes table_rename. -func (db *DB) TableRename(name, newName string) (bool, Response, error) { +func (db *DB) TableRename(name, newName string) error { resp, err := db.Invoke("table_rename", map[string]interface{}{ "name": name, "new_name": newName, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } @@ -2543,12 +2546,12 @@ func (db *DB) TokenizerList() ([]DBTokenizer, Response, error) { } // Truncate executes truncate. -func (db *DB) Truncate(target string) (bool, Response, error) { +func (db *DB) Truncate(target string) error { resp, err := db.Invoke("truncate", map[string]interface{}{ "target_name": target, }, nil) if err != nil { - return false, nil, err + return err } return db.recvBool(resp) } Modified: v2/libgrn/db_test.go (+13 -66) =================================================================== --- v2/libgrn/db_test.go 2017-08-03 15:26:22 +0900 (880b6e9) +++ v2/libgrn/db_test.go 2017-09-11 16:23:28 +0900 (5acc79e) @@ -95,34 +95,20 @@ func TestDBColumnCreate(t *testing.T) { if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { t.Fatalf("db.Restore failed: %v", err) } - _, resp, err := db.ColumnCreate("Tbl.col", "Text", nil) - if err == nil { - err = resp.Err() - } - if err != nil { + if err := db.ColumnCreate("Tbl.col", "Text", nil); err != nil { t.Fatalf("db.ColumnCreate failed: %v", err) } - result, resp, err := db.ObjectExist("Tbl.col") - if err == nil { - err = resp.Err() - } - if err != nil { + if err := db.ObjectExist("Tbl.col"); err != nil { t.Fatalf("db.ObjectExist failed: %v", err) } - if !result { - t.Fatalf("Column not found") - } } func TestDBColumnCreateInvalidTable(t *testing.T) { db, dir := makeDB(t) defer removeDB(db, dir) - _, resp, err := db.ColumnCreate("no_such_table.col", "Text", nil) - if err != nil { - t.Fatalf("db.ColumnCreate failed: %v", err) - } - if resp.Err() == nil { + err := db.ColumnCreate("no_such_table.col", "Text", nil) + if err == nil { t.Fatalf("db.ColumnCreate wrongly succeeded") } } @@ -136,11 +122,7 @@ column_create Tbl col COLUMN_SCALAR ShortText` if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { t.Fatalf("db.Restore failed: %v", err) } - _, resp, err := db.ColumnRemove("Tbl.col") - if err == nil { - err = resp.Err() - } - if err != nil { + if err := db.ColumnRemove("Tbl.col"); err != nil { t.Fatalf("db.ColumnRemove failed: %v", err) } } @@ -149,11 +131,7 @@ func TestDBColumnRemoveInvalidTable(t *testing.T) { db, dir := makeDB(t) defer removeDB(db, dir) - _, resp, err := db.ColumnRemove("no_such_table.no_such_column") - if err != nil { - t.Fatalf("db.ColumnRemove failed: %v", err) - } - if resp.Err() == nil { + if err := db.ColumnRemove("no_such_table.no_such_column"); err == nil { t.Fatalf("db.ColumnRemove wrongly succeeded") } } @@ -166,11 +144,7 @@ func TestDBColumnRemoveInvalidColumn(t *testing.T) { if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { t.Fatalf("db.Restore failed: %v", err) } - _, resp, err := db.ColumnRemove("Tbl.no_such_column") - if err != nil { - t.Fatalf("db.ColumnRemove failed: %v", err) - } - if resp.Err() == nil { + if err := db.ColumnRemove("Tbl.no_such_column"); err == nil { t.Fatalf("db.ColumnRemove wrongly succeeded") } } @@ -393,16 +367,9 @@ func TestDBQuit(t *testing.T) { db, dir := makeDB(t) defer removeDB(db, dir) - result, resp, err := db.Quit() - if err == nil { - err = resp.Err() - } - if err != nil { + if err := db.Quit(); err != nil { t.Fatalf("db.Quit failed: %v", err) } - if !result { - t.Fatalf("db.Quit failed: result = %v", result) - } } func TestRestore(t *testing.T) { @@ -750,11 +717,7 @@ load --table Tbl --columns _key --values '[["Key"]]'` if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { t.Fatalf("db.Restore failed: %v", err) } - _, resp, err := db.Truncate("Tbl") - if err == nil { - err = resp.Err() - } - if err != nil { + if err := db.Truncate("Tbl"); err != nil { t.Fatalf("db.Truncate failed: %v", err) } obj, resp, err := db.ObjectInspect("Tbl") @@ -777,11 +740,7 @@ func TestDBTruncateInvalidTarget(t *testing.T) { db, dir := makeDB(t) defer removeDB(db, dir) - _, resp, err := db.Truncate("no_such_target") - if err != nil { - t.Fatalf("db.Truncate failed: %v", err) - } - if resp.Err() == nil { + if err := db.Truncate("no_such_target"); err == nil { t.Fatalf("db.Truncate wrongly succeeded") } } @@ -794,11 +753,7 @@ func TestDBTableRemove(t *testing.T) { if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { t.Fatalf("db.Restore failed: %v", err) } - _, resp, err := db.TableRemove("Tbl", false) - if err == nil { - err = resp.Err() - } - if err != nil { + if err := db.TableRemove("Tbl", false); err != nil { t.Fatalf("db.TableRemove failed: %v", err) } } @@ -807,11 +762,7 @@ func TestDBTableRemoveInvalidName(t *testing.T) { db, dir := makeDB(t) defer removeDB(db, dir) - _, resp, err := db.TableRemove("no_such_table", false) - if err != nil { - t.Fatalf("db.TableRemove failed: %v", err) - } - if resp.Err() == nil { + if err := db.TableRemove("no_such_table", false); err == nil { t.Fatalf("db.TableRemove wrongly succeeded") } } @@ -825,11 +776,7 @@ table_create Referrer TABLE_HASH_KEY Referred` if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { t.Fatalf("db.Restore failed: %v", err) } - _, resp, err := db.TableRemove("Referred", true) - if err == nil { - err = resp.Err() - } - if err != nil { + if err := db.TableRemove("Referred", true); err != nil { t.Fatalf("db.TableRemove failed: %v", err) } } -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20170911/c9b16bd0/attachment-0001.htm