Skip to content

Commit

Permalink
Merge pull request #859 from thrawn01/improve-mapscan-docs
Browse files Browse the repository at this point in the history
Improved MapScan() documentation and added new MapScan test
  • Loading branch information
Zariel committed Feb 15, 2017
2 parents 9d95e30 + 9fd8d12 commit 1f87449
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
49 changes: 48 additions & 1 deletion cassandra_test.go
Expand Up @@ -620,7 +620,8 @@ func TestMapScanWithRefMap(t *testing.T) {
m["testfullname"] = FullName{"John", "Doe"}
m["testint"] = 100

if err := session.Query(`INSERT INTO scan_map_ref_table (testtext, testfullname, testint) values (?,?,?)`, m["testtext"], m["testfullname"], m["testint"]).Exec(); err != nil {
if err := session.Query(`INSERT INTO scan_map_ref_table (testtext, testfullname, testint) values (?,?,?)`,
m["testtext"], m["testfullname"], m["testint"]).Exec(); err != nil {
t.Fatal("insert:", err)
}

Expand All @@ -646,7 +647,53 @@ func TestMapScanWithRefMap(t *testing.T) {
t.Fatal("returned testinit did not match")
}
}
if testText != "testtext" {
t.Fatal("returned testtext did not match")
}
if testFullName.FirstName != "John" || testFullName.LastName != "Doe" {
t.Fatal("returned testfullname did not match")
}
}

func TestMapScan(t *testing.T) {
session := createSession(t)
defer session.Close()
if err := createTable(session, `CREATE TABLE gocql_test.scan_map_table (
fullname text PRIMARY KEY,
age int,
address inet,
)`); err != nil {
t.Fatal("create table:", err)
}

if err := session.Query(`INSERT INTO scan_map_table (fullname, age, address) values (?,?,?)`,
"Grace Hopper", 31, net.ParseIP("10.0.0.1")).Exec(); err != nil {
t.Fatal("insert:", err)
}
if err := session.Query(`INSERT INTO scan_map_table (fullname, age, address) values (?,?,?)`,
"Ada Lovelace", 30, net.ParseIP("10.0.0.2")).Exec(); err != nil {
t.Fatal("insert:", err)
}

iter := session.Query(`SELECT * FROM scan_map_table`).Iter()

// First iteration
row := make(map[string]interface{})
if !iter.MapScan(row) {
t.Fatal("select:", iter.Close())
}
assertEqual(t, "fullname", "Ada Lovelace", row["fullname"])
assertEqual(t, "age", 30, row["age"])
assertEqual(t, "address", "10.0.0.2", row["address"])

// Second iteration using a new map
row = make(map[string]interface{})
if !iter.MapScan(row) {
t.Fatal("select:", iter.Close())
}
assertEqual(t, "fullname", "Grace Hopper", row["fullname"])
assertEqual(t, "age", 31, row["age"])
assertEqual(t, "address", "10.0.0.1", row["address"])
}

func TestSliceMap(t *testing.T) {
Expand Down
36 changes: 36 additions & 0 deletions helpers.go
Expand Up @@ -250,6 +250,42 @@ func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {

// MapScan takes a map[string]interface{} and populates it with a row
// that is returned from cassandra.
//
// Each call to MapScan() must be called with a new map object.
// During the call to MapScan() any pointers in the existing map
// are replaced with non pointer types before the call returns
//
// iter := session.Query(`SELECT * FROM mytable`).Iter()
// for {
// // New map each iteration
// row = make(map[string]interface{})
// if !iter.MapScan(row) {
// break
// }
// // Do things with row
// if fullname, ok := row["fullname"]; ok {
// fmt.Printf("Full Name: %s\n", fullname)
// }
// }
//
// You can also pass pointers in the map before each call
//
// var fullName FullName // Implements gocql.Unmarshaler and gocql.Marshaler interfaces
// var address net.IP
// var age int
// iter := session.Query(`SELECT * FROM scan_map_table`).Iter()
// for {
// // New map each iteration
// row := map[string]interface{}{
// "fullname": &fullName,
// "age": &age,
// "address": &address,
// }
// if !iter.MapScan(row) {
// break
// }
// fmt.Printf("First: %s Age: %d Address: %q\n", fullName.FirstName, age, address)
// }
func (iter *Iter) MapScan(m map[string]interface{}) bool {
if iter.err != nil {
return false
Expand Down

0 comments on commit 1f87449

Please sign in to comment.