Skip to content

Commit d3914f0

Browse files
authored
Merge pull request #63 from shipengqi/feat/dependson
fix(list): remove unused list options
2 parents 4de8b0f + cecbfdf commit d3914f0

File tree

11 files changed

+70
-45
lines changed

11 files changed

+70
-45
lines changed

internal/errors/missing.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package errors
2+
3+
import "fmt"
4+
5+
type MissingErr struct {
6+
name string
7+
field string
8+
}
9+
10+
func (e MissingErr) Error() string {
11+
if e.name == "" {
12+
return fmt.Sprintf("missing required field `%s`", e.field)
13+
}
14+
return fmt.Sprintf("'%s' missing required field: %s", e.name, e.field)
15+
}
16+
17+
func NewMissingErr(field string, name ...string) error {
18+
err := MissingErr{field: field}
19+
if len(name) > 0 {
20+
err.name = name[0]
21+
}
22+
return err
23+
}

internal/parameter/integer/int.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (p Param) Render() huh.Field {
2626

2727
param.Value(&p.DefaultValue)
2828

29-
var group []validators.Validator
29+
var group []validators.Validator[string]
3030
if p.Required {
3131
group = append(group, validators.Required(p.Name, true))
3232
}

internal/parameter/list/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Param struct {
1919
func (p Param) Validate() []error {
2020
errs := p.Parameter.Validate()
2121
if len(p.Options) < 1 {
22-
errs = append(errs, errors.NewRequiredErr("parameter.options"))
22+
errs = append(errs, errors.NewMissingErr("options", p.Name))
2323
}
2424
return errs
2525
}
@@ -34,7 +34,7 @@ func (p Param) Render() huh.Field {
3434

3535
param.Value(&p.DefaultValue)
3636

37-
var group []validators.Validator
37+
var group []validators.Validator[string]
3838
if p.Required {
3939
group = append(group, validators.Required(p.Name, false))
4040
}

internal/parameter/multilist/list.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ type Param struct {
1414
Options []huh.Option[string] `yaml:"options" json:"options" mapstructure:"options"`
1515
DefaultValue []string `yaml:"default_value" json:"default_value" mapstructure:"default_value"`
1616
Required bool `yaml:"required" json:"required" mapstructure:"required"`
17+
Limit *int `yaml:"limit" json:"limit" mapstructure:"limit"`
1718
}
1819

1920
func (p Param) Validate() []error {
2021
errs := p.Parameter.Validate()
2122
if len(p.Options) < 1 {
22-
errs = append(errs, errors.NewRequiredErr("parameter.options"))
23+
errs = append(errs, errors.NewMissingErr("options", p.Name))
2324
}
2425
return errs
2526
}
@@ -32,10 +33,18 @@ func (p Param) Render() huh.Field {
3233
param.Description(p.Description)
3334
}
3435

36+
if p.Limit != nil {
37+
param.Limit(*p.Limit)
38+
}
3539
param.Value(&p.DefaultValue)
3640

41+
var group []validators.Validator[[]string]
42+
if p.Required {
43+
group = append(group, validators.MultiRequired(p.Name))
44+
}
45+
3746
if p.Required {
38-
param.Validate(validators.MultiRequired(p.Name))
47+
param.Validate(validators.Group(group...))
3948
}
4049

4150
return param

internal/parameter/param.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ func (p Parameter) Render() huh.Field {
3333
func (p Parameter) Validate() []error {
3434
var errs []error
3535
if strutil.IsEmpty(p.Name) {
36-
errs = append(errs, errors.NewRequiredErr("parameter.name"))
36+
errs = append(errs, errors.NewMissingErr("parameter.name"))
3737
}
3838
if strutil.IsEmpty(p.Label) {
39-
errs = append(errs, errors.NewRequiredErr("parameter.label"))
39+
errs = append(errs, errors.NewMissingErr("label", p.Name))
4040
}
4141
if strutil.IsEmpty(p.Type) {
42-
errs = append(errs, errors.NewRequiredErr("parameter.type"))
42+
errs = append(errs, errors.NewMissingErr("type", p.Name))
4343
}
4444
return errs
4545
}

internal/parameter/str/str.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (p Param) RenderInput() *huh.Input {
3535

3636
param.Value(&p.DefaultValue)
3737

38-
var group []validators.Validator
38+
var group []validators.Validator[string]
3939
if p.Required {
4040
group = append(group, validators.Required(p.Name, p.Trim))
4141
}

internal/parameter/text/text.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@ type Param struct {
1616
RegexMessage string `yaml:"regex_message" json:"regex_message" mapstructure:"regex_message"`
1717
MinLength *int `yaml:"min_length" json:"min_length" mapstructure:"min_length"`
1818
MaxLength *int `yaml:"max_length" json:"max_length" mapstructure:"max_length"`
19+
Height *int `yaml:"height" json:"height" mapstructure:"height"`
1920
}
2021

2122
func (p Param) Render() huh.Field {
2223
param := huh.NewText().Key(p.Name).
2324
Title(p.Label)
2425

26+
if p.Height != nil {
27+
param.Lines(*p.Height)
28+
}
29+
2530
if len(p.Description) > 0 {
2631
param.Description(p.Description)
2732
}
2833

2934
param.Value(&p.DefaultValue)
3035

31-
var group []validators.Validator
36+
var group []validators.Validator[string]
3237
if p.Required {
3338
group = append(group, validators.Required(p.Name, false))
3439
}

internal/parameter/validators/required.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package validators
33
import (
44
"fmt"
55
"strings"
6-
7-
"github.com/shipengqi/commitizen/internal/errors"
86
)
97

108
func Required(name string, trim bool) func(string) error {
@@ -18,12 +16,3 @@ func Required(name string, trim bool) func(string) error {
1816
return nil
1917
}
2018
}
21-
22-
func MultiRequired(name string) func([]string) error {
23-
return func(strs []string) error {
24-
if len(strs) == 0 {
25-
return errors.NewRequiredErr(name)
26-
}
27-
return nil
28-
}
29-
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package validators
2+
3+
import "github.com/shipengqi/commitizen/internal/errors"
4+
5+
func MultiRequired(name string) func([]string) error {
6+
return func(vals []string) error {
7+
if len(vals) == 0 {
8+
return errors.NewRequiredErr(name)
9+
}
10+
return nil
11+
}
12+
}

internal/parameter/validators/validators.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package validators
22

3-
type Validator func(string) error
3+
type Validator[T string | []string] func(T) error
44

5-
func Group(validators ...Validator) Validator {
6-
return func(str string) error {
5+
func Group[T string | []string](validators ...Validator[T]) Validator[T] {
6+
return func(t T) error {
77
for _, validator := range validators {
8-
if err := validator(str); err != nil {
8+
if err := validator(t); err != nil {
99
return err
1010
}
1111
}

0 commit comments

Comments
 (0)