Skip to content

Commit

Permalink
Add a string conversion for a variety of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
cantstopthesignal authored and arlolra committed Nov 14, 2016
1 parent bf32de3 commit 76396c9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
12 changes: 12 additions & 0 deletions configuration.go
Expand Up @@ -48,6 +48,10 @@ const (

var BundlePolicyString = []string{"Balanced", "MaxBundle", "MaxCompat"}

func (p BundlePolicy) String() string {
return EnumToStringSafe(int(p), BundlePolicyString)
}

const (
IceTransportPolicyNone IceTransportPolicy = iota
IceTransportPolicyRelay
Expand All @@ -60,6 +64,10 @@ const (

var IceTransportPolicyString = []string{"None", "Relay", "NoHost", "All"}

func (p IceTransportPolicy) String() string {
return EnumToStringSafe(int(p), IceTransportPolicyString)
}

const (
SignalingStateStable SignalingState = iota
SignalingStateHaveLocalOffer
Expand All @@ -74,6 +82,10 @@ var SignalingStateString = []string{"Stable",
"HaveRemoteOffer", "HaveRemotePrAnswer",
"Closed"}

func (s SignalingState) String() string {
return EnumToStringSafe(int(s), SignalingStateString)
}

// TODO: [ED]
/* const (
RtcpMuxPolicyNegotiate RtcpMuxPolicy = iota
Expand Down
4 changes: 4 additions & 0 deletions datachannel.go
Expand Up @@ -31,6 +31,10 @@ const (

var DataStateString = []string{"Connecting", "Open", "Closing", "Closed"}

func (s DataState) String() string {
return EnumToStringSafe(int(s), DataStateString)
}

var DCMap = NewCGOMap()

/* DataChannel
Expand Down
12 changes: 12 additions & 0 deletions ice.go
Expand Up @@ -19,6 +19,10 @@ const (

var IceProtocolString = []string{"udp", "tcp"}

func (p IceProtocol) String() string {
return EnumToStringSafe(int(p), IceProtocolString)
}

const (
IceCandidateTypeHost IceCandidateType = iota
IceCandidateTypeSrflx
Expand All @@ -28,6 +32,10 @@ const (

var IceCandidateTypeString = []string{"host", "srflx", "prflx", "relay"}

func (t IceCandidateType) String() string {
return EnumToStringSafe(int(t), IceCandidateTypeString)
}

const (
IceTcpCandidateTypeActive IceTcpCandidateType = iota
IceTcpCandidateTypePassive
Expand All @@ -36,6 +44,10 @@ const (

var IceTcpCandidateTypeString = []string{"active", "passive", "so"}

func (t IceTcpCandidateType) String() string {
return EnumToStringSafe(int(t), IceTcpCandidateTypeString)
}

type IceCandidate struct {
Candidate string `json:"candidate"`
SdpMid string `json:"sdpMid"`
Expand Down
12 changes: 12 additions & 0 deletions peerconnection.go
Expand Up @@ -66,6 +66,10 @@ const (
var PeerConnectionStateString = []string{
"New", "Connecting", "Connected", "Disconnected", "Failed"}

func (s PeerConnectionState) String() string {
return EnumToStringSafe(int(s), PeerConnectionStateString)
}

const (
IceConnectionStateNew IceConnectionState = iota
IceConnectionStateChecking
Expand All @@ -80,6 +84,10 @@ var IceConnectionStateString = []string{
"New", "Checking", "Connected", "Completed",
"Failed", "Disconnected", "Closed"}

func (s IceConnectionState) String() string {
return EnumToStringSafe(int(s), IceConnectionStateString)
}

const (
IceGatheringStateNew IceGatheringState = iota
IceGatheringStateGathering
Expand All @@ -89,6 +97,10 @@ const (
var IceGatheringStateString = []string{
"New", "Gathering", "Complete"}

func (s IceGatheringState) String() string {
return EnumToStringSafe(int(s), IceGatheringStateString)
}

var PCMap = NewCGOMap()

/* WebRTC PeerConnection
Expand Down
15 changes: 14 additions & 1 deletion utils.go
@@ -1,6 +1,9 @@
package webrtc

import "sync"
import (
"strconv"
"sync"
)

// A general map that stores pointers by an unique int index, since we can't
// keep Go pointers in C land. This is a little workaround suggested in the
Expand Down Expand Up @@ -40,3 +43,13 @@ func (m *CGOMap) Delete(index int) {
delete(m.pointers, index)
m.lock.Unlock()
}

// Return a string value for an integer enum from a mapping array
// or the integer string if the integer it outside the expected range.
func EnumToStringSafe(value int, valueStrings []string) string {
if value >= 0 && value < len(valueStrings) {
return valueStrings[value]
} else {
return strconv.Itoa(value)
}
}

0 comments on commit 76396c9

Please sign in to comment.