aboutsummaryrefslogtreecommitdiff
path: root/string_slice.go
diff options
context:
space:
mode:
authorwu8685 <[email protected]>2016-09-06 19:35:35 +0800
committerwu8685 <[email protected]>2016-09-06 19:35:35 +0800
commit191ef6fd817dfda320833b43a97932f049562514 (patch)
tree0b879204d147d343013bfd9df10c26e0d9fdda0b /string_slice.go
parent103ce5cd2042f2fe629c1957abb64ab3e7f50235 (diff)
add a new type StringArray
Diffstat (limited to 'string_slice.go')
-rw-r--r--string_slice.go18
1 files changed, 13 insertions, 5 deletions
diff --git a/string_slice.go b/string_slice.go
index 927a440..51e3c5d 100644
--- a/string_slice.go
+++ b/string_slice.go
@@ -31,6 +31,17 @@ func readAsCSV(val string) ([]string, error) {
return csvReader.Read()
}
+func writeAsCSV(vals []string) (string, error) {
+ b := &bytes.Buffer{}
+ w := csv.NewWriter(b)
+ err := w.Write(vals)
+ if err != nil {
+ return "", err
+ }
+ w.Flush()
+ return strings.TrimSuffix(b.String(), fmt.Sprintln()), nil
+}
+
func (s *stringSliceValue) Set(val string) error {
v, err := readAsCSV(val)
if err != nil {
@@ -50,11 +61,8 @@ func (s *stringSliceValue) Type() string {
}
func (s *stringSliceValue) String() string {
- b := &bytes.Buffer{}
- w := csv.NewWriter(b)
- w.Write(*s.value)
- w.Flush()
- return "[" + strings.TrimSuffix(b.String(), fmt.Sprintln()) + "]"
+ str, _ := writeAsCSV(*s.value)
+ return "[" + str + "]"
}
func stringSliceConv(sval string) (interface{}, error) {