-
Notifications
You must be signed in to change notification settings - Fork 25
/
uritemplatecache.go
57 lines (48 loc) · 1.19 KB
/
uritemplatecache.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
package uritemplates
import (
"errors"
"sync"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
)
type URITemplateCache struct {
cache map[string]*UriTemplate
mutex *sync.Mutex
}
func NewUriTemplateCache() *URITemplateCache {
return &URITemplateCache{
cache: map[string]*UriTemplate{},
mutex: &sync.Mutex{},
}
}
func (c *URITemplateCache) Intern(uriTemplate string) (*UriTemplate, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
cachedTemplate, ok := c.cache[uriTemplate]
if ok {
return cachedTemplate, nil
}
template, err := Parse(uriTemplate)
if err != nil {
return nil, err
}
c.cache[uriTemplate] = template
return template, nil
}
func (c *URITemplateCache) Expand(uriTemplate string, value any) (string, error) {
template, err := c.Intern(uriTemplate)
if err != nil {
return "", err
}
return template.Expand(value)
}
func (c *URITemplateCache) ExpandLinked(resource *resources.Resource, linkKey string, value any) (string, error) {
rawTemplate, ok := resource.Links[linkKey]
if !ok {
return "", errors.New("ExpandLinked could not find linkKey")
}
template, err := c.Intern(rawTemplate)
if err != nil {
return "", err
}
return template.Expand(value)
}