generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.go
100 lines (89 loc) · 2.53 KB
/
chart.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
package v2models
import (
"fmt"
"github.com/broadinstitute/sherlock/internal/utils"
"strconv"
"github.com/broadinstitute/sherlock/internal/errors"
"gorm.io/gorm"
)
type Chart struct {
gorm.Model
CiIdentifier *CiIdentifier `gorm:"polymorphic:Resource; polymorphicValue:chart"`
Name string `gorm:"not null; default:null; unique"`
// Mutable
ChartRepo *string `gorm:"not null; default:null"`
AppImageGitRepo *string
AppImageGitMainBranch *string
ChartExposesEndpoint *bool
DefaultSubdomain *string
DefaultProtocol *string
DefaultPort *uint
LegacyConfigsEnabled *bool
Description *string
PlaybookURL *string
}
func (c Chart) TableName() string {
return "v2_charts"
}
func (c Chart) getID() uint {
return c.ID
}
func (c Chart) GetCiIdentifier() *CiIdentifier {
if c.CiIdentifier != nil {
return c.CiIdentifier
} else {
return &CiIdentifier{ResourceType: "chart", ResourceID: c.ID}
}
}
var chartStore *internalModelStore[Chart]
func init() {
chartStore = &internalModelStore[Chart]{
selectorToQueryModel: chartSelectorToQuery,
modelToSelectors: chartToSelectors,
validateModel: validateChart,
}
}
func chartSelectorToQuery(_ *gorm.DB, selector string) (Chart, error) {
if len(selector) == 0 {
return Chart{}, fmt.Errorf("(%s) chart selector cannot be empty", errors.BadRequest)
}
var query Chart
if utils.IsNumeric(selector) { // ID
id, err := strconv.Atoi(selector)
if err != nil {
return Chart{}, fmt.Errorf("(%s) string to int conversion error of '%s': %v", errors.BadRequest, selector, err)
}
query.ID = uint(id)
return query, nil
} else if utils.IsAlphaNumericWithHyphens(selector) &&
utils.IsStartingWithLetter(selector) &&
utils.IsEndingWithAlphaNumeric(selector) { // Name
query.Name = selector
return query, nil
}
return Chart{}, fmt.Errorf("(%s) invalid chart selector '%s'", errors.BadRequest, selector)
}
func chartToSelectors(chart *Chart) []string {
var selectors []string
if chart != nil {
if chart.Name != "" {
selectors = append(selectors, chart.Name)
}
if chart.ID != 0 {
selectors = append(selectors, fmt.Sprintf("%d", chart.ID))
}
}
return selectors
}
func validateChart(chart *Chart) error {
if chart == nil {
return fmt.Errorf("the model passed was nil")
}
if chart.Name == "" {
return fmt.Errorf("a %T must have a non-empty name", chart)
}
if chart.ChartRepo == nil || *chart.ChartRepo == "" {
return fmt.Errorf("a %T must have a non-empty chart repo", chart)
}
return nil
}