Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MarshalYAML on RoomConfiguration and related types #771

Merged
merged 10 commits into from
Jul 30, 2024
5 changes: 5 additions & 0 deletions .changeset/tame-peas-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"github.com/livekit/protocol": patch
---

Implement MarshalYAML on RoomConfiguration and related types
30 changes: 30 additions & 0 deletions livekit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package livekit

import (
"github.com/bufbuild/protoyaml-go"
proto "google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
68 changes: 68 additions & 0 deletions livekit/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
proto "google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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))
}
Loading