示例#1
0
文件: slice_test.go 项目: rjp/crud
func TestIsSlice(t *testing.T) {
	assert.False(t, meta.IsSlice(123))
	assert.False(t, meta.IsSlice("yo"))
	assert.False(t, meta.IsSlice(Foo{}))
	assert.True(t, meta.IsSlice(FooSlice{}))
	assert.True(t, meta.IsSlice(&FooSlice{}))
	assert.True(t, meta.IsSlice(FooPTRSlice{}))
	assert.True(t, meta.IsSlice(&FooPTRSlice{}))
}
示例#2
0
文件: read.go 项目: rjp/crud
func Read(query QueryFn, scanTo interface{}, allparams []interface{}) error {
	sql, params, err := ResolveReadParams(allparams)
	if err != nil {
		return err
	}

	if !meta.IsPointer(scanTo) {
		return errors.New("A pointer has to be passed for scanning rows to.")
	}

	if meta.IsSlice(scanTo) {
		return ReadAll(query, scanTo, sql, params)
	}

	return ReadOne(query, scanTo, sql, params)
}
示例#3
0
文件: table.go 项目: rjp/crud
func NewTable(any interface{}) (*Table, error) {
	if meta.IsSlice(any) {
		any = meta.CreateElement(any).Interface()
	}

	fields, err := GetFieldsOf(any)
	if err != nil {
		return nil, err
	}

	SetDefaultPK(fields)

	name := meta.TypeNameOf(any)

	return &Table{
		Name:    name,
		SQLName: snakecase.SnakeCase(name),
		Fields:  fields,
	}, nil
}