-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiline.go
45 lines (39 loc) · 1013 Bytes
/
multiline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package common
import (
"encoding/json"
"reflect"
)
// MultilineString stores multi-line text as strings or string arrays in the raw JSON.
// TODO(optimization): check if we can scan into [][]byte directly
type MultilineString []string
func (s *MultilineString) UnmarshalJSON(data []byte) error {
var nums interface{}
err := json.Unmarshal(data, &nums)
if err != nil {
return err
}
items := reflect.ValueOf(nums)
switch items.Kind() {
case reflect.String:
*s = append(*s, items.String())
case reflect.Slice:
*s = make(MultilineString, 0, items.Len())
for i := 0; i < items.Len(); i++ {
item := items.Index(i)
switch item.Kind() {
case reflect.String:
*s = append(*s, item.String())
case reflect.Interface:
*s = append(*s, item.Interface().(string))
}
}
}
return nil
}
// Text concatenates all lines in a multiline string into a single byte slice.
func (s MultilineString) Text() (txt []byte) {
for _, line := range s {
txt = append(txt, line...)
}
return
}