Move "StringMap" class to api module.

This commit is contained in:
Joachim Bauch 2025-09-26 13:50:18 +02:00
commit 41728572fe
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
41 changed files with 414 additions and 342 deletions

69
api/stringmap.go Normal file
View 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
View 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)
}