-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
57 lines (51 loc) · 1.64 KB
/
entity.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ens
import (
"github.com/things-go/ens/proto"
"github.com/things-go/ens/rapier"
"github.com/things-go/ens/sqlx"
"github.com/things-go/ens/utils"
)
// EntityDescriptor Each table corresponds to an EntityDescriptor
type EntityDescriptor struct {
Name string // entity name
Comment string // entity comment
Table TableDef // entity table define
Fields []*FieldDescriptor // field information
Indexes []*IndexDescriptor // index information
ForeignKeys []*ForeignKeyDescriptor // foreign key information
}
func (s *EntityDescriptor) IntoProto() *proto.Message {
fields := make([]*proto.MessageField, 0, len(s.Fields))
for _, field := range s.Fields {
fields = append(fields, field.IntoProto())
}
return &proto.Message{
Name: s.Name,
TableName: s.Name,
Comment: s.Comment,
Fields: fields,
}
}
func (s *EntityDescriptor) IntoRapier() *rapier.Struct {
fields := make([]*rapier.StructField, 0, len(s.Fields))
for _, field := range s.Fields {
fields = append(fields, field.IntoRapier())
}
return &rapier.Struct{
GoName: utils.PascalCase(s.Name),
TableName: s.Name,
Comment: s.Comment,
Fields: fields,
}
}
func (s *EntityDescriptor) IntoSQL() *sqlx.Table {
return &sqlx.Table{
Name: s.Name,
Sql: s.Table.Definition(),
Comment: s.Comment,
}
}
type EntityDescriptorSlice []*EntityDescriptor
func (t EntityDescriptorSlice) Len() int { return len(t) }
func (t EntityDescriptorSlice) Less(i, j int) bool { return t[i].Name < t[j].Name }
func (t EntityDescriptorSlice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }