Skip to content

Commit

Permalink
support tomap interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed May 17, 2023
1 parent d6c3719 commit 10fe009
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions variable/interfaces/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Constraint interface {
String() string
Validate() error
DescriptionStr() *string
ToMap() map[string]any
}
7 changes: 7 additions & 0 deletions variable/types/base_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ type BaseConstraint struct {
func (b *BaseConstraint) DescriptionStr() *string {
return &b.Description
}

func (b *BaseConstraint) ToMap() map[string]any {
m := make(map[string]any)
m["default"] = b.Default
m["description"] = b.Description
return m
}
11 changes: 11 additions & 0 deletions variable/types/float_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ func (i *FloatConstraint) Validate() error {
return nil
}

func (i *FloatConstraint) ToMap() map[string]any {
m := i.BaseConstraint.ToMap()
if i.Min != nil {
m["min"] = *i.Min
}
if i.Max != nil {
m["max"] = *i.Max
}
return m
}

var _ interfaces.Constraint = &FloatConstraint{}
11 changes: 11 additions & 0 deletions variable/types/int_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ func (i *IntConstraint) Validate() error {
return nil
}

func (i *IntConstraint) ToMap() map[string]any {
m := i.BaseConstraint.ToMap()
if i.Min != nil {
m["min"] = *i.Min
}
if i.Max != nil {
m["max"] = *i.Max
}
return m
}

var _ interfaces.Constraint = &IntConstraint{}
5 changes: 5 additions & 0 deletions variable/types/nil_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ func (c *NilConstraint) DescriptionStr() *string {
return nil
}

func (c *NilConstraint) ToMap() map[string]any {
m := make(map[string]any)
return m
}

var _ interfaces.Constraint = &NilConstraint{}
11 changes: 11 additions & 0 deletions variable/types/string_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ func (s *StringConstraint) Validate() error {
return nil
}

func (s *StringConstraint) ToMap() map[string]any {
m := s.BaseConstraint.ToMap()
if s.MinLength != nil {
m["minLen"] = *s.MinLength
}
if s.MaxLength != nil {
m["maxLen"] = *s.MaxLength
}
return m
}

var _ interfaces.Constraint = &StringConstraint{}

0 comments on commit 10fe009

Please sign in to comment.