@@ -2,9 +2,9 @@ package parameter
22
33import (
44 standarderrs "errors"
5-
65 "github.com/charmbracelet/huh"
76 "github.com/shipengqi/golib/strutil"
7+ "github.com/shipengqi/log"
88
99 "github.com/shipengqi/commitizen/internal/errors"
1010 "github.com/shipengqi/commitizen/internal/helpers"
@@ -34,18 +34,23 @@ func (g *Group) Validate() []error {
3434}
3535
3636func (g * Group ) Render (all map [FieldKey ]huh.Field , fields []huh.Field ) * huh.Group {
37+ group := huh .NewGroup (fields ... )
38+ if len (g .DependsOn .OrConditions ) < 1 && len (g .DependsOn .AndConditions ) < 1 {
39+ return group
40+ }
41+
3742 for _ , v := range g .DependsOn .OrConditions {
3843 v .fields = all
3944 }
4045 for _ , v := range g .DependsOn .AndConditions {
4146 v .fields = all
4247 }
4348
44- group := huh .NewGroup (fields ... )
4549 group .WithHideFunc (func () bool {
4650 orCount := len (g .DependsOn .OrConditions )
4751 andCount := len (g .DependsOn .AndConditions )
4852
53+ log .Debugf ("OrConditions: %d, AndConditions: %d" , orCount , andCount )
4954 if orCount < 1 && andCount < 1 {
5055 return false
5156 }
@@ -64,15 +69,15 @@ func (g *Group) Render(all map[FieldKey]huh.Field, fields []huh.Field) *huh.Grou
6469 andMetCount ++
6570 }
6671 }
67-
72+ log . Debugf ( "orMet: %v, andMetCount: %d" , orMet , andMetCount )
6873 if orCount > 0 && andCount < 1 {
69- return orMet
74+ return ! orMet
7075 }
7176 if orCount < 1 && andCount > 0 {
72- return andCount == andMetCount
77+ return ! ( andCount == andMetCount )
7378 }
7479 if orCount > 0 && andCount > 0 {
75- return orMet && ( andMetCount == orCount )
80+ return ! ( orMet && andMetCount == orCount )
7681 }
7782 return false
7883 })
@@ -81,8 +86,8 @@ func (g *Group) Render(all map[FieldKey]huh.Field, fields []huh.Field) *huh.Grou
8186}
8287
8388type DependsOn struct {
84- AndConditions []Condition `yaml:"and_conditions" json:"and_conditions" mapstructure:"and_conditions"`
85- OrConditions []Condition `yaml:"or_conditions" json:"or_conditions" mapstructure:"or_conditions"`
89+ AndConditions []* Condition `yaml:"and_conditions" json:"and_conditions" mapstructure:"and_conditions"`
90+ OrConditions []* Condition `yaml:"or_conditions" json:"or_conditions" mapstructure:"or_conditions"`
8691}
8792
8893type Condition struct {
@@ -96,7 +101,7 @@ type Condition struct {
96101 ValueNotContains interface {} `yaml:"value_not_contains" json:"value_not_contains" mapstructure:"value_not_contains"`
97102}
98103
99- func (c Condition ) Validate () []error {
104+ func (c * Condition ) Validate () []error {
100105 var errs []error
101106 if strutil .IsEmpty (c .ParameterName ) {
102107 errs = append (errs , errors .NewMissingErr ("parameter_name" , "condition" ))
@@ -108,50 +113,48 @@ func (c Condition) Validate() []error {
108113 return errs
109114}
110115
111- func (c Condition ) Match () bool {
116+ func (c * Condition ) Match () bool {
117+ field , ok := c .fields [GetFiledKey (c .ParameterName )]
118+ if ! ok {
119+ return false
120+ }
121+ val := field .GetValue ()
122+
112123 if c .ValueEmpty != nil {
113- return c .IsEmpty (* c .ValueEmpty )
124+ return c .IsEmpty (* c .ValueEmpty , val )
114125 }
115126 if c .ValueEquals != nil {
116- return c .Equal ()
127+ return c .Equal (val )
117128 }
118129 if c .ValueNotEquals != nil {
119- return c .NotEqual ()
130+ return c .NotEqual (val )
120131 }
121132 if c .ValueContains != nil {
122- return c .Contains ()
133+ return c .Contains (val )
123134 }
124135 if c .ValueNotContains != nil {
125- return c .NotContains ()
136+ return c .NotContains (val )
126137 }
127138 return false
128139}
129140
130- func (c Condition ) Equal () bool {
131- val := c .fields [GetFiledKey (c .ParameterName )]
141+ func (c * Condition ) Equal (val interface {}) bool {
132142 return helpers .Equal (c .ValueEquals , val )
133143}
134144
135- func (c Condition ) NotEqual () bool {
136- val := c .fields [GetFiledKey (c .ParameterName )]
145+ func (c * Condition ) NotEqual (val interface {}) bool {
137146 return helpers .NotEqual (c .ValueNotEquals , val )
138147}
139148
140- func (c Condition ) Contains () bool {
141- val := c .fields [GetFiledKey (c .ParameterName )]
149+ func (c * Condition ) Contains (val interface {}) bool {
142150 return helpers .Contains (val , c .ValueContains )
143151}
144152
145- func (c Condition ) NotContains () bool {
146- val := c .fields [GetFiledKey (c .ParameterName )]
153+ func (c * Condition ) NotContains (val interface {}) bool {
147154 return helpers .NotContains (val , c .ValueNotContains )
148155}
149156
150- func (c Condition ) IsEmpty (empty bool ) bool {
151- val , ok := c .fields [GetFiledKey (c .ParameterName )]
152- if ! ok {
153- return false
154- }
157+ func (c * Condition ) IsEmpty (empty bool , val interface {}) bool {
155158 if empty && helpers .Empty (val ) {
156159 return true
157160 }
0 commit comments