Skip to content

Commit 369b5d1

Browse files
authored
chore: Add generics to typescript generator (coder#4664)
* feat: Support generating generics in interfaces * Switch struct to a template * Support generics in apitypings
1 parent d0b1c36 commit 369b5d1

File tree

9 files changed

+598
-91
lines changed

9 files changed

+598
-91
lines changed

coderd/util/slice/slice.go

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ func Overlap[T comparable](a []T, b []T) bool {
2222
})
2323
}
2424

25+
// Unique returns a new slice with all duplicate elements removed.
26+
// This is a slow function on large lists.
27+
// TODO: Sort elements and implement a faster search algorithm if we
28+
// really start to use this.
29+
func Unique[T comparable](a []T) []T {
30+
cpy := make([]T, 0, len(a))
31+
for _, v := range a {
32+
v := v
33+
if !Contains(cpy, v) {
34+
cpy = append(cpy, v)
35+
}
36+
}
37+
return cpy
38+
}
39+
2540
func OverlapCompare[T any](a []T, b []T, equal func(a, b T) bool) bool {
2641
// For each element in b, if at least 1 is contained in 'a',
2742
// return true.

coderd/util/slice/slice_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ import (
99
"github.com/coder/coder/coderd/util/slice"
1010
)
1111

12+
func TestUnique(t *testing.T) {
13+
t.Parallel()
14+
15+
require.ElementsMatch(t,
16+
[]int{1, 2, 3, 4, 5},
17+
slice.Unique([]int{
18+
1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
19+
}))
20+
21+
require.ElementsMatch(t,
22+
[]string{"a"},
23+
slice.Unique([]string{
24+
"a", "a", "a",
25+
}))
26+
}
27+
1228
func TestContains(t *testing.T) {
1329
t.Parallel()
1430

0 commit comments

Comments
 (0)