Rewrite long if-else chains as switch statements.

This commit is contained in:
Joachim Bauch 2025-12-04 11:06:37 +01:00
commit 9d07a852a9
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
5 changed files with 36 additions and 28 deletions

View file

@ -50,13 +50,14 @@ func formatWithRemainder(value uint64, divisor uint64, format string) string {
// String returns the formatted bandwidth.
func (b Bandwidth) String() string {
if b >= Gigabit {
switch {
case b >= Gigabit:
return formatWithRemainder(b.Bits(), Gigabit.Bits(), "Gbps")
} else if b >= Megabit {
case b >= Megabit:
return formatWithRemainder(b.Bits(), Megabit.Bits(), "Mbps")
} else if b >= Kilobit {
case b >= Kilobit:
return formatWithRemainder(b.Bits(), Kilobit.Bits(), "Kbps")
} else {
default:
return fmt.Sprintf("%d bps", b)
}
}

View file

@ -329,13 +329,14 @@ type EventProxyServerBandwidth struct {
}
func (b *EventProxyServerBandwidth) String() string {
if b.Incoming != nil && b.Outgoing != nil {
switch {
case b.Incoming != nil && b.Outgoing != nil:
return fmt.Sprintf("bandwidth: incoming=%.3f%%, outgoing=%.3f%%", *b.Incoming, *b.Outgoing)
} else if b.Incoming != nil {
case b.Incoming != nil:
return fmt.Sprintf("bandwidth: incoming=%.3f%%, outgoing=unlimited", *b.Incoming)
} else if b.Outgoing != nil {
case b.Outgoing != nil:
return fmt.Sprintf("bandwidth: incoming=unlimited, outgoing=%.3f%%", *b.Outgoing)
} else {
default:
return "bandwidth: incoming=unlimited, outgoing=unlimited"
}
}

View file

@ -835,22 +835,23 @@ func (s *ClientSession) IsAllowedToSend(data *MessageClientMessageData) error {
s.mu.Lock()
defer s.mu.Unlock()
if data != nil && data.RoomType == "screen" {
switch {
case data != nil && data.RoomType == "screen":
if s.hasPermissionLocked(PERMISSION_MAY_PUBLISH_SCREEN) {
return nil
}
return &PermissionError{PERMISSION_MAY_PUBLISH_SCREEN}
} else if s.hasPermissionLocked(PERMISSION_MAY_PUBLISH_MEDIA) {
case s.hasPermissionLocked(PERMISSION_MAY_PUBLISH_MEDIA):
// Client is allowed to publish any media (audio / video).
return nil
} else if data != nil && data.Type == "offer" {
case data != nil && data.Type == "offer":
// Check what user is trying to publish and check permissions accordingly.
if _, err := s.isSdpAllowedToSendLocked(data.offerSdp); err != nil {
return err
}
return nil
} else {
default:
// Candidate or unknown event, check if client is allowed to publish any media.
if s.hasAnyPermissionLocked(PERMISSION_MAY_PUBLISH_AUDIO, PERMISSION_MAY_PUBLISH_VIDEO) {
return nil

View file

@ -319,7 +319,8 @@ func (p *mcuJanusPublisher) GetStreams(ctx context.Context) ([]PublisherStream,
continue
}
if strings.EqualFold(s.Type, "audio") {
switch {
case strings.EqualFold(s.Type, "audio"):
s.Codec = answerCodec.Name
if value, found := getFmtpValue(answerCodec.Fmtp, "useinbandfec"); found && value == "1" {
s.Fec = true
@ -330,7 +331,7 @@ func (p *mcuJanusPublisher) GetStreams(ctx context.Context) ([]PublisherStream,
if value, found := getFmtpValue(answerCodec.Fmtp, "stereo"); found && value == "1" {
s.Stereo = true
}
} else if strings.EqualFold(s.Type, "video") {
case strings.EqualFold(s.Type, "video"):
s.Codec = answerCodec.Name
// TODO: Determine if SVC is used.
s.Svc = false
@ -384,9 +385,9 @@ func (p *mcuJanusPublisher) GetStreams(ctx context.Context) ([]PublisherStream,
}
}
} else if strings.EqualFold(s.Type, "data") { // nolint
case strings.EqualFold(s.Type, "data"):
// Already handled above.
} else {
default:
p.logger.Printf("Skip type %s", s.Type)
continue
}

View file

@ -2086,18 +2086,20 @@ func (m *mcuProxy) NewPublisher(ctx context.Context, listener McuListener, id Pu
incoming_b = bw.Incoming
}
if incoming_a == nil && incoming_b == nil {
switch {
case incoming_a == nil && incoming_b == nil:
return 0
} else if incoming_a == nil && incoming_b != nil {
case incoming_a == nil && incoming_b != nil:
return -1
} else if incoming_a != nil && incoming_b == nil {
case incoming_a != nil && incoming_b == nil:
return -1
} else if *incoming_a < *incoming_b {
case *incoming_a < *incoming_b:
return -1
} else if *incoming_a > *incoming_b {
case *incoming_a > *incoming_b:
return 1
default:
return 0
}
return 0
})
publisher = m.createPublisher(ctx, listener, id, sid, streamType, settings, initiator, connections2, func(c *mcuProxyConnection) bool {
return true
@ -2349,18 +2351,20 @@ func (m *mcuProxy) NewSubscriber(ctx context.Context, listener McuListener, publ
outgoing_b = bw.Outgoing
}
if outgoing_a == nil && outgoing_b == nil {
switch {
case outgoing_a == nil && outgoing_b == nil:
return 0
} else if outgoing_a == nil && outgoing_b != nil {
case outgoing_a == nil && outgoing_b != nil:
return -1
} else if outgoing_a != nil && outgoing_b == nil {
case outgoing_a != nil && outgoing_b == nil:
return -1
} else if *outgoing_a < *outgoing_b {
case *outgoing_a < *outgoing_b:
return -1
} else if *outgoing_a > *outgoing_b {
case *outgoing_a > *outgoing_b:
return 1
default:
return 0
}
return 0
})
subscriber = m.createSubscriber(ctx, listener, publisherInfo, publisher, streamType, connections2, func(c *mcuProxyConnection) bool {
return true