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 /ip_slice.go | |
| parent | c6c0f0fd30b9b96d1c9a1cbf90799706bd501458 (diff) | |
Expand SliceValue support to all slice and array types.
Diffstat (limited to 'ip_slice.go')
| -rw-r--r-- | ip_slice.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/ip_slice.go b/ip_slice.go index 6ad2e92..775faae 100644 --- a/ip_slice.go +++ b/ip_slice.go @@ -72,6 +72,44 @@ func (s *ipSliceValue) String() string { return "[" + out + "]" } +func (s *ipSliceValue) fromString(val string) (net.IP, error) { + return net.ParseIP(strings.TrimSpace(val)), nil +} + +func (s *ipSliceValue) toString(val net.IP) string { + return val.String() +} + +func (s *ipSliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *ipSliceValue) Replace(val []string) error { + out := make([]net.IP, 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 *ipSliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func ipSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry |
