You've already forked filebrowser
mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-11-26 14:25:26 +08:00
updates
This commit is contained in:
13
variables/types.go
Normal file
13
variables/types.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package variables
|
||||
|
||||
import "reflect"
|
||||
|
||||
// IsMap checks if some variable is a map
|
||||
func IsMap(sth interface{}) bool {
|
||||
return reflect.ValueOf(sth).Kind() == reflect.Map
|
||||
}
|
||||
|
||||
// IsSlice checks if some variable is a slice
|
||||
func IsSlice(sth interface{}) bool {
|
||||
return reflect.ValueOf(sth).Kind() == reflect.Slice
|
||||
}
|
||||
49
variables/types_test.go
Normal file
49
variables/types_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package variables
|
||||
|
||||
import "testing"
|
||||
|
||||
type interfaceToBool struct {
|
||||
Value interface{}
|
||||
Result bool
|
||||
}
|
||||
|
||||
var testIsMap = []*interfaceToBool{
|
||||
&interfaceToBool{"teste", false},
|
||||
&interfaceToBool{453478, false},
|
||||
&interfaceToBool{-984512, false},
|
||||
&interfaceToBool{true, false},
|
||||
&interfaceToBool{map[string]bool{}, true},
|
||||
&interfaceToBool{map[int]bool{}, true},
|
||||
&interfaceToBool{map[interface{}]bool{}, true},
|
||||
&interfaceToBool{[]string{}, false},
|
||||
}
|
||||
|
||||
func TestIsMap(t *testing.T) {
|
||||
for _, test := range testIsMap {
|
||||
if IsMap(test.Value) != test.Result {
|
||||
t.Errorf("Incorrect value on IsMap for %v; want: %v; got: %v", test.Value, test.Result, !test.Result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var testIsSlice = []*interfaceToBool{
|
||||
&interfaceToBool{"teste", false},
|
||||
&interfaceToBool{453478, false},
|
||||
&interfaceToBool{-984512, false},
|
||||
&interfaceToBool{true, false},
|
||||
&interfaceToBool{map[string]bool{}, false},
|
||||
&interfaceToBool{map[int]bool{}, false},
|
||||
&interfaceToBool{map[interface{}]bool{}, false},
|
||||
&interfaceToBool{[]string{}, true},
|
||||
&interfaceToBool{[]int{}, true},
|
||||
&interfaceToBool{[]bool{}, true},
|
||||
&interfaceToBool{[]interface{}{}, true},
|
||||
}
|
||||
|
||||
func TestIsSlice(t *testing.T) {
|
||||
for _, test := range testIsSlice {
|
||||
if IsSlice(test.Value) != test.Result {
|
||||
t.Errorf("Incorrect value on IsSlice for %v; want: %v; got: %v", test.Value, test.Result, !test.Result)
|
||||
}
|
||||
}
|
||||
}
|
||||
47
variables/variables.go
Normal file
47
variables/variables.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package variables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Dict allows to send more than one variable into a template.
|
||||
func Dict(values ...interface{}) (map[string]interface{}, error) {
|
||||
if len(values)%2 != 0 {
|
||||
return nil, errors.New("invalid dict call")
|
||||
}
|
||||
dict := make(map[string]interface{}, len(values)/2)
|
||||
for i := 0; i < len(values); i += 2 {
|
||||
key, ok := values[i].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("dict keys must be strings")
|
||||
}
|
||||
dict[key] = values[i+1]
|
||||
}
|
||||
|
||||
return dict, nil
|
||||
}
|
||||
|
||||
// FieldInStruct checks if variable is defined in a struct.
|
||||
func FieldInStruct(data interface{}, field string) bool {
|
||||
t := reflect.Indirect(reflect.ValueOf(data)).Type()
|
||||
|
||||
if t.Kind() != reflect.Struct {
|
||||
log.Print("Non-struct type not allowed.")
|
||||
return false
|
||||
}
|
||||
|
||||
_, b := t.FieldByName(field)
|
||||
return b
|
||||
}
|
||||
|
||||
// StringInSlice checks if a slice contains a string.
|
||||
func StringInSlice(a string, list []string) (bool, int) {
|
||||
for i, b := range list {
|
||||
if b == a {
|
||||
return true, i
|
||||
}
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
41
variables/variables_test.go
Normal file
41
variables/variables_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package variables
|
||||
|
||||
import "testing"
|
||||
|
||||
type testFieldInStructData struct {
|
||||
f1 string
|
||||
f2 bool
|
||||
f3 int
|
||||
f4 func()
|
||||
}
|
||||
|
||||
type testFieldInStruct struct {
|
||||
data interface{}
|
||||
field string
|
||||
result bool
|
||||
}
|
||||
|
||||
var testFieldInStructCases = []testFieldInStruct{
|
||||
{testFieldInStructData{}, "f1", true},
|
||||
{testFieldInStructData{}, "f2", true},
|
||||
{testFieldInStructData{}, "f3", true},
|
||||
{testFieldInStructData{}, "f4", true},
|
||||
{testFieldInStructData{}, "f5", false},
|
||||
{[]string{}, "", false},
|
||||
{map[string]int{"oi": 4}, "", false},
|
||||
{"asa", "", false},
|
||||
{"int", "", false},
|
||||
}
|
||||
|
||||
func TestFieldInStruct(t *testing.T) {
|
||||
for _, pair := range testFieldInStructCases {
|
||||
v := FieldInStruct(pair.data, pair.field)
|
||||
if v != pair.result {
|
||||
t.Error(
|
||||
"For", pair.data,
|
||||
"expected", pair.result,
|
||||
"got", v,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user