Implement MarshalYAML on RoomConfiguration and related types (#771)

This commit is contained in:
Benjamin Pracht
2024-07-30 11:53:18 -07:00
committed by GitHub
parent 45b9751aaf
commit ffccf573cd
3 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"github.com/livekit/protocol": patch
---
Implement MarshalYAML on RoomConfiguration and related types

View File

@@ -16,6 +16,7 @@ package livekit
import (
"github.com/bufbuild/protoyaml-go"
proto "google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
)
@@ -73,6 +74,10 @@ func (r *RoomConfiguration) UnmarshalYAML(value *yaml.Node) error {
return protoyaml.Unmarshal(str, r)
}
func (r *RoomConfiguration) MarshalYAML() (interface{}, error) {
return marshalProto(r)
}
func (r *RoomEgress) UnmarshalYAML(value *yaml.Node) error {
// Marshall the Node back to yaml to pass it to the protobuf specific unmarshaller
str, err := yaml.Marshal(value)
@@ -83,6 +88,10 @@ func (r *RoomEgress) UnmarshalYAML(value *yaml.Node) error {
return protoyaml.Unmarshal(str, r)
}
func (r *RoomEgress) MarshalYAML() (interface{}, error) {
return marshalProto(r)
}
func (r *RoomAgent) UnmarshalYAML(value *yaml.Node) error {
// Marshall the Node back to yaml to pass it to the protobuf specific unmarshaller
str, err := yaml.Marshal(value)
@@ -92,3 +101,24 @@ func (r *RoomAgent) UnmarshalYAML(value *yaml.Node) error {
return protoyaml.Unmarshal(str, r)
}
func (r *RoomAgent) MarshalYAML() (interface{}, error) {
return marshalProto(r)
}
func marshalProto(o proto.Message) (map[string]interface{}, error) {
// Marshall the Node to yaml using the protobuf specific marshaller to ensure the proper field names are used
str, err := protoyaml.MarshalOptions{UseProtoNames: true}.Marshal(o)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
err = yaml.Unmarshal(str, &m)
if err != nil {
return nil, err
}
return m, nil
}

View File

@@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/require"
proto "google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
)
@@ -39,6 +40,38 @@ a:
}
func TestMarshallRoomConfiguration(t *testing.T) {
r := &RoomConfiguration{
Name: "name",
MaxParticipants: 42,
EmptyTimeout: 12,
DepartureTimeout: 13,
MinPlayoutDelay: 14,
MaxPlayoutDelay: 15,
Egress: &RoomEgress{
Room: &RoomCompositeEgressRequest{
AudioOnly: true,
RoomName: "room name",
},
},
Agent: &RoomAgent{
Dispatches: []*RoomAgentDispatch{
&RoomAgentDispatch{
AgentName: "agent name",
},
},
},
}
b, err := yaml.Marshal(r)
require.NoError(t, err)
var ur RoomConfiguration
err = yaml.Unmarshal(b, &ur)
require.NoError(t, err)
require.True(t, proto.Equal(r, &ur))
}
func TestUnmarshallRoomEgress(t *testing.T) {
y := `
a:
@@ -67,6 +100,23 @@ b:
require.Equal(t, "key", re.Participant.FileOutputs[0].Output.(*EncodedFileOutput_S3).S3.AccessKey)
}
func TestMarshallRoomEgress(t *testing.T) {
e := &RoomEgress{
Room: &RoomCompositeEgressRequest{
AudioOnly: true,
RoomName: "room name",
},
}
b, err := yaml.Marshal(e)
require.NoError(t, err)
var ue RoomEgress
err = yaml.Unmarshal(b, &ue)
require.NoError(t, err)
require.True(t, proto.Equal(e, &ue))
}
func TestUnmarshallRoomAgent(t *testing.T) {
y := `
a:
@@ -87,3 +137,21 @@ a:
require.Equal(t, "ag", re.Dispatches[1].AgentName)
require.Equal(t, "mm", re.Dispatches[1].Metadata)
}
func TestMarshallRoomAgent(t *testing.T) {
a := &RoomAgent{
Dispatches: []*RoomAgentDispatch{
&RoomAgentDispatch{
AgentName: "agent name",
},
},
}
b, err := yaml.Marshal(a)
require.NoError(t, err)
var ua RoomAgent
err = yaml.Unmarshal(b, &ua)
require.NoError(t, err)
require.True(t, proto.Equal(a, &ua))
}