From 317b7c46287234eac393509dd3a46536821e05a6 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Fri, 27 Oct 2023 10:18:42 +0200 Subject: [PATCH] label can be set with empty value Signed-off-by: Nicolas De Loof --- types/labels.go | 5 +---- types/labels_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 types/labels_test.go diff --git a/types/labels.go b/types/labels.go index f30699b5..000476bf 100644 --- a/types/labels.go +++ b/types/labels.go @@ -66,10 +66,7 @@ func (l *Labels) DecodeMapstructure(value interface{}) error { case []interface{}: labels := make(map[string]string, len(v)) for _, s := range v { - k, e, ok := strings.Cut(fmt.Sprint(s), "=") - if !ok { - return fmt.Errorf("invalid label %q", v) - } + k, e, _ := strings.Cut(fmt.Sprint(s), "=") labels[k] = labelValue(e) } *l = labels diff --git a/types/labels_test.go b/types/labels_test.go new file mode 100644 index 00000000..9a4bf5c5 --- /dev/null +++ b/types/labels_test.go @@ -0,0 +1,34 @@ +/* + Copyright 2020 The Compose Specification Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package types + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestDecodeLabel(t *testing.T) { + l := Labels{} + err := l.DecodeMapstructure([]any{ + "a=b", + "c", + }) + assert.NilError(t, err) + assert.Equal(t, l["a"], "b") + assert.Equal(t, l["c"], "") +}