-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.go
More file actions
35 lines (30 loc) · 789 Bytes
/
entity.go
File metadata and controls
35 lines (30 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package pg
import (
"fmt"
"reflect"
)
type Entity interface {
Result
Table() string
OrderBy() string
}
type EntityHandler interface {
Create(entity Entity) error
FindOne(entity Entity, where string, whereParams ...interface{}) (Entity, error)
FindAll(entity Entity, where string, whereParams ...interface{}) ([]Entity, error)
Update(entity Entity) error
Delete(entity Entity) error
}
func NewEntity(e Entity) Entity {
mustBeStructPtr(e)
entityType := reflect.TypeOf(e).Elem()
newEntity := reflect.New(entityType)
return newEntity.Interface().(Entity)
}
func mustBeStructPtr(e interface{}) {
v := reflect.ValueOf(e)
t := v.Type()
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
panic(fmt.Errorf("entity must be pointer to struct; got %T", v))
}
}