File tree Expand file tree Collapse file tree 4 files changed +126
-0
lines changed Expand file tree Collapse file tree 4 files changed +126
-0
lines changed Original file line number Diff line number Diff line change @@ -37,5 +37,6 @@ require (
3737 golang.org/x/sys v0.0.0-20191220220014-0732a990476f
3838 golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
3939 google.golang.org/grpc v1.26.0 // indirect
40+ gopkg.in/go-playground/assert.v1 v1.2.1
4041 sigs.k8s.io/yaml v1.1.0 // indirect
4142)
Original file line number Diff line number Diff line change @@ -139,6 +139,7 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
139139github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E =
140140github.com/soheilhy/cmux v0.1.4 /go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM =
141141github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
142+ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A =
142143github.com/stretchr/objx v0.1.1 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
143144github.com/stretchr/testify v1.2.2 /go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs =
144145github.com/stretchr/testify v1.3.0 /go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI =
@@ -240,6 +241,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
240241gopkg.in/errgo.v2 v2.1.0 /go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI =
241242gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4 =
242243gopkg.in/fsnotify.v1 v1.4.7 /go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys =
244+ gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM =
245+ gopkg.in/go-playground/assert.v1 v1.2.1 /go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE =
243246gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ =
244247gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 /go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw =
245248gopkg.in/yaml.v2 v2.2.1 /go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI =
Original file line number Diff line number Diff line change 1+ package array
2+
3+ import (
4+ "database/sql/driver"
5+ "encoding/json"
6+ "fmt"
7+ "strings"
8+ )
9+
10+ var (
11+ // Separator 序列化时的分隔符
12+ Separator = ";"
13+ // StartEndMark 开始和结尾标志
14+ StartEndMark = ";"
15+ )
16+
17+ // NewStringArray todo
18+ func NewStringArray (items []string ) * StringArray {
19+ return & StringArray {
20+ items : items ,
21+ }
22+ }
23+
24+ // StringArray todo
25+ type StringArray struct {
26+ items []string
27+ }
28+
29+ func (t * StringArray ) add (item string ) {
30+ if item != "" {
31+ t .items = append (t .items , item )
32+ }
33+ }
34+
35+ // Items 列表
36+ func (t StringArray ) Items () []string {
37+ return t .items
38+ }
39+
40+ // Length 长度
41+ func (t StringArray ) Length () int {
42+ return len (t .items )
43+ }
44+
45+ func (t StringArray ) String () string {
46+ if t .Length () == 0 {
47+ return ""
48+ }
49+
50+ return StartEndMark + strings .Join (t .items , Separator ) + StartEndMark
51+ }
52+
53+ // Scan 实现sql的反序列化
54+ func (t * StringArray ) Scan (value interface {}) error {
55+ if value == nil {
56+ return nil
57+ }
58+
59+ switch data := value .(type ) {
60+ case []byte :
61+ raw := string (data )
62+ if raw == "" {
63+ t .items = []string {}
64+ return nil
65+ }
66+ for _ , item := range strings .Split (raw , ";" ) {
67+ t .add (item )
68+ }
69+ return nil
70+ default :
71+ return fmt .Errorf ("unsupport type: %s" , data )
72+ }
73+ }
74+
75+ // Value 实现sql的序列化
76+ func (t StringArray ) Value () (driver.Value , error ) {
77+ return t .String (), nil
78+ }
79+
80+ // MarshalJSON todo
81+ func (t StringArray ) MarshalJSON () ([]byte , error ) {
82+ return json .Marshal (t .items )
83+ }
84+
85+ // UnmarshalJSON todo
86+ func (t * StringArray ) UnmarshalJSON (b []byte ) error {
87+ return json .Unmarshal (b , & t .items )
88+ }
Original file line number Diff line number Diff line change 1+ package array_test
2+
3+ import (
4+ "testing"
5+
6+ "github.com/infraboard/mcube/types/array"
7+ "github.com/stretchr/testify/assert"
8+ )
9+
10+ func TestStringArrayString (t * testing.T ) {
11+ should := assert .New (t )
12+ data := array .NewStringArray ([]string {"1" , "2" , "3" })
13+ should .Equal (data .String (), ";1;2;3;" )
14+ }
15+
16+ func TestStringArrayValue (t * testing.T ) {
17+ should := assert .New (t )
18+ data := array .NewStringArray ([]string {"1" , "2" , "3" })
19+ v , err := data .Value ()
20+ if should .NoError (err ) {
21+ strv , ok := v .(string )
22+ if should .True (ok ) {
23+ should .Equal (strv , ";1;2;3;" )
24+ }
25+ }
26+ }
27+
28+ func TestStringArrayScan (t * testing.T ) {
29+ should := assert .New (t )
30+ data := array .NewStringArray ([]string {})
31+ if should .NoError (data .Scan ([]byte (";1;2;3;" ))) {
32+ should .Equal (data .Items (), []string {"1" , "2" , "3" })
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments