-
Notifications
You must be signed in to change notification settings - Fork 1
/
formres.go
67 lines (60 loc) · 1.46 KB
/
formres.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
//----------------------------------------
//
// Copyright © ying32. All Rights Reserved.
//
// Licensed under Apache License 2.0
//
//----------------------------------------
package lcl
import (
"errors"
"reflect"
"strings"
"sync"
)
type formResItem struct {
ClassName string
Data *[]byte
}
var (
// 用于自动查找并加载资源的
formResMap sync.Map
)
func getClassName(aClass interface{}) string {
className := ""
switch aClass.(type) {
case string:
className = aClass.(string)
default:
temp := strings.Split(reflect.TypeOf(aClass).String(), ".")
if len(temp) > 0 {
className = temp[len(temp)-1]
}
}
if len(className) == 0 {
return ""
}
return strings.ToUpper(className)
}
// RegisterFormResource
// 注册一个Form的资源
// 此种方式用于不指定Form资源,直接通过类名查找方式
func RegisterFormResource(aClass interface{}, data *[]byte) error {
className := getClassName(aClass)
if className == "" || data == nil {
return errors.New("className and data cannot be empty")
}
// FreePascal中不区分大小写的,所以统一转为大写
formResMap.Store(className, &formResItem{ClassName: className, Data: data})
return nil
}
// 查找对应的Form资源
func findFormResource(aClass interface{}) (*formResItem, error) {
className := getClassName(aClass)
if className != "" {
if val, ok := formResMap.Load(className); ok {
return val.(*formResItem), nil
}
}
return nil, errors.New("not found")
}