diff --git a/variable/interfaces/interface.go b/variable/interfaces/interface.go index d164d8c..a5b6245 100644 --- a/variable/interfaces/interface.go +++ b/variable/interfaces/interface.go @@ -15,4 +15,5 @@ type Constraint interface { String() string Validate() error DescriptionStr() *string + ToMap() map[string]any } diff --git a/variable/types/base_constraint.go b/variable/types/base_constraint.go index db9a43b..c3ef7d4 100644 --- a/variable/types/base_constraint.go +++ b/variable/types/base_constraint.go @@ -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 +} diff --git a/variable/types/float_constraint.go b/variable/types/float_constraint.go index 05e46d0..8fb518b 100644 --- a/variable/types/float_constraint.go +++ b/variable/types/float_constraint.go @@ -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{} diff --git a/variable/types/int_constraint.go b/variable/types/int_constraint.go index e651b70..3517b99 100644 --- a/variable/types/int_constraint.go +++ b/variable/types/int_constraint.go @@ -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{} diff --git a/variable/types/nil_constraint.go b/variable/types/nil_constraint.go index 60de486..86922a2 100644 --- a/variable/types/nil_constraint.go +++ b/variable/types/nil_constraint.go @@ -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{} diff --git a/variable/types/string_constraint.go b/variable/types/string_constraint.go index e5d10c9..bae8b87 100644 --- a/variable/types/string_constraint.go +++ b/variable/types/string_constraint.go @@ -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{}