This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
/
convert.go
108 lines (95 loc) · 2.82 KB
/
convert.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package starlark
import (
"strings"
"github.com/go-vela/types/raw"
"go.starlark.net/starlark"
)
// convertTemplateVars takes template variables and converts
// them to a starlark string dictionary for template reference.
//
// Example Usage within template: ctx["vars"]["message"] = "Hello, World!"
//
// Explanation of type "starlark.StringDict":
// https://pkg.go.dev/go.starlark.net/starlark#StringDict
func convertTemplateVars(m map[string]interface{}) (*starlark.Dict, error) {
dict := starlark.NewDict(0)
// loop through user vars converting provided types to starlark primitives
for key, value := range m {
val, err := toStarlark(value)
if err != nil {
return nil, err
}
err = dict.SetKey(starlark.String(key), val)
if err != nil {
return nil, err
}
}
return dict, nil
}
// convertPlatformVars takes the platform injected variables
// within the step environment block and converts them to a
// starlark string dictionary.
//
// Example Usage within template: ctx["vela"]["build"]["number"] = 1
//
// Explanation of type "starlark.StringDict":
// https://pkg.go.dev/go.starlark.net/starlark#StringDict
func convertPlatformVars(slice raw.StringSliceMap, name string) (*starlark.Dict, error) {
build := starlark.NewDict(0)
repo := starlark.NewDict(0)
user := starlark.NewDict(0)
system := starlark.NewDict(0)
dict := starlark.NewDict(0)
err := dict.SetKey(starlark.String("build"), build)
if err != nil {
return nil, err
}
err = dict.SetKey(starlark.String("repo"), repo)
if err != nil {
return nil, err
}
err = dict.SetKey(starlark.String("user"), user)
if err != nil {
return nil, err
}
err = dict.SetKey(starlark.String("system"), system)
if err != nil {
return nil, err
}
err = system.SetKey(starlark.String("template_name"), starlark.String(name))
if err != nil {
return nil, err
}
for key, value := range slice {
key = strings.ToLower(key)
if strings.HasPrefix(key, "vela_") {
key = strings.TrimPrefix(key, "vela_")
switch {
case strings.HasPrefix(key, "build_"):
err := build.SetKey(starlark.String(strings.TrimPrefix(key, "build_")), starlark.String(value))
if err != nil {
return nil, err
}
case strings.HasPrefix(key, "repo_"):
err := repo.SetKey(starlark.String(strings.TrimPrefix(key, "repo_")), starlark.String(value))
if err != nil {
return nil, err
}
case strings.HasPrefix(key, "user_"):
err := user.SetKey(starlark.String(strings.TrimPrefix(key, "user_")), starlark.String(value))
if err != nil {
return nil, err
}
default:
err := system.SetKey(starlark.String(key), starlark.String(value))
if err != nil {
return nil, err
}
}
}
}
return dict, nil
}