From bc1d1e667fcc92c5a5d1c13d447c5a0f0da7f1ff Mon Sep 17 00:00:00 2001 From: Steve Domino Date: Mon, 12 Oct 2015 12:14:54 -0600 Subject: [PATCH] actions are now a constant rather than a string --- README.md | 2 +- scribble.go | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9709930..41d4d45 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ if err != nil { record := Record{} // create a new transaction for scribble to run -t := scribble.Transaction{Action: "read", Collection: "records", ResourceID: "", Container: &record} +t := scribble.Transaction{Action: scribble.READ, Collection: "records", ResourceID: "", Container: &record} // have scribble attempt to run the transaction if err := database.Transact(t); err != nil { diff --git a/scribble.go b/scribble.go index 3f6720d..a1e4da9 100644 --- a/scribble.go +++ b/scribble.go @@ -37,13 +37,21 @@ type ( // a Transactions is what is used by a Driver to complete database operations Transaction struct { - Action string // the action for scribble to preform + Action int // the action for scribble to preform Collection string // the forlder for scribble to read/write to/from ResourceID string // the unique ID of the record Container interface{} // what scribble will marshal from or unmarshal into } ) +// +const ( + WRITE = iota + READ + READALL + DELETE +) + // New creates a new scribble database at the desired directory location, and // returns a *Driver to then use for interacting with the database func New(dir string, logger hatchet.Logger) (*Driver, error) { @@ -72,23 +80,23 @@ func New(dir string, logger hatchet.Logger) (*Driver, error) { // Transact determins the type of transactions to be run, and calls the appropriate // method to complete the operation -func (d *Driver) Transact(trans Transaction) error { +func (d *Driver) Transact(trans Transaction) (err error) { // determin transaction to be run switch trans.Action { - case "write": + case WRITE: return d.write(trans) - case "read": + case READ: return d.read(trans) - case "readall": + case READALL: return d.readAll(trans) - case "delete": + case DELETE: return d.delete(trans) default: return errors.New(fmt.Sprintf("Unsupported action %+v", trans.Action)) } - return nil + return } // write locks the database and then proceeds to write the data represented by a