Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
✌️ Address small nitpicks around code. Also add better handling aroun…
Browse files Browse the repository at this point in the history
…d errors
  • Loading branch information
suhailpatel committed Jun 24, 2019
1 parent 5e9f840 commit 4b6277f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
3 changes: 1 addition & 2 deletions multiop.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ func (mo multiOp) RunAtomically() error {
return err
}
stmts := make([]Statement, len(mo))
var qe QueryExecutor
for i, op := range mo {
s := op.GenerateStatement()
qe = op.QueryExecutor()
stmts[i] = s
}

qe := mo.QueryExecutor()
return qe.ExecuteAtomicallyWithOptions(mo.Options(), stmts)
}

Expand Down
8 changes: 5 additions & 3 deletions reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package reflect

import (
"fmt"
r "reflect"
"strings"
)
Expand Down Expand Up @@ -49,13 +50,14 @@ func StructToMap(val interface{}) (map[string]interface{}, bool) {
// Field int "myName"
//
// If lowercaseFields is set to true, field names are lowercased in the map
func StructFieldMap(val interface{}, lowercaseFields bool) (map[string]Field, bool) {
func StructFieldMap(val interface{}, lowercaseFields bool) (map[string]Field, error) {
// indirect so function works with both structs and pointers to them
structVal := r.Indirect(r.ValueOf(val))
kind := structVal.Kind()
if kind != r.Struct {
return nil, false
return nil, fmt.Errorf("expected val to be a struct, got %T", val)
}

structFields := cachedTypeFields(structVal.Type())
mapVal := make(map[string]Field, len(structFields))
for _, info := range structFields {
Expand All @@ -65,7 +67,7 @@ func StructFieldMap(val interface{}, lowercaseFields bool) (map[string]Field, bo
}
mapVal[name] = info
}
return mapVal, true
return mapVal, nil
}

// MapToStruct converts a map to a struct. It is the inverse of the StructToMap
Expand Down
24 changes: 17 additions & 7 deletions reflect/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func TestMapToStruct(t *testing.T) {
}

func TestStructFieldMap(t *testing.T) {
m, ok := StructFieldMap(Tweet{}, false)
if !ok {
t.Fatalf("expected field map to be created")
m, err := StructFieldMap(Tweet{}, false)
if err != nil {
t.Fatalf("expected field map to be created, err: %v", err)
}

if timeline, ok := m["Timeline"]; ok {
Expand Down Expand Up @@ -185,7 +185,11 @@ func TestStructFieldMap(t *testing.T) {
}

// Test lowercasing fields
m2, ok := StructFieldMap(Tweet{}, true)
m2, err := StructFieldMap(Tweet{}, true)
if err != nil {
t.Fatalf("expected field map to be created, err: %v", err)
}

if timeline, ok := m2["timeline"]; !ok {
if timeline.Name() != "Timeline" {
t.Errorf("Timeline should have name 'Timeline' but got %s", timeline.Name())
Expand All @@ -199,9 +203,9 @@ func TestStructFieldMapEmbeddedStruct(t *testing.T) {
Embedder string
}

m, ok := StructFieldMap(EmbeddedTweet{}, false)
if !ok {
t.Fatalf("expected field map to be created")
m, err := StructFieldMap(EmbeddedTweet{}, false)
if err != nil {
t.Fatalf("expected field map to be created, err: %v", err)
}

if timeline, ok := m["Timeline"]; ok {
Expand All @@ -227,7 +231,13 @@ func TestStructFieldMapEmbeddedStruct(t *testing.T) {
} else {
t.Errorf("Embedder should be present but wasn't: %+v", m)
}
}

func TestStructFieldMapNonStruct(t *testing.T) {
_, err := StructFieldMap(42, false)
if err == nil {
t.Fatalf("expected StructFieldMap to have an error, got nil error")
}
}

func TestFieldsAndValues(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func (s *scanner) iterSingle(iter Scannable) (int, error) {
// within the target struct type
func (s *scanner) structFields(structType reflect.Type) ([]*r.Field, error) {
fmPtr := reflect.New(structType).Interface()
m, ok := r.StructFieldMap(fmPtr, true)
if !ok {
return nil, fmt.Errorf("could not decode struct of type %T", fmPtr)
m, err := r.StructFieldMap(fmPtr, true)
if err != nil {
return nil, fmt.Errorf("could not decode struct of type %T: %v", fmPtr, err)
}

structFields := make([]*r.Field, len(s.stmt.fieldNames))
Expand Down

0 comments on commit 4b6277f

Please sign in to comment.