Skip to content

Commit

Permalink
Add generic type cast for ids and strings (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnderrauber authored Sep 20, 2023
1 parent 0708b5a commit 821c244
Showing 1 changed file with 13 additions and 57 deletions.
70 changes: 13 additions & 57 deletions livekit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,80 +14,36 @@

package livekit

// ----------------------------------------------------------------

type TrackID string

func StringsAsTrackIDs(trackIDs []string) []TrackID {
asTrackID := make([]TrackID, 0, len(trackIDs))
for _, trackID := range trackIDs {
asTrackID = append(asTrackID, TrackID(trackID))
}

return asTrackID
}

// ----------------------------------------------------------------

type ParticipantID string

func ParticipantIDsAsStrings(ids []ParticipantID) []string {
strs := make([]string, 0, len(ids))
for _, id := range ids {
strs = append(strs, string(id))
}
return strs
}

// ----------------------------------------------------------------

type ParticipantIdentity string
type ParticipantName string

type RoomID string

// ----------------------------------------------------------------

type RoomName string

func RoomNamesAsStrings(roomNames []RoomName) []string {
asString := make([]string, 0, len(roomNames))
for _, roomName := range roomNames {
asString = append(asString, string(roomName))
}

return asString
}

func StringsAsRoomNames(roomNames []string) []RoomName {
asRoomName := make([]RoomName, 0, len(roomNames))
for _, roomName := range roomNames {
asRoomName = append(asRoomName, RoomName(roomName))
}

return asRoomName
}

// ----------------------------------------------------------------

type ConnectionID string

// ----------------------------------------------------------------

type NodeID string
type ParticipantKey string

type stringTypes interface {
ParticipantID | RoomID | TrackID | ParticipantIdentity | ParticipantName | RoomName | ConnectionID | NodeID | ParticipantKey
}

func NodeIDsAsStrings(ids []NodeID) []string {
func IDsAsStrings[T stringTypes](ids []T) []string {
strs := make([]string, 0, len(ids))
for _, id := range ids {
strs = append(strs, string(id))
}
return strs
}

// ----------------------------------------------------------------
type ParticipantKey string
func StringsAsIDs[T stringTypes](ids []string) []T {
asID := make([]T, 0, len(ids))
for _, id := range ids {
asID = append(asID, T(id))
}

// ----------------------------------------------------------------
return asID
}

type Guid interface {
TrackID | ParticipantID | RoomID
Expand Down

0 comments on commit 821c244

Please sign in to comment.