mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2026-03-14 14:35:44 +01:00
Move "StringMap" class to api module.
This commit is contained in:
parent
91220431be
commit
41728572fe
41 changed files with 414 additions and 342 deletions
69
api/stringmap.go
Normal file
69
api/stringmap.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/**
|
||||
* Standalone signaling server for the Nextcloud Spreed app.
|
||||
* Copyright (C) 2021 struktur AG
|
||||
*
|
||||
* @author Joachim Bauch <bauch@struktur.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package api
|
||||
|
||||
// StringMap maps string keys to arbitrary values.
|
||||
type StringMap map[string]any
|
||||
|
||||
func ConvertStringMap(ob any) (StringMap, bool) {
|
||||
if ob == nil {
|
||||
return nil, true
|
||||
}
|
||||
|
||||
switch ob := ob.(type) {
|
||||
case map[string]any:
|
||||
return StringMap(ob), true
|
||||
case StringMap:
|
||||
return ob, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
// GetStringMapEntry returns an entry from a string map in a given type.
|
||||
func GetStringMapEntry[T any](m StringMap, key string) (s T, ok bool) {
|
||||
var defaultValue T
|
||||
v, found := m[key]
|
||||
if !found {
|
||||
return defaultValue, false
|
||||
}
|
||||
|
||||
s, ok = v.(T)
|
||||
return
|
||||
}
|
||||
|
||||
func GetStringMapString[T ~string](m StringMap, key string) (T, bool) {
|
||||
var defaultValue T
|
||||
v, found := m[key]
|
||||
if !found {
|
||||
return defaultValue, false
|
||||
}
|
||||
|
||||
switch v := v.(type) {
|
||||
case T:
|
||||
return v, true
|
||||
case string:
|
||||
return T(v), true
|
||||
default:
|
||||
return defaultValue, false
|
||||
}
|
||||
}
|
||||
90
api/stringmap_test.go
Normal file
90
api/stringmap_test.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* Standalone signaling server for the Nextcloud Spreed app.
|
||||
* Copyright (C) 2021 struktur AG
|
||||
*
|
||||
* @author Joachim Bauch <bauch@struktur.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestConvertStringMap(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
d := map[string]any{
|
||||
"foo": "bar",
|
||||
"bar": 2,
|
||||
}
|
||||
|
||||
m, ok := ConvertStringMap(d)
|
||||
if assert.True(ok) {
|
||||
assert.EqualValues(d, m)
|
||||
}
|
||||
|
||||
if m, ok := ConvertStringMap(nil); assert.True(ok) {
|
||||
assert.Nil(m)
|
||||
}
|
||||
|
||||
_, ok = ConvertStringMap("foo")
|
||||
assert.False(ok)
|
||||
|
||||
_, ok = ConvertStringMap(1)
|
||||
assert.False(ok)
|
||||
|
||||
_, ok = ConvertStringMap(map[int]any{
|
||||
1: "foo",
|
||||
})
|
||||
assert.False(ok)
|
||||
}
|
||||
|
||||
func TestGetStringMapString(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
type StringMapTestString string
|
||||
|
||||
var ok bool
|
||||
m := StringMap{
|
||||
"foo": "bar",
|
||||
"bar": StringMapTestString("baz"),
|
||||
"baz": 1234,
|
||||
}
|
||||
if v, ok := GetStringMapString[string](m, "foo"); assert.True(ok) {
|
||||
assert.Equal("bar", v)
|
||||
}
|
||||
if v, ok := GetStringMapString[StringMapTestString](m, "foo"); assert.True(ok) {
|
||||
assert.Equal(StringMapTestString("bar"), v)
|
||||
}
|
||||
v, ok := GetStringMapString[string](m, "bar")
|
||||
assert.False(ok, "should not find object, got %+v", v)
|
||||
|
||||
if v, ok := GetStringMapString[StringMapTestString](m, "bar"); assert.True(ok) {
|
||||
assert.Equal(StringMapTestString("baz"), v)
|
||||
}
|
||||
|
||||
_, ok = GetStringMapString[string](m, "baz")
|
||||
assert.False(ok)
|
||||
_, ok = GetStringMapString[StringMapTestString](m, "baz")
|
||||
assert.False(ok)
|
||||
_, ok = GetStringMapString[string](m, "invalid")
|
||||
assert.False(ok)
|
||||
_, ok = GetStringMapString[StringMapTestString](m, "invalid")
|
||||
assert.False(ok)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue