예제 #1
0
func TestCreateElement(t *testing.T) {
	assert.Equal(t, meta.DirectTypeOf(meta.CreateElement([]Foo{}).Interface()).String(), "meta_test.Foo")
	assert.Equal(t, meta.DirectTypeOf(meta.CreateElement([]*Foo{}).Interface()).String(), "*meta_test.Foo")
	assert.Equal(t, meta.DirectTypeOf(meta.CreateElement(&[]Foo{}).Interface()).String(), "meta_test.Foo")
	assert.Equal(t, meta.DirectTypeOf(meta.CreateElement(&[]*Foo{}).Interface()).String(), "*meta_test.Foo")
	assert.Equal(t, meta.DirectTypeOf(meta.CreateElement(FooPTRSlice{}).Interface()).String(), "*meta_test.Foo")
	assert.Equal(t, meta.DirectTypeOf(meta.CreateElement(&FooPTRSlice{}).Interface()).String(), "*meta_test.Foo")
	assert.Equal(t, meta.DirectTypeOf(meta.CreateElement([]string{}).Interface()).String(), "string")
}
예제 #2
0
파일: scan.go 프로젝트: rjp/crud
func (scan *Scan) All(rows *sql.Rows) error {
	writeTo := meta.Addressable(scan.To)

	for rows.Next() {
		record := meta.CreateElement(scan.To)

		if err := scan.Scan(rows, record); err != nil {
			return err
		}

		meta.Push(writeTo, record)
	}

	return nil
}
예제 #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
}