-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
token_types.go
63 lines (50 loc) · 1.79 KB
/
token_types.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
package core
import (
"context"
"fmt"
)
// Token represent a value that can be resolved at resolution time.
type Token interface {
// token's value resolution may depends on 0 or more Resources.
Dependencies() []Resource
}
// StringToken represent a string value that can be resolved at resolution time.
type StringToken interface {
Token
Resolve(ctx context.Context) (string, error)
}
var _ StringToken = LiteralStringToken("")
// LiteralStringToken represents a literal string value.
type LiteralStringToken string
func (t LiteralStringToken) Resolve(ctx context.Context) (string, error) {
return string(t), nil
}
func (t LiteralStringToken) Dependencies() []Resource {
return nil
}
// NewResourceFieldStringToken constructs new ResourceFieldStringToken.
// @TODO: ideally the resolverFunc can be a shared implementation which dump Resource as json and obtain the fieldPath.
func NewResourceFieldStringToken(res Resource, fieldPath string,
resolverFunc func(ctx context.Context, res Resource, fieldPath string) (string, error)) *ResourceFieldStringToken {
return &ResourceFieldStringToken{
res: res,
fieldPath: fieldPath,
resolveFunc: resolverFunc,
}
}
var _ StringToken = &ResourceFieldStringToken{}
type ResourceFieldStringToken struct {
res Resource
fieldPath string
resolveFunc func(ctx context.Context, res Resource, fieldPath string) (string, error)
}
func (t *ResourceFieldStringToken) Resolve(ctx context.Context) (string, error) {
return t.resolveFunc(ctx, t.res, t.fieldPath)
}
func (t *ResourceFieldStringToken) Dependencies() []Resource {
return []Resource{t.res}
}
func (t *ResourceFieldStringToken) MarshalJSON() ([]byte, error) {
payload := fmt.Sprintf(`{"$ref": "#/resources/%v/%v/%v"}`, t.res.Type(), t.res.ID(), t.fieldPath)
return []byte(payload), nil
}