Skip to content

Commit 3718c19

Browse files
committed
name external resources if no explicit name is set
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent bb9c98a commit 3718c19

File tree

2 files changed

+95
-19
lines changed

2 files changed

+95
-19
lines changed

transform/external.go

+12-19
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,21 @@ func transformMaybeExternal(data any, p tree.Path) (any, error) {
3333
}
3434

3535
if ext, ok := resource["external"]; ok {
36-
external, ok := ext.(map[string]any)
37-
if !ok {
38-
return resource, nil
39-
}
4036
name, named := resource["name"]
41-
extname, extNamed := external["name"]
42-
if extNamed {
43-
logrus.Warnf("%s: external.name is deprecated. Please set name and external: true", p)
44-
if named && extname != name {
45-
return nil, fmt.Errorf("%s: name and external.name conflict; only use name", p)
46-
}
47-
}
48-
if !named {
49-
if extNamed {
50-
// adopt (deprecated) external.name if set
51-
resource["name"] = extname
52-
} else {
53-
// otherwise, just replicate the mapping key for convenience
54-
resource["name"] = p
37+
if external, ok := ext.(map[string]any); ok {
38+
resource["external"] = true
39+
if extname, extNamed := external["name"]; extNamed {
40+
logrus.Warnf("%s: external.name is deprecated. Please set name and external: true", p)
41+
if named && extname != name {
42+
return nil, fmt.Errorf("%s: name and external.name conflict; only use name", p)
43+
}
44+
if !named {
45+
// adopt (deprecated) external.name if set
46+
resource["name"] = extname
47+
return resource, nil
48+
}
5549
}
5650
}
57-
resource["external"] = true
5851
}
5952

6053
return resource, nil

transform/external_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package transform
18+
19+
import (
20+
"testing"
21+
22+
"github.com/compose-spec/compose-go/v2/tree"
23+
"gotest.tools/v3/assert"
24+
)
25+
26+
func TestNotExternal(t *testing.T) {
27+
ssh, err := transformMaybeExternal(map[string]any{
28+
"driver": "foo",
29+
}, tree.NewPath("resources.test"))
30+
assert.NilError(t, err)
31+
assert.DeepEqual(t, ssh, map[string]any{
32+
"driver": "foo",
33+
})
34+
}
35+
36+
func TestExternalNamed(t *testing.T) {
37+
ssh, err := transformMaybeExternal(map[string]any{
38+
"external": true,
39+
"name": "foo",
40+
}, tree.NewPath("resources.test"))
41+
assert.NilError(t, err)
42+
assert.DeepEqual(t, ssh, map[string]any{
43+
"external": true,
44+
"name": "foo",
45+
})
46+
}
47+
48+
func TestExternalUnnamed(t *testing.T) {
49+
ssh, err := transformMaybeExternal(map[string]any{
50+
"external": true,
51+
}, tree.NewPath("resources.test"))
52+
assert.NilError(t, err)
53+
assert.DeepEqual(t, ssh, map[string]any{
54+
"external": true,
55+
})
56+
}
57+
58+
func TestExternalLegacy(t *testing.T) {
59+
ssh, err := transformMaybeExternal(map[string]any{
60+
"external": map[string]any{
61+
"name": "foo",
62+
},
63+
}, tree.NewPath("resources.test"))
64+
assert.NilError(t, err)
65+
assert.DeepEqual(t, ssh, map[string]any{
66+
"external": true,
67+
"name": "foo",
68+
})
69+
}
70+
71+
func TestExternalLegacyNamed(t *testing.T) {
72+
ssh, err := transformMaybeExternal(map[string]any{
73+
"external": map[string]any{
74+
"name": "foo",
75+
},
76+
"name": "foo",
77+
}, tree.NewPath("resources.test"))
78+
assert.NilError(t, err)
79+
assert.DeepEqual(t, ssh, map[string]any{
80+
"external": true,
81+
"name": "foo",
82+
})
83+
}

0 commit comments

Comments
 (0)