-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_loader.go
More file actions
87 lines (69 loc) · 2.08 KB
/
Copy pathtemplate_loader.go
File metadata and controls
87 lines (69 loc) · 2.08 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
package parser
import (
"context"
"sync"
"time"
)
// TemplateLoader defines the interface for loading templates
type TemplateLoader interface {
// Load returns the template content by name
Load(name string) (content string, err error)
// List returns all available template names
List() ([]string, error)
// Watch starts watching for template changes and calls the callback
// when a template is modified. Returns a context cancel function.
Watch(ctx context.Context, callback func(name string)) error
// LastModified returns the last modification time of a template
LastModified(name string) (time.Time, error)
}
// MemoryLoader loads templates from memory (useful for testing)
type MemoryLoader struct {
templates map[string]string
mu sync.RWMutex
}
// NewMemoryLoader creates a new memory-based template loader
func NewMemoryLoader() *MemoryLoader {
return &MemoryLoader{
templates: make(map[string]string),
}
}
// AddTemplate adds a template to memory
func (m *MemoryLoader) AddTemplate(name, content string) {
m.mu.Lock()
defer m.mu.Unlock()
m.templates[name] = content
}
// Load implements TemplateLoader
func (m *MemoryLoader) Load(name string) (string, error) {
m.mu.RLock()
defer m.mu.RUnlock()
content, exists := m.templates[name]
if !exists {
return "", ErrTemplateNotFound
}
return content, nil
}
// List implements TemplateLoader
func (m *MemoryLoader) List() ([]string, error) {
m.mu.RLock()
defer m.mu.RUnlock()
names := make([]string, 0, len(m.templates))
for name := range m.templates {
names = append(names, name)
}
return names, nil
}
// Watch implements TemplateLoader (no-op for memory loader)
func (m *MemoryLoader) Watch(ctx context.Context, callback func(name string)) error {
// Memory loader doesn't support watching
return nil
}
// LastModified implements TemplateLoader (returns current time for memory loader)
func (m *MemoryLoader) LastModified(name string) (time.Time, error) {
m.mu.RLock()
defer m.mu.RUnlock()
if _, exists := m.templates[name]; !exists {
return time.Time{}, ErrTemplateNotFound
}
return time.Now(), nil
}