-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgroup.go
More file actions
179 lines (156 loc) · 5.02 KB
/
Copy pathgroup.go
File metadata and controls
179 lines (156 loc) · 5.02 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package parameter
import (
standarderrs "errors"
"fmt"
"github.com/charmbracelet/huh"
"github.com/shipengqi/golib/strutil"
"github.com/shipengqi/log"
"github.com/shipengqi/commitizen/internal/errorsx"
"github.com/shipengqi/commitizen/internal/helpers"
)
type Group struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
DependsOn DependsOn `yaml:"depends_on" json:"depends_on" mapstructure:"depends_on"`
}
func NewGroup(name string) *Group {
return &Group{Name: name}
}
func (g *Group) Validate() []error {
var errs []error
if strutil.IsEmpty(g.Name) {
errs = append(errs, standarderrs.New("the group missing required field: name"))
}
if !regexName.MatchString(g.Name) {
errs = append(errs, fmt.Errorf("group.name '%s' must match the regex: ^[a-zA-Z0-9-_]{1,62}$", g.Name))
}
for _, v := range g.DependsOn.OrConditions {
errs = append(errs, v.Validate()...)
}
for _, v := range g.DependsOn.AndConditions {
errs = append(errs, v.Validate()...)
}
return errs
}
func (g *Group) Render(all map[FieldKey]huh.Field, fields []huh.Field) *huh.Group {
group := huh.NewGroup(fields...)
if len(g.DependsOn.OrConditions) < 1 && len(g.DependsOn.AndConditions) < 1 {
return group
}
for _, v := range g.DependsOn.OrConditions {
v.fields = all
}
for _, v := range g.DependsOn.AndConditions {
v.fields = all
}
group.WithHideFunc(func() bool {
orCount := len(g.DependsOn.OrConditions)
andCount := len(g.DependsOn.AndConditions)
log.Debugf("OrConditions: %d, AndConditions: %d", orCount, andCount)
if orCount < 1 && andCount < 1 {
return false
}
orMet := false
for _, condition := range g.DependsOn.OrConditions {
if condition.Match() {
orMet = true
break
}
}
andMetCount := 0
for _, condition := range g.DependsOn.AndConditions {
if condition.Match() {
andMetCount++
}
}
log.Debugf("orMet: %v, andMetCount: %d", orMet, andMetCount)
if orCount > 0 && andCount < 1 {
return !orMet
}
if orCount < 1 && andCount > 0 {
return andCount != andMetCount
}
if orCount > 0 && andCount > 0 {
return !orMet || andMetCount != orCount
}
return false
})
return group
}
type DependsOn struct {
AndConditions []*Condition `yaml:"and_conditions" json:"and_conditions" mapstructure:"and_conditions"`
OrConditions []*Condition `yaml:"or_conditions" json:"or_conditions" mapstructure:"or_conditions"`
}
type Condition struct {
fields map[FieldKey]huh.Field
ParameterName string `yaml:"parameter_name" json:"parameter_name" mapstructure:"parameter_name"`
ValueEmpty *bool `yaml:"value_empty" json:"value_empty" mapstructure:"value_empty"`
ValueEquals interface{} `yaml:"value_equals" json:"value_equals" mapstructure:"value_equals"`
ValueNotEquals interface{} `yaml:"value_not_equals" json:"value_not_equals" mapstructure:"value_not_equals"`
ValueContains interface{} `yaml:"value_contains" json:"value_contains" mapstructure:"value_contains"`
ValueNotContains interface{} `yaml:"value_not_contains" json:"value_not_contains" mapstructure:"value_not_contains"`
}
func (c *Condition) Validate() []error {
var errs []error
if strutil.IsEmpty(c.ParameterName) {
errs = append(errs, errorsx.NewMissingErr("parameter_name", "condition"))
}
if c.ValueEmpty == nil && c.ValueEquals == nil && c.ValueNotEquals == nil &&
c.ValueNotContains == nil && c.ValueContains == nil {
errs = append(errs, standarderrs.New("missing a valid condition"))
}
return errs
}
func (c *Condition) Match() bool {
key := GetFiledKey(c.ParameterName)
log.Debugf("math field condition: %s", key)
field, ok := c.fields[key]
if !ok {
log.Debugf("cannot find field condition: %s", key)
return false
}
val := field.GetValue()
if c.ValueEmpty != nil {
return c.IsEmpty(*c.ValueEmpty, val)
}
if c.ValueEquals != nil {
return c.Equal(val)
}
if c.ValueNotEquals != nil {
return c.NotEqual(val)
}
if c.ValueContains != nil {
return c.Contains(val)
}
if c.ValueNotContains != nil {
log.Debugf("value not contains val: %v", val)
return c.NotContains(val)
}
return false
}
func (c *Condition) Equal(val interface{}) bool {
log.Debugf("%v contains match val: %v", c.ValueEquals, val)
return helpers.Equal(c.ValueEquals, val)
}
func (c *Condition) NotEqual(val interface{}) bool {
log.Debugf("%v not equals val: %v", c.ValueNotEquals, val)
return helpers.NotEqual(c.ValueNotEquals, val)
}
func (c *Condition) Contains(val interface{}) bool {
log.Debugf("%v contains val: %v", val, c.ValueContains)
return helpers.Contains(val, c.ValueContains)
}
func (c *Condition) NotContains(val interface{}) bool {
log.Debugf("%v not contains val: %v", val, c.ValueContains)
return helpers.NotContains(val, c.ValueNotContains)
}
func (c *Condition) IsEmpty(empty bool, val interface{}) bool {
if empty && helpers.Empty(val) {
log.Debugf("value is empty: %v", val)
return true
}
if !empty && helpers.NotEmpty(val) {
log.Debugf("value is not empty: %v", val)
return true
}
return false
}