-
-
Notifications
You must be signed in to change notification settings - Fork 408
feature: create rows columns and value from structs and slice by custom tag #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
79f6b89
eb54fbc
462cf4b
9c4a207
f19b529
db273b6
56464bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ import ( | |
"bytes" | ||
"database/sql/driver" | ||
"encoding/csv" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
|
@@ -127,6 +129,103 @@ type Rows struct { | |
closeErr error | ||
} | ||
|
||
// new Rows and add a set of driver.Value by using the struct reflect tag | ||
func newRowsFromStruct(m interface{}, tagName ...string) (*Rows, error) { | ||
val := reflect.ValueOf(m).Elem() | ||
num := val.NumField() | ||
if num == 0 { | ||
return nil, errors.New("no properties available") | ||
} | ||
columns := make([]string, 0, num) | ||
var values []driver.Value | ||
tag := "json" | ||
if len(tagName) > 0 { | ||
tag = tagName[0] | ||
} | ||
for i := 0; i < num; i++ { | ||
f := val.Type().Field(i) | ||
column := f.Tag.Get(tag) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code allows to use JSON tags, but doesn't fully support them. Examples of incorrectly handled values: "-", "data,omitempty". I think that using JSON tags is a bad default. |
||
if len(column) > 0 { | ||
columns = append(columns, column) | ||
values = append(values, val.Field(i).Interface()) | ||
} | ||
} | ||
if len(columns) == 0 { | ||
return nil, errors.New("tag not match") | ||
} | ||
rows := &Rows{ | ||
cols: columns, | ||
nextErr: make(map[int]error), | ||
converter: driver.DefaultParameterConverter, | ||
} | ||
return rows.AddRow(values...), nil | ||
} | ||
|
||
// NewRowsFromInterface new Rows from struct or slice or array reflect with tagName | ||
// NOTE: arr/slice must be of the same type | ||
// tagName default "json" | ||
func NewRowsFromInterface(m interface{}, tagName string) (*Rows, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go has generics now. It would be better to have multiple functions with stronger typing. NewRowsFromSlice |
||
kind := reflect.TypeOf(m).Elem().Kind() | ||
if kind == reflect.Ptr { | ||
kind = reflect.TypeOf(m).Kind() | ||
} | ||
switch kind { | ||
case reflect.Slice, reflect.Array: | ||
return newRowsFromSliceOrArray(m, tagName) | ||
case reflect.Struct: | ||
return newRowsFromStruct(m, tagName) | ||
default: | ||
return nil, errors.New("the type m must in struct or slice or array") | ||
} | ||
} | ||
|
||
// new Rows and add multiple sets of driver.Value by using the tags of the element in reflect type slice/array | ||
func newRowsFromSliceOrArray(m interface{}, tagName string) (*Rows, error) { | ||
vals := reflect.ValueOf(m) | ||
if vals.Len() == 0 { | ||
return nil, errors.New("the len of m is zero") | ||
} | ||
typ := reflect.TypeOf(vals.Index(0).Interface()).Elem() | ||
if typ.Kind() != reflect.Struct { | ||
return nil, errors.New("param type must be struct") | ||
} | ||
if typ.NumField() == 0 { | ||
return nil, errors.New("no properties available") | ||
} | ||
var idx []int | ||
tag := "json" | ||
if len(tagName) > 0 { | ||
tag = tagName | ||
} | ||
columns := make([]string, 0, typ.NumField()) | ||
for i := 0; i < typ.NumField(); i++ { | ||
f := typ.Field(i) | ||
column := f.Tag.Get(tag) | ||
if len(column) > 0 { | ||
columns = append(columns, column) | ||
idx = append(idx, i) | ||
} | ||
} | ||
if len(columns) == 0 { | ||
return nil, errors.New("tag not match") | ||
} | ||
rows := &Rows{ | ||
cols: columns, | ||
nextErr: make(map[int]error), | ||
converter: driver.DefaultParameterConverter, | ||
} | ||
for i := 0; i < vals.Len(); i++ { | ||
val := vals.Index(i).Elem() | ||
values := make([]driver.Value, 0, len(idx)) | ||
for _, i := range idx { | ||
// NOTE: field by name ptr nil | ||
values = append(values, val.Field(i).Interface()) | ||
} | ||
rows.AddRow(values...) | ||
} | ||
return rows, nil | ||
} | ||
|
||
// NewRows allows Rows to be created from a | ||
// sql driver.Value slice or from the CSV string and | ||
// to be used as sql driver.Rows. | ||
|
Uh oh!
There was an error while loading. Please reload this page.