diff --git a/.changeset/tame-peas-tickle.md b/.changeset/tame-peas-tickle.md new file mode 100644 index 000000000..90fa88f17 --- /dev/null +++ b/.changeset/tame-peas-tickle.md @@ -0,0 +1,5 @@ +--- +"github.com/livekit/protocol": patch +--- + +Implement MarshalYAML on RoomConfiguration and related types diff --git a/livekit/types.go b/livekit/types.go index a07cfbe87..9f4b1ba3b 100644 --- a/livekit/types.go +++ b/livekit/types.go @@ -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 +} diff --git a/livekit/types_test.go b/livekit/types_test.go index c0984732d..5d1257188 100644 --- a/livekit/types_test.go +++ b/livekit/types_test.go @@ -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)) +}