-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext_manager.go
50 lines (37 loc) · 963 Bytes
/
context_manager.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
package digo
import (
"errors"
"reflect"
)
type ContextManager struct {
contexts map[string]*Context
}
func (this *ContextManager) Context(filePath string) (*Context, error) {
if ctx, exists := this.contexts[filePath]; exists {
return ctx, nil
}
ctx, err := this.newContext(filePath)
if err != nil {
return nil, err
}
this.contexts[filePath] = ctx
return ctx, nil
}
func (this *ContextManager) newContext(filePath string) (*Context, error) {
ctx := &Context{}
err := ctx.unmarshal(filePath)
if err != nil {
return nil, errors.New("Error creating new Context -> " + err.Error())
}
return ctx, nil
}
func (this *ContextManager) New(key string, isPtr bool) (interface{}, error) {
t, err := TypeRegistry.Get(key)
if err != nil {
return struct{}{}, errors.New("Error getting the type from TypeRegistry -> " + err.Error())
}
if isPtr {
return reflect.New(t).Interface(), nil
}
return reflect.New(t).Elem().Interface(), nil
}