-
Notifications
You must be signed in to change notification settings - Fork 13
/
log.go
158 lines (129 loc) · 3.87 KB
/
log.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-License-Identifier: Apache-2.0
package database
import (
"database/sql"
"errors"
"github.com/go-vela/types/library"
)
var (
// ErrEmptyLogBuildID defines the error type when a
// Log type has an empty BuildID field provided.
ErrEmptyLogBuildID = errors.New("empty log build_id provided")
// ErrEmptyLogRepoID defines the error type when a
// Log type has an empty RepoID field provided.
ErrEmptyLogRepoID = errors.New("empty log repo_id provided")
// ErrEmptyLogStepOrServiceID defines the error type when a
// Log type has an empty StepID or ServiceID field provided.
ErrEmptyLogStepOrServiceID = errors.New("empty log step_id or service_id not provided")
)
// Log is the database representation of a log for a step in a build.
type Log struct {
ID sql.NullInt64 `sql:"id"`
BuildID sql.NullInt64 `sql:"build_id"`
RepoID sql.NullInt64 `sql:"repo_id"`
ServiceID sql.NullInt64 `sql:"service_id"`
StepID sql.NullInt64 `sql:"step_id"`
Data []byte `sql:"data"`
}
// Compress will manipulate the existing data for the
// log entry by compressing that data. This produces
// a significantly smaller amount of data that is
// stored in the system.
func (l *Log) Compress(level int) error {
// compress the database log data
data, err := compress(level, l.Data)
if err != nil {
return err
}
// overwrite database log data with compressed log data
l.Data = data
return nil
}
// Decompress will manipulate the existing data for the
// log entry by decompressing that data. This allows us
// to have a significantly smaller amount of data that
// is stored in the system.
func (l *Log) Decompress() error {
// decompress the database log data
data, err := decompress(l.Data)
if err != nil {
return err
}
// overwrite compressed log data with decompressed log data
l.Data = data
return nil
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Log type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (l *Log) Nullify() *Log {
if l == nil {
return nil
}
// check if the ID field should be false
if l.ID.Int64 == 0 {
l.ID.Valid = false
}
// check if the BuildID field should be false
if l.BuildID.Int64 == 0 {
l.BuildID.Valid = false
}
// check if the RepoID field should be false
if l.RepoID.Int64 == 0 {
l.RepoID.Valid = false
}
// check if the ServiceID field should be false
if l.ServiceID.Int64 == 0 {
l.ServiceID.Valid = false
}
// check if the StepID field should be false
if l.StepID.Int64 == 0 {
l.StepID.Valid = false
}
return l
}
// ToLibrary converts the Log type
// to a library Log type.
func (l *Log) ToLibrary() *library.Log {
log := new(library.Log)
log.SetID(l.ID.Int64)
log.SetBuildID(l.BuildID.Int64)
log.SetRepoID(l.RepoID.Int64)
log.SetServiceID(l.ServiceID.Int64)
log.SetStepID(l.StepID.Int64)
log.SetData(l.Data)
return log
}
// Validate verifies the necessary fields for
// the Log type are populated correctly.
func (l *Log) Validate() error {
// verify the has StepID or ServiceID field populated
if l.StepID.Int64 <= 0 && l.ServiceID.Int64 <= 0 {
return ErrEmptyLogStepOrServiceID
}
// verify the BuildID field is populated
if l.BuildID.Int64 <= 0 {
return ErrEmptyLogBuildID
}
// verify the RepoID field is populated
if l.RepoID.Int64 <= 0 {
return ErrEmptyLogRepoID
}
return nil
}
// LogFromLibrary converts the Log type
// to a library Log type.
func LogFromLibrary(l *library.Log) *Log {
log := &Log{
ID: sql.NullInt64{Int64: l.GetID(), Valid: true},
BuildID: sql.NullInt64{Int64: l.GetBuildID(), Valid: true},
RepoID: sql.NullInt64{Int64: l.GetRepoID(), Valid: true},
ServiceID: sql.NullInt64{Int64: l.GetServiceID(), Valid: true},
StepID: sql.NullInt64{Int64: l.GetStepID(), Valid: true},
Data: l.GetData(),
}
return log.Nullify()
}