From 9edda394ec07cafa963ccf3e4a0ae9db0b07761f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Jak=C3=B3n?= Date: Wed, 3 Aug 2016 12:53:15 -0400 Subject: [PATCH 1/2] Add automated scanning using user-defined slices --- cqlr.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/cqlr.go b/cqlr.go index f95c426..a900a62 100644 --- a/cqlr.go +++ b/cqlr.go @@ -82,6 +82,26 @@ func (b *Binding) Close() error { return nil } +func (b *Binding) ScanMany(dest interface{}) (int, error) { + + v := reflect.ValueOf(dest) + + if v.Kind() != reflect.Ptr || v.IsNil() { + return 0, ErrInvalidPtrToSlice + } + + sp := v.Elem() + ep := reflect.New(reflect.TypeOf(dest).Elem().Elem()) + + i := 0 + for b.Scan(ep.Interface()) { + sp.Set(reflect.Append(sp, reflect.Indirect(ep))) + i = i + 1 + } + + return i, nil +} + func (b *Binding) Scan(dest interface{}) bool { v := reflect.ValueOf(dest) @@ -215,5 +235,6 @@ func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error { } var ( - ErrMissingStrategy = errors.New("insufficient column mapping") + ErrMissingStrategy = errors.New("insufficient column mapping") + ErrInvalidPtrToSlice = errors.New("Invalid pointer to slice") ) From 2152702024399ed5739731cfc102aa6d0f50baf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Jak=C3=B3n?= Date: Wed, 3 Aug 2016 17:14:23 -0400 Subject: [PATCH 2/2] Use json tag as strategy fallback --- cqlr.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cqlr.go b/cqlr.go index a900a62..67667b8 100644 --- a/cqlr.go +++ b/cqlr.go @@ -180,6 +180,9 @@ func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error { for i := 0; i < s.NumField(); i++ { f := s.Field(i) tag := f.Tag.Get("cql") + if tag == "" { + tag = f.Tag.Get("json") + } if tag != "" { b.strategy[tag] = indirect.Field(i) } else {