github.com/sandwich-go/boost@v1.3.29/xslice/internal/template2/slice_template.go (about)

     1  package template2
     2  
     3  import "sort"
     4  
     5  type SliceConfig struct {
     6  	Key string
     7  }
     8  
     9  func GetSliceTPLArgs() interface{} {
    10  	var sliceConfigs = make([]SliceConfig, 0)
    11  	var types = []string{
    12  		"int", "int8", "int16", "int32", "int64",
    13  		"uint", "uint8", "uint16", "uint32", "uint64",
    14  		"string",
    15  		"float32", "float64",
    16  	}
    17  	for _, tk := range types {
    18  		sliceConfigs = append(sliceConfigs, SliceConfig{Key: tk})
    19  	}
    20  	sort.Slice(sliceConfigs, func(i, j int) bool {
    21  		return sliceConfigs[i].Key < sliceConfigs[j].Key
    22  	})
    23  	return map[string]interface{}{"SliceConfigs": sliceConfigs}
    24  }
    25  
    26  const SliceTestTPL = `// Code generated by tools. DO NOT EDIT.
    27  package xslice
    28  
    29  import (
    30  	. "github.com/smartystreets/goconvey/convey"
    31  	"testing"
    32  )
    33  
    34  {{ range $sliceConfig := .SliceConfigs }}
    35  {{- $camelCaseKey := $sliceConfig.Key | CamelCase }}
    36  {{- $camelCaseName := print $camelCaseKey "s" }}
    37  func Test{{ $camelCaseName }}(t *testing.T) {
    38  	Convey("{{ $sliceConfig.Key }} slice", t, func() {
    39  		for _, test := range []struct {
    40  			ss 			[]{{ $sliceConfig.Key }}
    41  			s           {{ $sliceConfig.Key }}
    42  			contains    bool
    43  		}{
    44  {{- if eq $sliceConfig.Key "string" }}
    45  			{ss: nil, s: "a"},
    46  			{ss: []string{"abc", "b"}, s: "a", contains: false},
    47  			{ss: []string{"abc", "b"}, s: "abc", contains: true},
    48  {{- else }}
    49  			{ss: nil, s: 1},
    50  			{ss: []{{ $sliceConfig.Key }}{1, 2}, s: 3, contains: false},
    51  			{ss: []{{ $sliceConfig.Key }}{1, 2}, s: 1, contains: true},
    52  {{- end }}
    53  		} {
    54  			So({{ $camelCaseName }}Contain(test.ss, test.s), ShouldEqual, test.contains)
    55  		}
    56  {{- if eq $sliceConfig.Key "string" }}
    57  		for _, test := range []struct {
    58  			ss 		  []{{ $sliceConfig.Key }}
    59  			s         {{ $sliceConfig.Key }}
    60  			contains    bool
    61  		}{
    62  			{ss: nil, s: "a"},
    63  			{ss: []{{ $sliceConfig.Key }}{"abc", "b"}, s: "a", contains: false},
    64  			{ss: []{{ $sliceConfig.Key }}{"abc", "b"}, s: "abc", contains: true},
    65  			{ss: []{{ $sliceConfig.Key }}{"ABC", "b"}, s: "abc", contains: true},
    66  		} {
    67  			So({{ $camelCaseName }}ContainEqualFold(test.ss, test.s), ShouldEqual, test.contains)
    68  		}
    69  {{- end }}
    70  {{- if eq $sliceConfig.Key "string" }}
    71  		dest := {{ $camelCaseName }}SetAdd(nil, "a")
    72  		So(len(dest), ShouldEqual, 1)
    73  		So(len({{ $camelCaseName }}SetAdd(dest, "a")), ShouldEqual, 1)
    74  
    75  		src := []{{ $sliceConfig.Key }}{"1", "2"}
    76  		dest = {{ $camelCaseName }}Walk(src, func(s {{ $sliceConfig.Key }}) ({{ $sliceConfig.Key }}, bool) {
    77  			return s + ",", true
    78  		})
    79  		So(len(src), ShouldEqual, len(dest))
    80  		for i := 0; i {{ "<" | Unescaped }} len(src); i++ {
    81  			So(src[i]+",", ShouldEqual, dest[i])
    82  		}
    83  
    84  		dest = {{ $camelCaseName }}AddSuffix(src, ",")
    85  		So(len(src), ShouldEqual, len(dest))
    86  		for i := 0; i {{ "<" | Unescaped }} len(src); i++ {
    87  			So(src[i]+",", ShouldEqual, dest[i])
    88  		}
    89  
    90  		dest = {{ $camelCaseName }}AddPrefix(src, ",")
    91  		So(len(src), ShouldEqual, len(dest))
    92  		for i := 0; i {{ "<" | Unescaped }} len(src); i++ {
    93  			So(","+src[i], ShouldEqual, dest[i])
    94  		}
    95  {{- else }}
    96  		dest := {{ $camelCaseName }}SetAdd(nil, 1)
    97  		So(len(dest), ShouldEqual, 1)
    98  		So(len({{ $camelCaseName }}SetAdd(dest, 1)), ShouldEqual, 1)
    99  
   100  		src := []{{ $sliceConfig.Key }}{1, 2}
   101  		dest = {{ $camelCaseName }}Walk(src, func(s {{ $sliceConfig.Key }}) ({{ $sliceConfig.Key }}, bool) {
   102  			return s + 1, true
   103  		})
   104  		So(len(src), ShouldEqual, len(dest))
   105  		for i := 0; i {{ "<" | Unescaped }} len(src); i++ {
   106  			So(src[i]+1, ShouldEqual, dest[i])
   107  		}
   108  {{- end }}
   109  		for i := 0; i {{ "<" | Unescaped }} 2; i ++ {
   110  			if i > 0 { 
   111  				tooManyElement = 4 
   112  			}
   113  			for _, test := range []struct {
   114  				src      []{{ $sliceConfig.Key }}
   115  				dest     []{{ $sliceConfig.Key }}
   116  				contains bool
   117  			}{
   118  	{{- if eq $sliceConfig.Key "string" }}
   119  				{src: []{{ $sliceConfig.Key }}{"abc", "b", "b"}, dest: []{{ $sliceConfig.Key }}{"abc", "b"}},
   120  				{src: []{{ $sliceConfig.Key }}{"abc", "abc", "b"}, dest: []{{ $sliceConfig.Key }}{"abc", "b"}},
   121  				{src: []{{ $sliceConfig.Key }}{"abc", "abc", "b", "b", "b"}, dest: []{{ $sliceConfig.Key }}{"abc", "b"}},
   122  	{{- else }}
   123  				{src: []{{ $sliceConfig.Key }}{1, 2, 2}, dest: []{{ $sliceConfig.Key }}{1, 2}},
   124  				{src: []{{ $sliceConfig.Key }}{1, 1, 2}, dest: []{{ $sliceConfig.Key }}{1, 2}},
   125  				{src: []{{ $sliceConfig.Key }}{1, 1, 2, 2, 2}, dest: []{{ $sliceConfig.Key }}{1, 2}},
   126  	{{- end }}
   127  			} {
   128  				So({{ $camelCaseName }}RemoveRepeated(test.src), ShouldResemble, test.dest)
   129  			}
   130  		}
   131  
   132  		for _, test := range []struct {
   133  			src      []{{ $sliceConfig.Key }}
   134  			dest     []{{ $sliceConfig.Key }}
   135  			contains bool
   136  		}{
   137  {{- if eq $sliceConfig.Key "string" }}
   138  			{src: []{{ $sliceConfig.Key }}{"abc", "", "b"}, dest: []{{ $sliceConfig.Key }}{"abc", "b"}},
   139  			{src: []{{ $sliceConfig.Key }}{"abc", "abc", ""}, dest: []{{ $sliceConfig.Key }}{"abc", "abc"}},
   140  			{src: []{{ $sliceConfig.Key }}{"abc", "", "", "b", ""}, dest: []{{ $sliceConfig.Key }}{"abc", "b"}},
   141  {{- else }}
   142  			{src: []{{ $sliceConfig.Key }}{1, 0, 2}, dest: []{{ $sliceConfig.Key }}{1, 2}},
   143  			{src: []{{ $sliceConfig.Key }}{1, 1, 0}, dest: []{{ $sliceConfig.Key }}{1, 1}},
   144  			{src: []{{ $sliceConfig.Key }}{1, 0, 0, 2, 0}, dest: []{{ $sliceConfig.Key }}{1, 2}},
   145  {{- end }}
   146  		} {
   147  			v := {{ $camelCaseName }}RemoveEmpty(test.src)
   148  			So(v, ShouldResemble, test.dest)
   149  			{{ $camelCaseName }}Shuffle(v)
   150  		}
   151  	})
   152  }
   153  {{ end }}
   154  `
   155  
   156  const SliceTPL = `// Code generated by tools. DO NOT EDIT.
   157  package xslice
   158  
   159  import (
   160  	"math/rand"
   161  	"strings"
   162  
   163  	_ "github.com/sandwich-go/boost/xrand"
   164  )
   165  
   166  var (
   167  	_ strings.Reader
   168  	tooManyElement = 1024
   169  )
   170   
   171  {{ range $sliceConfig := .SliceConfigs }}
   172  {{- $camelCaseKey := $sliceConfig.Key | CamelCase }}
   173  {{- $camelCaseName := print $camelCaseKey "s" }}
   174  // {{ $camelCaseName }}Contain s 中是否含有指定元素 v
   175  func {{ $camelCaseName }}Contain(s []{{ $sliceConfig.Key }}, v {{ $sliceConfig.Key }}) bool {
   176  	for _, ele := range s {
   177  		if ele == v {
   178  			return true
   179  		}
   180  	}
   181  	return false
   182  }
   183  
   184  {{- if eq $sliceConfig.Key "string" }}
   185  // {{ $camelCaseName }}ContainEqualFold s 中是否含有指定元素 v,不区分大小写
   186  func {{ $camelCaseName }}ContainEqualFold(s []{{ $sliceConfig.Key }}, v {{ $sliceConfig.Key }}) bool {
   187  	for _, ele := range s {
   188  		if strings.EqualFold(ele, v) {
   189  			return true
   190  		}
   191  	}
   192  	return false
   193  }
   194  {{- end }}
   195  
   196  // {{ $camelCaseName }}SetAdd 如果 s 中不存在给定的元素 v 则添加
   197  func {{ $camelCaseName }}SetAdd(s []{{ $sliceConfig.Key }}, v ...{{ $sliceConfig.Key }}) []{{ $sliceConfig.Key }} {
   198  	for _, ele := range v {
   199  		if !{{ $camelCaseKey }}sContain(s, ele) {
   200  			s = append(s, ele)
   201  		}
   202  	}
   203  	return s
   204  }
   205  
   206  // {{ $camelCaseName }}Walk 遍历 s 将 f 应用到每一个元素,返回更新后的数据
   207  func {{ $camelCaseName }}Walk(s []{{ $sliceConfig.Key }}, f func({{ $sliceConfig.Key }}) ({{ $sliceConfig.Key }}, bool)) []{{ $sliceConfig.Key }} {
   208  	out := make([]{{ $sliceConfig.Key }}, 0, len(s))
   209  	for _, ele := range s {
   210  		if ret, valid := f(ele); valid {
   211  			out = append(out, ret)
   212  		}
   213  	}
   214  	return out
   215  }
   216  
   217  {{- if eq $sliceConfig.Key "string" }}
   218  // {{ $camelCaseName }}AddPrefix 每一个元素添加前缀
   219  func {{ $camelCaseName }}AddPrefix(s []{{ $sliceConfig.Key }}, prefix {{ $sliceConfig.Key }}) []{{ $sliceConfig.Key }} {
   220  	out := make([]{{ $sliceConfig.Key }}, 0, len(s))
   221  	for _, ele := range s {
   222  		out = append(out, prefix+ele)
   223  	}
   224  	return out
   225  }
   226  
   227  // {{ $camelCaseName }}AddSuffix 每一个元素添加后缀
   228  func {{ $camelCaseName }}AddSuffix(s []{{ $sliceConfig.Key }}, suffix {{ $sliceConfig.Key }}) []{{ $sliceConfig.Key }} {
   229  	out := make([]{{ $sliceConfig.Key }}, 0, len(s))
   230  	for _, ele := range s {
   231  		out = append(out, ele+suffix)
   232  	}
   233  	return out
   234  }
   235  {{- end }}
   236  
   237  // {{ $camelCaseName }}RemoveRepeated 移除重复元素
   238  func {{ $camelCaseName }}RemoveRepeated(s []{{ $sliceConfig.Key }}) []{{ $sliceConfig.Key }} {
   239  	if len(s) == 0 {
   240  		return s
   241  	}
   242  	if len(s) {{ "<" | Unescaped }} tooManyElement {
   243  		return {{ $sliceConfig.Key }}RemoveRepeatByLoop(s)
   244  	} else {
   245  		return {{ $sliceConfig.Key }}RemoveRepeatByMap(s)
   246  	}
   247  }
   248  
   249  // {{ $camelCaseName }}RemoveEmpty 移除空元素
   250  func {{ $camelCaseName }}RemoveEmpty(s []{{ $sliceConfig.Key }}) []{{ $sliceConfig.Key }} {
   251  	out := make([]{{ $sliceConfig.Key }}, 0, len(s))
   252  	for _, ele := range s {
   253  {{- if eq $sliceConfig.Key "string" }}
   254  		if len(ele) > 0 {
   255  {{- else }}
   256  		if ele != 0 {
   257  {{- end }}
   258  			out = append(out, ele)
   259  		}
   260  	}
   261  	return out
   262  }
   263  
   264  func {{ $sliceConfig.Key }}RemoveRepeatByMap(s []{{ $sliceConfig.Key }}) []{{ $sliceConfig.Key }} {
   265  	out := make([]{{ $sliceConfig.Key }}, 0, len(s))
   266  	tmp := make(map[{{ $sliceConfig.Key }}]struct{})
   267  	for _, ele := range s {
   268  		l := len(tmp)
   269  		tmp[ele] = struct{}{}
   270  		if len(tmp) != l {
   271  			out = append(out, ele)
   272  		}
   273  	}
   274  	return out
   275  }
   276  
   277  func {{ $sliceConfig.Key }}RemoveRepeatByLoop(s []{{ $sliceConfig.Key }}) []{{ $sliceConfig.Key }} {
   278  	out := make([]{{ $sliceConfig.Key }}, 0, len(s))
   279  	flag := true
   280  	for i := range s {
   281  		flag = true
   282  		for j := range out {
   283  			if s[i] == out[j] {
   284  				flag = false 
   285  				break
   286  			}
   287  		}
   288  		if flag {
   289  			out = append(out, s[i])
   290  		}
   291  	}
   292  	return out
   293  }
   294  
   295  // {{ $camelCaseName }}Shuffle 数组打乱
   296  func {{ $camelCaseName }}Shuffle(s []{{ $sliceConfig.Key }}) {
   297  	for i := range s {
   298  		j := rand.Intn(i + 1)
   299  		s[i], s[j] = s[j], s[i]
   300  	}
   301  }
   302  {{ end }}
   303  `