chore: prefer slices over plural types (#655)

* chore: prefer slices over plural types

* chore: update formatting to reflect slice types
This commit is contained in:
Luke Kingland 2021-11-12 20:36:36 +09:00 committed by GitHub
parent db2c3e246e
commit 4d29384958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 232 additions and 234 deletions

View File

@ -407,7 +407,7 @@ func runRemoveEnvsPrompt(f fn.Function) (err error) {
return return
} }
var newEnvs fn.Envs var newEnvs []fn.Env
removed := false removed := false
for i, e := range f.Envs { for i, e := range f.Envs {
if e.String() == selectedEnv { if e.String() == selectedEnv {

View File

@ -269,7 +269,7 @@ func runRemoveLabelsPrompt(f fn.Function, saver functionSaver) (err error) {
return return
} }
var newLabels fn.Labels var newLabels []fn.Label
removed := false removed := false
for i, e := range f.Labels { for i, e := range f.Labels {
if e.String() == selectedLabel { if e.String() == selectedLabel {

View File

@ -30,7 +30,7 @@ func (m *mockFunctionLoaderSaver) Save(f fn.Function) error {
return nil return nil
} }
func assertLabelEq(t *testing.T, actual fn.Labels, want fn.Labels) { func assertLabelEq(t *testing.T, actual []fn.Label, want []fn.Label) {
t.Helper() t.Helper()
if !reflect.DeepEqual(actual, want) { if !reflect.DeepEqual(actual, want) {
t.Errorf("labels = %v, want %v", actual, want) t.Errorf("labels = %v, want %v", actual, want)
@ -113,22 +113,22 @@ func TestNewConfigLabelsCmd(t *testing.T) {
return fn.Label{Key: &k, Value: &v} return fn.Label{Key: &k, Value: &v}
} }
assertLabel := func(ps fn.Labels) { assertLabel := func(ps []fn.Label) {
t.Helper() t.Helper()
assertLabelEq(t, *labels, ps) assertLabelEq(t, *labels, ps)
} }
run("add", enter, "a", enter, "b", enter) run("add", enter, "a", enter, "b", enter)
assertLabel(fn.Labels{p("a", "b")}) assertLabel([]fn.Label{p("a", "b")})
run("add", enter, enter, "c", enter, "d", enter) run("add", enter, enter, "c", enter, "d", enter)
assertLabel(fn.Labels{p("a", "b"), p("c", "d")}) assertLabel([]fn.Label{p("a", "b"), p("c", "d")})
run("add", arrowUp, arrowUp, enter, enter, "e", enter, "f", enter) run("add", arrowUp, arrowUp, enter, enter, "e", enter, "f", enter)
assertLabel(fn.Labels{p("e", "f"), p("a", "b"), p("c", "d")}) assertLabel([]fn.Label{p("e", "f"), p("a", "b"), p("c", "d")})
run("remove", arrowDown, enter) run("remove", arrowDown, enter)
assertLabel(fn.Labels{p("e", "f"), p("c", "d")}) assertLabel([]fn.Label{p("e", "f"), p("c", "d")})
} }
func TestListLabels(t *testing.T) { func TestListLabels(t *testing.T) {

View File

@ -224,7 +224,7 @@ func runRemoveVolumesPrompt(f fn.Function) (err error) {
return return
} }
var newVolumes fn.Volumes var newVolumes []fn.Volume
removed := false removed := false
for i, v := range f.Volumes { for i, v := range f.Volumes {
if v.String() == selectedVolume { if v.String() == selectedVolume {

View File

@ -299,7 +299,7 @@ func envFromCmd(cmd *cobra.Command) (*util.OrderedMap, []string, error) {
return util.NewOrderedMap(), []string{}, nil return util.NewOrderedMap(), []string{}, nil
} }
func mergeEnvs(envs fn.Envs, envToUpdate *util.OrderedMap, envToRemove []string) (fn.Envs, error) { func mergeEnvs(envs []fn.Env, envToUpdate *util.OrderedMap, envToRemove []string) ([]fn.Env, error) {
updated := sets.NewString() updated := sets.NewString()
for i := range envs { for i := range envs {
@ -332,7 +332,7 @@ func mergeEnvs(envs fn.Envs, envToUpdate *util.OrderedMap, envToRemove []string)
errMsg := fn.ValidateEnvs(envs) errMsg := fn.ValidateEnvs(envs)
if len(errMsg) > 0 { if len(errMsg) > 0 {
return fn.Envs{}, fmt.Errorf(strings.Join(errMsg, "\n")) return []fn.Env{}, fmt.Errorf(strings.Join(errMsg, "\n"))
} }
return envs, nil return envs, nil

View File

@ -18,77 +18,77 @@ func Test_mergeEnvMaps(t *testing.T) {
v2 := "y" v2 := "y"
type args struct { type args struct {
envs fn.Envs envs []fn.Env
toUpdate *util.OrderedMap toUpdate *util.OrderedMap
toRemove []string toRemove []string
} }
tests := []struct { tests := []struct {
name string name string
args args args args
want fn.Envs want []fn.Env
}{ }{
{ {
"add new var to empty list", "add new var to empty list",
args{ args{
fn.Envs{}, []fn.Env{},
util.NewOrderedMapWithKVStrings([][]string{{a, v1}}), util.NewOrderedMapWithKVStrings([][]string{{a, v1}}),
[]string{}, []string{},
}, },
fn.Envs{fn.Env{Name: &a, Value: &v1}}, []fn.Env{{Name: &a, Value: &v1}},
}, },
{ {
"add new var", "add new var",
args{ args{
fn.Envs{fn.Env{Name: &b, Value: &v2}}, []fn.Env{{Name: &b, Value: &v2}},
util.NewOrderedMapWithKVStrings([][]string{{a, v1}}), util.NewOrderedMapWithKVStrings([][]string{{a, v1}}),
[]string{}, []string{},
}, },
fn.Envs{fn.Env{Name: &b, Value: &v2}, fn.Env{Name: &a, Value: &v1}}, []fn.Env{{Name: &b, Value: &v2}, {Name: &a, Value: &v1}},
}, },
{ {
"update var", "update var",
args{ args{
fn.Envs{fn.Env{Name: &a, Value: &v1}}, []fn.Env{{Name: &a, Value: &v1}},
util.NewOrderedMapWithKVStrings([][]string{{a, v2}}), util.NewOrderedMapWithKVStrings([][]string{{a, v2}}),
[]string{}, []string{},
}, },
fn.Envs{fn.Env{Name: &a, Value: &v2}}, []fn.Env{{Name: &a, Value: &v2}},
}, },
{ {
"update multiple vars", "update multiple vars",
args{ args{
fn.Envs{fn.Env{Name: &a, Value: &v1}, fn.Env{Name: &b, Value: &v2}}, []fn.Env{{Name: &a, Value: &v1}, {Name: &b, Value: &v2}},
util.NewOrderedMapWithKVStrings([][]string{{a, v2}, {b, v1}}), util.NewOrderedMapWithKVStrings([][]string{{a, v2}, {b, v1}}),
[]string{}, []string{},
}, },
fn.Envs{fn.Env{Name: &a, Value: &v2}, fn.Env{Name: &b, Value: &v1}}, []fn.Env{{Name: &a, Value: &v2}, {Name: &b, Value: &v1}},
}, },
{ {
"remove var", "remove var",
args{ args{
fn.Envs{fn.Env{Name: &a, Value: &v1}}, []fn.Env{{Name: &a, Value: &v1}},
util.NewOrderedMap(), util.NewOrderedMap(),
[]string{a}, []string{a},
}, },
fn.Envs{}, []fn.Env{},
}, },
{ {
"remove multiple vars", "remove multiple vars",
args{ args{
fn.Envs{fn.Env{Name: &a, Value: &v1}, fn.Env{Name: &b, Value: &v2}}, []fn.Env{{Name: &a, Value: &v1}, {Name: &b, Value: &v2}},
util.NewOrderedMap(), util.NewOrderedMap(),
[]string{a, b}, []string{a, b},
}, },
fn.Envs{}, []fn.Env{},
}, },
{ {
"update and remove vars", "update and remove vars",
args{ args{
fn.Envs{fn.Env{Name: &a, Value: &v1}, fn.Env{Name: &b, Value: &v2}}, []fn.Env{{Name: &a, Value: &v1}, {Name: &b, Value: &v2}},
util.NewOrderedMapWithKVStrings([][]string{{a, v2}}), util.NewOrderedMapWithKVStrings([][]string{{a, v2}}),
[]string{b}, []string{b},
}, },
fn.Envs{fn.Env{Name: &a, Value: &v2}}, []fn.Env{{Name: &a, Value: &v2}},
}, },
} }
for _, tt := range tests { for _, tt := range tests {

View File

@ -26,7 +26,6 @@ var (
regLocalEnv = regexp.MustCompile(`^{{\s*env:(\w+)\s*}}$`) regLocalEnv = regexp.MustCompile(`^{{\s*env:(\w+)\s*}}$`)
) )
type Volumes []Volume
type Volume struct { type Volume struct {
Secret *string `yaml:"secret,omitempty" jsonschema:"oneof_required=secret"` Secret *string `yaml:"secret,omitempty" jsonschema:"oneof_required=secret"`
ConfigMap *string `yaml:"configMap,omitempty" jsonschema:"oneof_required=configmap"` ConfigMap *string `yaml:"configMap,omitempty" jsonschema:"oneof_required=configmap"`
@ -43,7 +42,6 @@ func (v Volume) String() string {
return "" return ""
} }
type Envs []Env
type Env struct { type Env struct {
Name *string `yaml:"name,omitempty" jsonschema:"pattern=^[-._a-zA-Z][-._a-zA-Z0-9]*$"` Name *string `yaml:"name,omitempty" jsonschema:"pattern=^[-._a-zA-Z][-._a-zA-Z0-9]*$"`
Value *string `yaml:"value"` Value *string `yaml:"value"`
@ -78,7 +76,6 @@ func (e Env) String() string {
return "" return ""
} }
type Labels []Label
type Label struct { type Label struct {
// Key consist of optional prefix part (ended by '/') and name part // Key consist of optional prefix part (ended by '/') and name part
// Prefix part validation pattern: [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)* // Prefix part validation pattern: [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*
@ -141,12 +138,12 @@ type Config struct {
Builders map[string]string `yaml:"builders"` Builders map[string]string `yaml:"builders"`
Buildpacks []string `yaml:"buildpacks"` Buildpacks []string `yaml:"buildpacks"`
HealthEndpoints HealthEndpoints `yaml:"healthEndpoints"` HealthEndpoints HealthEndpoints `yaml:"healthEndpoints"`
Volumes Volumes `yaml:"volumes"` Volumes []Volume `yaml:"volumes"`
BuildEnvs Envs `yaml:"buildEnvs"` BuildEnvs []Env `yaml:"buildEnvs"`
Envs Envs `yaml:"envs"` Envs []Env `yaml:"envs"`
Annotations map[string]string `yaml:"annotations"` Annotations map[string]string `yaml:"annotations"`
Options Options `yaml:"options"` Options Options `yaml:"options"`
Labels Labels `yaml:"labels"` Labels []Label `yaml:"labels"`
Created time.Time `yaml:"created"` Created time.Time `yaml:"created"`
// Add new values to the toConfig/fromConfig functions. // Add new values to the toConfig/fromConfig functions.
} }
@ -282,7 +279,7 @@ func writeConfig(f Function) (err error) {
// path: /etc/secret-volume // path: /etc/secret-volume
// - configMap: example-configMap # mount ConfigMap as Volume // - configMap: example-configMap # mount ConfigMap as Volume
// path: /etc/configMap-volume // path: /etc/configMap-volume
func validateVolumes(volumes Volumes) (errors []string) { func validateVolumes(volumes []Volume) (errors []string) {
for i, vol := range volumes { for i, vol := range volumes {
if vol.Secret != nil && vol.ConfigMap != nil { if vol.Secret != nil && vol.ConfigMap != nil {
@ -312,7 +309,7 @@ func validateVolumes(volumes Volumes) (errors []string) {
// value: value1 // value: value1
// - name: EXAMPLE2 # ENV from the local ENV var // - name: EXAMPLE2 # ENV from the local ENV var
// value: {{ env:MY_ENV }} // value: {{ env:MY_ENV }}
func ValidateBuildEnvs(envs Envs) (errors []string) { func ValidateBuildEnvs(envs []Env) (errors []string) {
for i, env := range envs { for i, env := range envs {
if env.Name == nil && env.Value == nil { if env.Name == nil && env.Value == nil {
errors = append(errors, fmt.Sprintf("env entry #%d is not properly set", i)) errors = append(errors, fmt.Sprintf("env entry #%d is not properly set", i))
@ -353,7 +350,7 @@ func ValidateBuildEnvs(envs Envs) (errors []string) {
// - name: EXAMPLE4 // - name: EXAMPLE4
// value: {{ configMap:configMapName:key }} # ENV from a key in configMap // value: {{ configMap:configMapName:key }} # ENV from a key in configMap
// - value: {{ configMap:configMapName }} # all key-pair values from configMap are set as ENV // - value: {{ configMap:configMapName }} # all key-pair values from configMap are set as ENV
func ValidateEnvs(envs Envs) (errors []string) { func ValidateEnvs(envs []Env) (errors []string) {
for i, env := range envs { for i, env := range envs {
if env.Name == nil && env.Value == nil { if env.Name == nil && env.Value == nil {
errors = append(errors, fmt.Sprintf("env entry #%d is not properly set", i)) errors = append(errors, fmt.Sprintf("env entry #%d is not properly set", i))
@ -396,7 +393,7 @@ func ValidateEnvs(envs Envs) (errors []string) {
// value: value1 // value: value1
// - key: EXAMPLE2 # label from the local ENV var // - key: EXAMPLE2 # label from the local ENV var
// value: {{ env:MY_ENV }} // value: {{ env:MY_ENV }}
func ValidateLabels(labels Labels) (errors []string) { func ValidateLabels(labels []Label) (errors []string) {
for i, label := range labels { for i, label := range labels {
if label.Key == nil && label.Value == nil { if label.Key == nil && label.Value == nil {
errors = append(errors, fmt.Sprintf("label entry #%d is not properly set", i)) errors = append(errors, fmt.Sprintf("label entry #%d is not properly set", i))

View File

@ -20,13 +20,13 @@ func Test_validateVolumes(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
volumes Volumes volumes []Volume
errs int errs int
}{ }{
{ {
"correct entry - single volume with secret", "correct entry - single volume with secret",
Volumes{ []Volume{
Volume{ {
Secret: &secret, Secret: &secret,
Path: &path, Path: &path,
}, },
@ -35,8 +35,8 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"correct entry - single volume with configmap", "correct entry - single volume with configmap",
Volumes{ []Volume{
Volume{ {
ConfigMap: &cm, ConfigMap: &cm,
Path: &path, Path: &path,
}, },
@ -45,12 +45,12 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"correct entry - multiple volumes with secrets", "correct entry - multiple volumes with secrets",
Volumes{ []Volume{
Volume{ {
Secret: &secret, Secret: &secret,
Path: &path, Path: &path,
}, },
Volume{ {
Secret: &secret2, Secret: &secret2,
Path: &path2, Path: &path2,
}, },
@ -59,12 +59,12 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"correct entry - multiple volumes with both secret and configMap", "correct entry - multiple volumes with both secret and configMap",
Volumes{ []Volume{
Volume{ {
Secret: &secret, Secret: &secret,
Path: &path, Path: &path,
}, },
Volume{ {
ConfigMap: &cm, ConfigMap: &cm,
Path: &path2, Path: &path2,
}, },
@ -73,8 +73,8 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"missing secret/configMap - single volume", "missing secret/configMap - single volume",
Volumes{ []Volume{
Volume{ {
Path: &path, Path: &path,
}, },
}, },
@ -82,8 +82,8 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"missing path - single volume with secret", "missing path - single volume with secret",
Volumes{ []Volume{
Volume{ {
Secret: &secret, Secret: &secret,
}, },
}, },
@ -91,8 +91,8 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"missing path - single volume with configMap", "missing path - single volume with configMap",
Volumes{ []Volume{
Volume{ {
ConfigMap: &cm, ConfigMap: &cm,
}, },
}, },
@ -100,19 +100,19 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"missing secret/configMap and path - single volume", "missing secret/configMap and path - single volume",
Volumes{ []Volume{
Volume{}, {},
}, },
1, 1,
}, },
{ {
"missing secret/configMap in one volume - multiple volumes", "missing secret/configMap in one volume - multiple volumes",
Volumes{ []Volume{
Volume{ {
Secret: &secret, Secret: &secret,
Path: &path, Path: &path,
}, },
Volume{ {
Path: &path2, Path: &path2,
}, },
}, },
@ -120,15 +120,15 @@ func Test_validateVolumes(t *testing.T) {
}, },
{ {
"missing secret/configMap and path in two different volumes - multiple volumes", "missing secret/configMap and path in two different volumes - multiple volumes",
Volumes{ []Volume{
Volume{ {
Secret: &secret, Secret: &secret,
Path: &path, Path: &path,
}, },
Volume{ {
Secret: &secret, Secret: &secret,
}, },
Volume{ {
Path: &path2, Path: &path2,
}, },
}, },
@ -165,13 +165,13 @@ func Test_validateBuildEnvs(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
envs Envs envs []Env
errs int errs int
}{ }{
{ {
"correct entry - single env with value", "correct entry - single env with value",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &value, Value: &value,
}, },
@ -180,8 +180,8 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - missing value", "incorrect entry - missing value",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
}, },
}, },
@ -189,8 +189,8 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - invalid name", "incorrect entry - invalid name",
Envs{ []Env{
Env{ {
Name: &incorrectName, Name: &incorrectName,
Value: &value, Value: &value,
}, },
@ -199,8 +199,8 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - invalid name2", "incorrect entry - invalid name2",
Envs{ []Env{
Env{ {
Name: &incorrectName2, Name: &incorrectName2,
Value: &value, Value: &value,
}, },
@ -209,12 +209,12 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"correct entry - multiple envs with value", "correct entry - multiple envs with value",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &value, Value: &value,
}, },
Env{ {
Name: &name2, Name: &name2,
Value: &value2, Value: &value2,
}, },
@ -223,11 +223,11 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - mmissing value - multiple envs", "incorrect entry - mmissing value - multiple envs",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
}, },
Env{ {
Name: &name2, Name: &name2,
}, },
}, },
@ -235,8 +235,8 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"correct entry - single env with value Local env", "correct entry - single env with value Local env",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
@ -245,16 +245,16 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"correct entry - multiple envs with value Local env", "correct entry - multiple envs with value Local env",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv2, Value: &valueLocalEnv2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv3, Value: &valueLocalEnv3,
}, },
@ -263,20 +263,20 @@ func Test_validateBuildEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - multiple envs with value Local env", "incorrect entry - multiple envs with value Local env",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnvIncorrect, Value: &valueLocalEnvIncorrect,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnvIncorrect2, Value: &valueLocalEnvIncorrect2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnvIncorrect3, Value: &valueLocalEnvIncorrect3,
}, },
@ -341,13 +341,13 @@ func Test_validateEnvs(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
envs Envs envs []Env
errs int errs int
}{ }{
{ {
"correct entry - single env with value", "correct entry - single env with value",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &value, Value: &value,
}, },
@ -356,8 +356,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - missing value", "incorrect entry - missing value",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
}, },
}, },
@ -365,8 +365,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - invalid name", "incorrect entry - invalid name",
Envs{ []Env{
Env{ {
Name: &incorrectName, Name: &incorrectName,
Value: &value, Value: &value,
}, },
@ -375,8 +375,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - invalid name2", "incorrect entry - invalid name2",
Envs{ []Env{
Env{ {
Name: &incorrectName2, Name: &incorrectName2,
Value: &value, Value: &value,
}, },
@ -385,12 +385,12 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - multiple envs with value", "correct entry - multiple envs with value",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &value, Value: &value,
}, },
Env{ {
Name: &name2, Name: &name2,
Value: &value2, Value: &value2,
}, },
@ -399,11 +399,11 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - mmissing value - multiple envs", "incorrect entry - mmissing value - multiple envs",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
}, },
Env{ {
Name: &name2, Name: &name2,
}, },
}, },
@ -411,8 +411,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - single env with value Local env", "correct entry - single env with value Local env",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
@ -421,16 +421,16 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - multiple envs with value Local env", "correct entry - multiple envs with value Local env",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv2, Value: &valueLocalEnv2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv3, Value: &valueLocalEnv3,
}, },
@ -439,20 +439,20 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - multiple envs with value Local env", "incorrect entry - multiple envs with value Local env",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnvIncorrect, Value: &valueLocalEnvIncorrect,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnvIncorrect2, Value: &valueLocalEnvIncorrect2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnvIncorrect3, Value: &valueLocalEnvIncorrect3,
}, },
@ -461,8 +461,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - single secret with key", "correct entry - single secret with key",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey, Value: &valueSecretKey,
}, },
@ -471,8 +471,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - single configMap with key", "correct entry - single configMap with key",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey, Value: &valueConfigMapKey,
}, },
@ -481,36 +481,36 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - multiple configMaps with key", "correct entry - multiple configMaps with key",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey, Value: &valueConfigMapKey,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey2, Value: &valueConfigMapKey2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey3, Value: &valueConfigMapKey3,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey4, Value: &valueConfigMapKey4,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey5, Value: &valueConfigMapKey5,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey6, Value: &valueConfigMapKey6,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey7, Value: &valueConfigMapKey7,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey8, Value: &valueConfigMapKey8,
}, },
@ -519,36 +519,36 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - multiple secrets with key", "correct entry - multiple secrets with key",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey, Value: &valueSecretKey,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey2, Value: &valueSecretKey2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey3, Value: &valueSecretKey3,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey4, Value: &valueSecretKey4,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey5, Value: &valueSecretKey5,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey6, Value: &valueSecretKey6,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey7, Value: &valueSecretKey7,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey8, Value: &valueSecretKey8,
}, },
@ -557,12 +557,12 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - both secret and configmap with key", "correct entry - both secret and configmap with key",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey, Value: &valueSecretKey,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey, Value: &valueConfigMapKey,
}, },
@ -571,8 +571,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - single secret with key", "incorrect entry - single secret with key",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKeyIncorrect, Value: &valueSecretKeyIncorrect,
}, },
@ -581,20 +581,20 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - mutliple secrets with key", "incorrect entry - mutliple secrets with key",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey, Value: &valueSecretKey,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKeyIncorrect, Value: &valueSecretKeyIncorrect,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKeyIncorrect2, Value: &valueSecretKeyIncorrect2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKeyIncorrect3, Value: &valueSecretKeyIncorrect3,
}, },
@ -603,8 +603,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - single whole secret", "correct entry - single whole secret",
Envs{ []Env{
Env{ {
Value: &valueSecret, Value: &valueSecret,
}, },
}, },
@ -612,8 +612,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - single whole configMap", "correct entry - single whole configMap",
Envs{ []Env{
Env{ {
Value: &valueConfigMap, Value: &valueConfigMap,
}, },
}, },
@ -621,14 +621,14 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - multiple whole secret", "correct entry - multiple whole secret",
Envs{ []Env{
Env{ {
Value: &valueSecret, Value: &valueSecret,
}, },
Env{ {
Value: &valueSecret2, Value: &valueSecret2,
}, },
Env{ {
Value: &valueSecret3, Value: &valueSecret3,
}, },
}, },
@ -636,11 +636,11 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - both whole secret and configMap", "correct entry - both whole secret and configMap",
Envs{ []Env{
Env{ {
Value: &valueSecret, Value: &valueSecret,
}, },
Env{ {
Value: &valueConfigMap, Value: &valueConfigMap,
}, },
}, },
@ -648,8 +648,8 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - single whole secret", "incorrect entry - single whole secret",
Envs{ []Env{
Env{ {
Value: &value, Value: &value,
}, },
}, },
@ -657,29 +657,29 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"incorrect entry - multiple whole secret", "incorrect entry - multiple whole secret",
Envs{ []Env{
Env{ {
Value: &valueSecretIncorrect, Value: &valueSecretIncorrect,
}, },
Env{ {
Value: &valueSecretIncorrect2, Value: &valueSecretIncorrect2,
}, },
Env{ {
Value: &valueSecretIncorrect3, Value: &valueSecretIncorrect3,
}, },
Env{ {
Value: &value, Value: &value,
}, },
Env{ {
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Env{ {
Value: &valueLocalEnv2, Value: &valueLocalEnv2,
}, },
Env{ {
Value: &valueLocalEnv3, Value: &valueLocalEnv3,
}, },
Env{ {
Value: &valueSecret, Value: &valueSecret,
}, },
}, },
@ -687,52 +687,52 @@ func Test_validateEnvs(t *testing.T) {
}, },
{ {
"correct entry - all combinations", "correct entry - all combinations",
Envs{ []Env{
Env{ {
Name: &name, Name: &name,
Value: &value, Value: &value,
}, },
Env{ {
Name: &name2, Name: &name2,
Value: &value2, Value: &value2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv2, Value: &valueLocalEnv2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueLocalEnv3, Value: &valueLocalEnv3,
}, },
Env{ {
Value: &valueSecret, Value: &valueSecret,
}, },
Env{ {
Value: &valueSecret2, Value: &valueSecret2,
}, },
Env{ {
Value: &valueSecret3, Value: &valueSecret3,
}, },
Env{ {
Value: &valueConfigMap, Value: &valueConfigMap,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey, Value: &valueSecretKey,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey2, Value: &valueSecretKey2,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueSecretKey3, Value: &valueSecretKey3,
}, },
Env{ {
Name: &name, Name: &name,
Value: &valueConfigMapKey, Value: &valueConfigMapKey,
}, },
@ -779,13 +779,13 @@ func Test_validateLabels(t *testing.T) {
tests := []struct { tests := []struct {
key string key string
labels Labels labels []Label
errs int errs int
}{ }{
{ {
"correct entry - single label with value", "correct entry - single label with value",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &value, Value: &value,
}, },
@ -794,8 +794,8 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"correct entry - prefixed label with value", "correct entry - prefixed label with value",
Labels{ []Label{
Label{ {
Key: &key3, Key: &key3,
Value: &value3, Value: &value3,
}, },
@ -803,19 +803,19 @@ func Test_validateLabels(t *testing.T) {
0, 0,
}, { }, {
"incorrect entry - missing key", "incorrect entry - missing key",
Labels{ []Label{
Label{ {
Value: &value, Value: &value,
}, },
}, },
1, 1,
}, { }, {
"incorrect entry - missing multiple keys", "incorrect entry - missing multiple keys",
Labels{ []Label{
Label{ {
Value: &value, Value: &value,
}, },
Label{ {
Value: &value2, Value: &value2,
}, },
}, },
@ -823,8 +823,8 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"incorrect entry - invalid key", "incorrect entry - invalid key",
Labels{ []Label{
Label{ {
Key: &incorrectKey, Key: &incorrectKey,
Value: &value, Value: &value,
}, },
@ -833,8 +833,8 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"incorrect entry - invalid key2", "incorrect entry - invalid key2",
Labels{ []Label{
Label{ {
Key: &incorrectKey2, Key: &incorrectKey2,
Value: &value, Value: &value,
}, },
@ -843,8 +843,8 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"incorrect entry - invalid value", "incorrect entry - invalid value",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &incorrectValue, Value: &incorrectValue,
}, },
@ -853,12 +853,12 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"correct entry - multiple labels with value", "correct entry - multiple labels with value",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &value, Value: &value,
}, },
Label{ {
Key: &key2, Key: &key2,
Value: &value2, Value: &value2,
}, },
@ -867,11 +867,11 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"correct entry - missing value - multiple labels", "correct entry - missing value - multiple labels",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
}, },
Label{ {
Key: &key2, Key: &key2,
}, },
}, },
@ -879,8 +879,8 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"correct entry - single label with value from local env", "correct entry - single label with value from local env",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
@ -889,16 +889,16 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"correct entry - multiple labels with values from Local env", "correct entry - multiple labels with values from Local env",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv2, Value: &valueLocalEnv2,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv3, Value: &valueLocalEnv3,
}, },
@ -907,20 +907,20 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"incorrect entry - multiple labels with values from Local env", "incorrect entry - multiple labels with values from Local env",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnvIncorrect, Value: &valueLocalEnvIncorrect,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnvIncorrect2, Value: &valueLocalEnvIncorrect2,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnvIncorrect3, Value: &valueLocalEnvIncorrect3,
}, },
@ -929,8 +929,8 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"correct entry - good environment variable value", "correct entry - good environment variable value",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv4, Value: &valueLocalEnv4,
}, },
@ -938,8 +938,8 @@ func Test_validateLabels(t *testing.T) {
0, 0,
}, { }, {
"incorrect entry - bad environment variable value", "incorrect entry - bad environment variable value",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnvIncorrect4, Value: &valueLocalEnvIncorrect4,
}, },
@ -948,28 +948,28 @@ func Test_validateLabels(t *testing.T) {
}, },
{ {
"correct entry - all combinations", "correct entry - all combinations",
Labels{ []Label{
Label{ {
Key: &key, Key: &key,
Value: &value, Value: &value,
}, },
Label{ {
Key: &key2, Key: &key2,
Value: &value2, Value: &value2,
}, },
Label{ {
Key: &key3, Key: &key3,
Value: &value3, Value: &value3,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv, Value: &valueLocalEnv,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv2, Value: &valueLocalEnv2,
}, },
Label{ {
Key: &key, Key: &key,
Value: &valueLocalEnv3, Value: &valueLocalEnv3,
}, },

View File

@ -56,13 +56,13 @@ type Function struct {
Buildpacks []string Buildpacks []string
// List of volumes to be mounted to the function // List of volumes to be mounted to the function
Volumes Volumes Volumes []Volume
// Build Env variables to be set // Build Env variables to be set
BuildEnvs Envs BuildEnvs []Env
// Env variables to be set // Env variables to be set
Envs Envs Envs []Env
// Map containing user-supplied annotations // Map containing user-supplied annotations
// Example: { "division": "finance" } // Example: { "division": "finance" }
@ -72,7 +72,7 @@ type Function struct {
Options Options Options Options
// Map of user-supplied labels // Map of user-supplied labels
Labels Labels Labels []Label
// Health endpoints specified by the language pack // Health endpoints specified by the language pack
HealthEndpoints HealthEndpoints HealthEndpoints HealthEndpoints

View File

@ -390,7 +390,7 @@ func processLabels(f fn.Function) (map[string]string, error) {
// - name: EXAMPLE4 // - name: EXAMPLE4
// value: {{ configMap:configMapName:key }} # ENV from a key in ConfigMap // value: {{ configMap:configMapName:key }} # ENV from a key in ConfigMap
// - value: {{ configMap:configMapName }} # all key-pair values from ConfigMap are set as ENV // - value: {{ configMap:configMapName }} # all key-pair values from ConfigMap are set as ENV
func processEnvs(envs fn.Envs, referencedSecrets, referencedConfigMaps *sets.String) ([]corev1.EnvVar, []corev1.EnvFromSource, error) { func processEnvs(envs []fn.Env, referencedSecrets, referencedConfigMaps *sets.String) ([]corev1.EnvVar, []corev1.EnvFromSource, error) {
envVars := []corev1.EnvVar{{Name: "BUILT", Value: time.Now().Format("20060102T150405")}} envVars := []corev1.EnvVar{{Name: "BUILT", Value: time.Now().Format("20060102T150405")}}
envFrom := []corev1.EnvFromSource{} envFrom := []corev1.EnvFromSource{}
@ -563,7 +563,7 @@ func processLocalEnvValue(val string) (string, error) {
// path: /etc/secret-volume // path: /etc/secret-volume
// - configMap: example-cm # mount ConfigMap as Volume // - configMap: example-cm # mount ConfigMap as Volume
// path: /etc/cm-volume // path: /etc/cm-volume
func processVolumes(volumes fn.Volumes, referencedSecrets, referencedConfigMaps *sets.String) ([]corev1.Volume, []corev1.VolumeMount, error) { func processVolumes(volumes []fn.Volume, referencedSecrets, referencedConfigMaps *sets.String) ([]corev1.Volume, []corev1.VolumeMount, error) {
createdVolumes := sets.NewString() createdVolumes := sets.NewString()
usedPaths := sets.NewString() usedPaths := sets.NewString()

View File

@ -1,5 +1,6 @@
// Code generated by pkger; DO NOT EDIT. // Code generated by pkger; DO NOT EDIT.
//go:build !skippkger
// +build !skippkger // +build !skippkger
package function package function