Skip to content

Commit

Permalink
Merge branch 'feat/room-meta'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Totting committed Nov 29, 2023
2 parents de8c7e3 + cccabc1 commit 1892d23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 10 additions & 2 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package sfu

import (
"context"
"errors"
"sync"
)

var (
ErrMetaNotFound = errors.New("meta: metadata not found")
)

type Metadata struct {
mu sync.RWMutex
m map[string]interface{}
Expand All @@ -27,11 +32,14 @@ func (m *Metadata) Set(key string, value interface{}) {
m.onChanged(key, value)
}

func (m *Metadata) Get(key string) interface{} {
func (m *Metadata) Get(key string) (interface{}, error) {
m.mu.RLock()
defer m.mu.RUnlock()

return m.m[key]
if _, ok := m.m[key]; !ok {
return nil, ErrMetaNotFound
}
return m.m[key], nil
}

func (m *Metadata) Delete(key string) {
Expand Down
11 changes: 8 additions & 3 deletions meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sfu
import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestMetadata(t *testing.T) {
Expand All @@ -12,19 +14,22 @@ func TestMetadata(t *testing.T) {
t.Error("NewMetadata returned nil")
}

_, err := m.Get("key1")
require.Equal(t, ErrMetaNotFound, err)

// Test Set and Get methods
m.Set("key1", "value1")
m.Set("key2", 123)
if value := m.Get("key1"); value != "value1" {
if value, _ := m.Get("key1"); value != "value1" {
t.Errorf("Get returned %v, expected %v", value, "value1")
}
if value := m.Get("key2"); value != 123 {
if value, _ := m.Get("key2"); value != 123 {
t.Errorf("Get returned %v, expected %v", value, 123)
}

// Test Delete method
m.Delete("key1")
if value := m.Get("key1"); value != nil {
if value, _ := m.Get("key1"); value != nil {
t.Errorf("Get returned %v, expected nil", value)
}

Expand Down

0 comments on commit 1892d23

Please sign in to comment.