forked from KusionStack/kusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_model.go
116 lines (102 loc) · 2.96 KB
/
stack_model.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package persistence
import (
"context"
"time"
"kusionstack.io/kusion/pkg/domain/constant"
"kusionstack.io/kusion/pkg/domain/entity"
"gorm.io/gorm"
)
// StackModel is a DO used to map the entity to the database.
type StackModel struct {
gorm.Model
Name string `gorm:"index:unique_project,unique"`
// SourceID uint
// Source *SourceModel
ProjectID uint
Project *ProjectModel
// OrganizationID uint
// Organization *OrganizationModel
Description string
Path string `gorm:"index:unique_project,unique"`
DesiredVersion string
Labels MultiString
Owners MultiString
SyncState string
LastSyncTimestamp time.Time
}
// The TableName method returns the name of the database table that the struct is mapped to.
func (m *StackModel) TableName() string {
return "stack"
}
// ToEntity converts the DO to an entity.
func (m *StackModel) ToEntity(ctx context.Context) (*entity.Stack, error) {
if m == nil {
return nil, ErrStackModelNil
}
stackState, err := constant.ParseStackState(m.SyncState)
if err != nil {
return nil, ErrFailedToGetStackState
}
// sourceEntity, err := m.Source.ToEntity()
// if err != nil {
// return nil, ErrFailedToConvertSourceToEntity
// }
// organizationEntity, err := m.Organization.ToEntity()
// if err != nil {
// return nil, ErrFailedToConvertSourceToEntity
// }
// projectEntity, err := m.Project.ToEntityWithSourceAndOrg(sourceEntity, organizationEntity)
projectEntity, err := m.Project.ToEntityWithSourceAndOrg(nil, nil)
if err != nil {
return nil, ErrFailedToConvertProjectToEntity
}
return &entity.Stack{
ID: m.ID,
Name: m.Name,
// Source: sourceEntity,
Project: projectEntity,
// Organization: organizationEntity,
Description: m.Description,
Path: m.Path,
DesiredVersion: m.DesiredVersion,
Labels: []string(m.Labels),
Owners: []string(m.Owners),
SyncState: stackState,
LastSyncTimestamp: m.LastSyncTimestamp,
CreationTimestamp: m.CreatedAt,
UpdateTimestamp: m.UpdatedAt,
}, nil
}
// FromEntity converts an entity to a DO.
func (m *StackModel) FromEntity(e *entity.Stack) error {
if m == nil {
return ErrStackModelNil
}
m.ID = e.ID
m.Name = e.Name
m.Description = e.Description
m.Path = e.Path
m.DesiredVersion = e.DesiredVersion
m.Labels = MultiString(e.Labels)
m.Owners = MultiString(e.Owners)
m.SyncState = string(e.SyncState)
m.LastSyncTimestamp = e.LastSyncTimestamp
m.CreatedAt = e.CreationTimestamp
m.UpdatedAt = e.UpdateTimestamp
// Convert the source to a DO
// if e.Source != nil {
// m.SourceID = e.Source.ID
// m.Source.FromEntity(e.Source)
// }
// Convert the project to a DO
if e.Project != nil {
m.ProjectID = e.Project.ID
m.Project.FromEntity(e.Project)
}
// Convert the org to a DO
// if e.Organization != nil {
// m.OrganizationID = e.Organization.ID
// m.Organization.FromEntity(e.Organization)
// }
return nil
}