-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_utils.go
75 lines (65 loc) · 1.66 KB
/
ui_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package chioas
import (
"fmt"
"html/template"
"mime"
"path/filepath"
"reflect"
"strings"
)
// MappableOptions is an interface that can be used by either DocOptions.RedocOptions or DocOptions.SwaggerOptions
// and converts the options to a map (as used by the HTML template)
type MappableOptions interface {
ToMap() map[string]any
}
type OptionsWithFavIcons interface {
GetFavIcons() FavIcons
}
// FavIcons is a map of icon size and fav icon filename
//
// Used by SwaggerOptions.FavIcons, RedocOptions.FavIcons and RapidocOptions.FavIcons
type FavIcons map[int]string
func (f FavIcons) toHtml() template.HTML {
result := make([]string, 0, len(f))
for k, v := range f {
typeAtt := ""
if mt := mime.TypeByExtension(filepath.Ext(v)); mt != "" {
typeAtt = `type="` + mt + `"`
}
result = append(result, fmt.Sprintf(`<link rel="icon" %s href="./%s" sizes="%dx%d" />`, typeAtt, v, k, k))
}
return template.HTML(strings.Join(result, "\n"))
}
func addMap[T comparable](m map[string]any, pty T, name string) {
var empty T
if pty != empty {
m[name] = pty
}
}
func addMapDef[T comparable](m map[string]any, pty T, name string, def T) {
if pty != def {
m[name] = pty
}
}
func addMapNonNil(m map[string]any, pty any, name string) {
if !isNil(reflect.ValueOf(pty)) {
m[name] = pty
}
}
func isNil(v reflect.Value) bool {
if v.IsValid() {
switch v.Kind() {
case reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return v.IsNil()
default:
return v.IsZero()
}
}
return true
}
func addMapMappable(m map[string]any, pty MappableOptions, name string) {
vo := reflect.ValueOf(pty)
if !vo.IsNil() {
m[name] = pty.ToMap()
}
}