diff options
| author | therealmitchconnors <[email protected]> | 2019-09-05 11:27:43 -0700 |
|---|---|---|
| committer | therealmitchconnors <[email protected]> | 2019-09-05 11:27:43 -0700 |
| commit | b22fc706c39eab8ef626dad18f21609425cc8714 (patch) | |
| tree | 324d2c8c6d112bf4f9e44db15c760831cfb9347d /int32_slice.go | |
| parent | c6c0f0fd30b9b96d1c9a1cbf90799706bd501458 (diff) | |
Expand SliceValue support to all slice and array types.
Diffstat (limited to 'int32_slice.go')
| -rw-r--r-- | int32_slice.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/int32_slice.go b/int32_slice.go index 6ccedcc..ff128ff 100644 --- a/int32_slice.go +++ b/int32_slice.go @@ -53,6 +53,48 @@ func (s *int32SliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *int32SliceValue) fromString(val string) (int32, error) { + t64, err := strconv.ParseInt(val, 0, 32) + if err != nil { + return 0, err + } + return int32(t64), nil +} + +func (s *int32SliceValue) toString(val int32) string { + return fmt.Sprintf("%d", val) +} + +func (s *int32SliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *int32SliceValue) Replace(val []string) error { + out := make([]int32, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *int32SliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func int32SliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry |
