Skip to content

Commit 821c244

Browse files
authored
Add generic type cast for ids and strings (#478)
1 parent 0708b5a commit 821c244

File tree

1 file changed

+13
-57
lines changed

1 file changed

+13
-57
lines changed

livekit/types.go

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,36 @@
1414

1515
package livekit
1616

17-
// ----------------------------------------------------------------
18-
1917
type TrackID string
20-
21-
func StringsAsTrackIDs(trackIDs []string) []TrackID {
22-
asTrackID := make([]TrackID, 0, len(trackIDs))
23-
for _, trackID := range trackIDs {
24-
asTrackID = append(asTrackID, TrackID(trackID))
25-
}
26-
27-
return asTrackID
28-
}
29-
30-
// ----------------------------------------------------------------
31-
3218
type ParticipantID string
33-
34-
func ParticipantIDsAsStrings(ids []ParticipantID) []string {
35-
strs := make([]string, 0, len(ids))
36-
for _, id := range ids {
37-
strs = append(strs, string(id))
38-
}
39-
return strs
40-
}
41-
42-
// ----------------------------------------------------------------
43-
4419
type ParticipantIdentity string
4520
type ParticipantName string
46-
4721
type RoomID string
48-
49-
// ----------------------------------------------------------------
50-
5122
type RoomName string
52-
53-
func RoomNamesAsStrings(roomNames []RoomName) []string {
54-
asString := make([]string, 0, len(roomNames))
55-
for _, roomName := range roomNames {
56-
asString = append(asString, string(roomName))
57-
}
58-
59-
return asString
60-
}
61-
62-
func StringsAsRoomNames(roomNames []string) []RoomName {
63-
asRoomName := make([]RoomName, 0, len(roomNames))
64-
for _, roomName := range roomNames {
65-
asRoomName = append(asRoomName, RoomName(roomName))
66-
}
67-
68-
return asRoomName
69-
}
70-
71-
// ----------------------------------------------------------------
72-
7323
type ConnectionID string
74-
75-
// ----------------------------------------------------------------
76-
7724
type NodeID string
25+
type ParticipantKey string
26+
27+
type stringTypes interface {
28+
ParticipantID | RoomID | TrackID | ParticipantIdentity | ParticipantName | RoomName | ConnectionID | NodeID | ParticipantKey
29+
}
7830

79-
func NodeIDsAsStrings(ids []NodeID) []string {
31+
func IDsAsStrings[T stringTypes](ids []T) []string {
8032
strs := make([]string, 0, len(ids))
8133
for _, id := range ids {
8234
strs = append(strs, string(id))
8335
}
8436
return strs
8537
}
8638

87-
// ----------------------------------------------------------------
88-
type ParticipantKey string
39+
func StringsAsIDs[T stringTypes](ids []string) []T {
40+
asID := make([]T, 0, len(ids))
41+
for _, id := range ids {
42+
asID = append(asID, T(id))
43+
}
8944

90-
// ----------------------------------------------------------------
45+
return asID
46+
}
9147

9248
type Guid interface {
9349
TrackID | ParticipantID | RoomID

0 commit comments

Comments
 (0)