diff options
| author | Mitch Connors <[email protected]> | 2019-09-17 09:43:30 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-09-17 09:43:30 -0700 |
| commit | 7b22f689a41a1727c42eb02aaa1492d26402c592 (patch) | |
| tree | ca8ae6c13649d5a76f09cd8c7a4ca5011c870c85 /bool_slice.go | |
| parent | 972238283c0625cf3e881de7699ba8f2524c340a (diff) | |
| parent | 8e39cc44d9bd06ee2d9ef3109c93809072d5e551 (diff) | |
Merge pull request #216 from therealmitchconnors/elegant
Implement SliceValue for better list semantics
Diffstat (limited to 'bool_slice.go')
| -rw-r--r-- | bool_slice.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/bool_slice.go b/bool_slice.go index 5af02f1..3731370 100644 --- a/bool_slice.go +++ b/bool_slice.go @@ -71,6 +71,44 @@ func (s *boolSliceValue) String() string { return "[" + out + "]" } +func (s *boolSliceValue) fromString(val string) (bool, error) { + return strconv.ParseBool(val) +} + +func (s *boolSliceValue) toString(val bool) string { + return strconv.FormatBool(val) +} + +func (s *boolSliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *boolSliceValue) Replace(val []string) error { + out := make([]bool, 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 *boolSliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func boolSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry |
