Beispiel #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")
}
Beispiel #2
0
Datei: scan.go Projekt: 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
}
Beispiel #3
0
Datei: table.go Projekt: 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
}