-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickles.go
77 lines (66 loc) · 2.36 KB
/
pickles.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package helpers
import (
"fmt"
"github.com/cucumber/messages-go/v10"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
type (
pickleDocString *messages.PickleStepArgument_PickleDocString
pickleTable *messages.PickleStepArgument_PickleTable
)
// YamlDocString adds unmarshalling method to easily manage YAML content.
type YamlDocString pickleDocString
// UnmarshalYamlDocString converts a *messages.PickleStepArgument_PickleDocString into
// a map[string]interface{}.
func UnmarshalYamlDocString(doc YamlDocString) (obj map[string]interface{}, err error) {
return obj, yaml.Unmarshal([]byte(doc.Content), &obj)
}
type (
// ResourceTable adds unmarshalling method to easily manage Resource table.
ResourceTable pickleTable
// ResourceTableRow describes a resource in the ResourceTable.
ResourceTableRow struct {
GroupVersion string
Kind string
Namespace string
Name string
}
)
// UnmarshalResourceTable converts a *messages.PickleStepArgument_PickleTable into a
// list of ResourceTableRow, in which we can easily extract GroupVersionKind and
// NamespacedName values.
func UnmarshalResourceTable(table ResourceTable) (resources []ResourceTableRow, err error) {
for i, row := range table.Rows {
if i == 0 && row.Cells[0].GetValue() == "ApiGroupVersion" {
// Ignore header line if exists
continue
}
if len(row.Cells) != 4 {
return nil, fmt.Errorf("invalid resource table: it must contains 4 columns (GroupVersion, Kind, Namespace, Name)")
}
resources = append(resources, ResourceTableRow{
GroupVersion: row.Cells[0].GetValue(),
Kind: row.Cells[1].GetValue(),
Namespace: row.Cells[2].GetValue(),
Name: row.Cells[3].GetValue(),
})
}
return resources, nil
}
// GroupVersionKind returns the GroupVersionKind of the current resource.
func (row ResourceTableRow) GroupVersionKind() schema.GroupVersionKind {
gvk, _ := GroupVersionKindFrom(row.GroupVersion + "/" + row.Kind)
return gvk
}
// NamespacedName returns the NamespacedName of the current resource.
func (row ResourceTableRow) NamespacedName() types.NamespacedName {
var namespacedName types.NamespacedName
if row.Namespace == "" {
namespacedName, _ = NamespacedNameFrom(row.Name)
return namespacedName
}
namespacedName, _ = NamespacedNameFrom(row.Namespace + "/" + row.Name)
return namespacedName
}