From b22fc706c39eab8ef626dad18f21609425cc8714 Mon Sep 17 00:00:00 2001 From: therealmitchconnors Date: Thu, 5 Sep 2019 11:27:43 -0700 Subject: Expand SliceValue support to all slice and array types. --- float64_slice.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'float64_slice.go') diff --git a/float64_slice.go b/float64_slice.go index 45d12b8..85bf307 100644 --- a/float64_slice.go +++ b/float64_slice.go @@ -51,6 +51,44 @@ func (s *float64SliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *float64SliceValue) fromString(val string) (float64, error) { + return strconv.ParseFloat(val, 64) +} + +func (s *float64SliceValue) toString(val float64) string { + return fmt.Sprintf("%f", val) +} + +func (s *float64SliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *float64SliceValue) Replace(val []string) error { + out := make([]float64, 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 *float64SliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func float64SliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry -- cgit v1.2.3