This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
/
app_version.go
130 lines (115 loc) · 3.7 KB
/
app_version.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2017 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package models
import (
"strings"
"time"
"github.com/Masterminds/semver"
"openpitrix.io/openpitrix/pkg/constants"
"openpitrix.io/openpitrix/pkg/db"
"openpitrix.io/openpitrix/pkg/pb"
"openpitrix.io/openpitrix/pkg/sender"
"openpitrix.io/openpitrix/pkg/util/idutil"
"openpitrix.io/openpitrix/pkg/util/pbutil"
)
func NewAppVersionId() string {
return idutil.GetUuid("appv-")
}
type AppVersion struct {
VersionId string
Active bool
AppId string
Owner string
OwnerPath sender.OwnerPath
Name string
Description string
PackageName string
Home string
Icon string
Screenshots string
Maintainers string
Keywords string
Sources string
Readme string
Status string
ReviewId string
Message string
Type string
Sequence uint32
CreateTime time.Time
StatusTime time.Time
UpdateTime *time.Time
}
var AppVersionColumns = db.GetColumnsFromStruct(&AppVersion{})
func (v AppVersion) GetSemver() string {
return strings.Split(v.Name, " ")[0]
}
type AppVersions []*AppVersion
// Len returns the length.
func (c AppVersions) Len() int { return len(c) }
// Swap swaps the position of two items in the versions slice.
func (c AppVersions) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
// Less returns true if the version of entry a is less than the version of entry b.
func (c AppVersions) Less(a, b int) bool {
// Failed parse pushes to the back.
aVersion := c[a]
bVersion := c[b]
i, err := semver.NewVersion(aVersion.GetSemver())
if err != nil {
return true
}
j, err := semver.NewVersion(bVersion.GetSemver())
if err != nil {
return false
}
if i.Equal(j) {
return aVersion.CreateTime.Before(bVersion.CreateTime)
}
return i.LessThan(j)
}
func NewAppVersion(appId, name, description string, ownerPath sender.OwnerPath) *AppVersion {
return &AppVersion{
VersionId: NewAppVersionId(),
Active: false,
AppId: appId,
Name: name,
Owner: ownerPath.Owner(),
OwnerPath: ownerPath,
Description: description,
Status: constants.StatusDraft,
CreateTime: time.Now(),
StatusTime: time.Now(),
}
}
func AppVersionToPb(appVersion *AppVersion) *pb.AppVersion {
if appVersion == nil {
return nil
}
pbAppVersion := pb.AppVersion{}
pbAppVersion.VersionId = pbutil.ToProtoString(appVersion.VersionId)
pbAppVersion.Active = pbutil.ToProtoBool(appVersion.Active)
pbAppVersion.AppId = pbutil.ToProtoString(appVersion.AppId)
pbAppVersion.Name = pbutil.ToProtoString(appVersion.Name)
pbAppVersion.Description = pbutil.ToProtoString(appVersion.Description)
pbAppVersion.Status = pbutil.ToProtoString(appVersion.Status)
pbAppVersion.PackageName = pbutil.ToProtoString(appVersion.PackageName)
pbAppVersion.OwnerPath = appVersion.OwnerPath.ToProtoString()
pbAppVersion.Owner = pbutil.ToProtoString(appVersion.Owner)
pbAppVersion.CreateTime = pbutil.ToProtoTimestamp(appVersion.CreateTime)
pbAppVersion.StatusTime = pbutil.ToProtoTimestamp(appVersion.StatusTime)
pbAppVersion.Sequence = pbutil.ToProtoUInt32(appVersion.Sequence)
pbAppVersion.Message = pbutil.ToProtoString(appVersion.Message)
pbAppVersion.Type = pbutil.ToProtoString(appVersion.Type)
pbAppVersion.ReviewId = pbutil.ToProtoString(appVersion.ReviewId)
if appVersion.UpdateTime != nil {
pbAppVersion.UpdateTime = pbutil.ToProtoTimestamp(*appVersion.UpdateTime)
}
return &pbAppVersion
}
func AppVersionsToPbs(appVersions []*AppVersion) (pbAppVersions []*pb.AppVersion) {
for _, appVersion := range appVersions {
pbAppVersions = append(pbAppVersions, AppVersionToPb(appVersion))
}
return
}