-
Notifications
You must be signed in to change notification settings - Fork 136
/
insert_annotation.go
60 lines (48 loc) · 1.48 KB
/
insert_annotation.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
package overlay
import (
"fmt"
"github.com/k14s/ytt/pkg/template"
tplcore "github.com/k14s/ytt/pkg/template/core"
"go.starlark.net/starlark"
)
type InsertAnnotation struct {
newItem template.EvaluationNode
before bool
after bool
}
func NewInsertAnnotation(newItem template.EvaluationNode) (InsertAnnotation, error) {
annotation := InsertAnnotation{newItem: newItem}
anns := template.NewAnnotations(newItem)
if !anns.Has(AnnotationInsert) {
return annotation, fmt.Errorf(
"Expected item to have '%s' annotation", AnnotationInsert)
}
kwargs := anns.Kwargs(AnnotationInsert)
if len(kwargs) == 0 {
return annotation, fmt.Errorf("Expected '%s' annotation to have "+
"at least one keyword argument (before=..., after=...)", AnnotationInsert)
}
for _, kwarg := range kwargs {
kwargName := string(kwarg[0].(starlark.String))
switch kwargName {
case "before":
resultBool, err := tplcore.NewStarlarkValue(kwarg[1]).AsBool()
if err != nil {
return InsertAnnotation{}, err
}
annotation.before = resultBool
case "after":
resultBool, err := tplcore.NewStarlarkValue(kwarg[1]).AsBool()
if err != nil {
return InsertAnnotation{}, err
}
annotation.after = resultBool
default:
return annotation, fmt.Errorf(
"Unknown '%s' annotation keyword argument '%s'", AnnotationInsert, kwargName)
}
}
return annotation, nil
}
func (a InsertAnnotation) IsBefore() bool { return a.before }
func (a InsertAnnotation) IsAfter() bool { return a.after }