Skip to content

Commit 806e635

Browse files
Merge pull request #513 from AamnaZahid/patch-1
Update configuration_metadata.go
2 parents 84d2bef + 7e12f5c commit 806e635

File tree

2 files changed

+100
-26
lines changed

2 files changed

+100
-26
lines changed

boot/configuration_metadata.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"archive/zip"
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"strings"
@@ -135,10 +134,10 @@ func DataFlowConfigurationExists(path string) (bool, error) {
135134

136135
func NewDataFlowConfigurationMetadata(path string, metadata ConfigurationMetadata) (ConfigurationMetadata, error) {
137136
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties")
138-
b, err := ioutil.ReadFile(file)
137+
b, err := os.ReadFile(file)
139138
if os.IsNotExist(err) {
140139
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties")
141-
b, err = ioutil.ReadFile(file)
140+
b, err = os.ReadFile(file)
142141
if os.IsNotExist(err) {
143142
return ConfigurationMetadata{}, nil
144143
} else if err != nil {
@@ -153,34 +152,49 @@ func NewDataFlowConfigurationMetadata(path string, metadata ConfigurationMetadat
153152
return ConfigurationMetadata{}, fmt.Errorf("unable to load properties from %s\n%w", file, err)
154153
}
155154

155+
// Load classes
156156
s, ok := p.Get("configuration-properties.classes")
157-
if !ok {
158-
return ConfigurationMetadata{}, nil
157+
var classes []string
158+
if ok {
159+
for _, s := range strings.Split(s, ",") {
160+
class := strings.TrimSpace(s)
161+
if class != "" {
162+
classes = append(classes, class)
163+
}
164+
}
159165
}
160166

161-
var classes []string
162-
for _, s := range strings.Split(s, ",") {
163-
class := strings.TrimSpace(s)
164-
if class == "" {
165-
continue
167+
// Load names
168+
s, ok = p.Get("configuration-properties.names")
169+
var names []string
170+
if ok {
171+
for _, s := range strings.Split(s, ",") {
172+
name := strings.TrimSpace(s)
173+
if name != "" {
174+
names = append(names, name)
175+
}
166176
}
167-
classes = append(classes, class)
168177
}
169178

179+
// Merge classes and names
180+
var combined []string
181+
combined = append(combined, classes...)
182+
combined = append(combined, names...)
183+
170184
m := ConfigurationMetadata{}
171185

172186
for _, g := range metadata.Groups {
173-
for _, c := range classes {
174-
if c == g.SourceType {
187+
for _, c := range combined {
188+
if c == g.SourceType || c == g.Name {
175189
m.Groups = append(m.Groups, g)
176190
break
177191
}
178192
}
179193
}
180194

181195
for _, p := range metadata.Properties {
182-
for _, c := range classes {
183-
if c == p.SourceType {
196+
for _, c := range combined {
197+
if c == p.SourceType || c == p.Name {
184198
m.Properties = append(m.Properties, p)
185199
break
186200
}

boot/configuration_metadata_test.go

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package boot_test
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"testing"
@@ -38,7 +37,7 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
3837
it.Before(func() {
3938
var err error
4039

41-
path, err = ioutil.TempDir("", "configuration-metadata")
40+
path, err = os.MkdirTemp("", "configuration-metadata")
4241
Expect(err).NotTo(HaveOccurred())
4342
})
4443

@@ -54,7 +53,7 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
5453

5554
it("returns decoded contents", func() {
5655
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
57-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
56+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
5857
[]byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed())
5958

6059
Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{
@@ -64,9 +63,9 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
6463

6564
it("returns dataflow decoded contents", func() {
6665
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
67-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
66+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
6867
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644))
69-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
68+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
7069
[]byte("configuration-properties.classes=alpha"), 0644))
7170

7271
cm, err := boot.NewConfigurationMetadataFromPath(path)
@@ -79,9 +78,9 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
7978

8079
it("returns dataflow decoded contents handling trailing comma correctly", func() {
8180
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
82-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
81+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
8382
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644))
84-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
83+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
8584
[]byte("configuration-properties.classes=alpha,"), 0644))
8685

8786
cm, err := boot.NewConfigurationMetadataFromPath(path)
@@ -94,9 +93,9 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
9493

9594
it("returns dataflow decoded contents", func() {
9695
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
97-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
96+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
9897
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
99-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
98+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
10099
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
101100

102101
cm, err := boot.NewConfigurationMetadataFromPath(path)
@@ -106,6 +105,67 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
106105
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
107106
}))
108107
})
108+
109+
it("returns dataflow decoded contents with names", func() {
110+
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
111+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
112+
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
113+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
114+
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())
115+
116+
cm, err := boot.NewConfigurationMetadataFromPath(path)
117+
Expect(err).NotTo(HaveOccurred())
118+
119+
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
120+
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
121+
}))
122+
})
123+
124+
it("returns combined dataflow decoded contents with classes and names", func() {
125+
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
126+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
127+
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
128+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
129+
[]byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed())
130+
131+
cm, err := boot.NewConfigurationMetadataFromPath(path)
132+
Expect(err).NotTo(HaveOccurred())
133+
134+
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
135+
Groups: []boot.Group{
136+
{Name: "alpha", SourceType: "alpha"},
137+
{Name: "beta", SourceType: "beta"},
138+
},
139+
}))
140+
})
141+
142+
it("handles missing names properly", func() {
143+
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
144+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
145+
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
146+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
147+
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())
148+
149+
cm, err := boot.NewConfigurationMetadataFromPath(path)
150+
Expect(err).NotTo(HaveOccurred())
151+
152+
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
153+
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
154+
}))
155+
})
156+
157+
it("handles empty names gracefully", func() {
158+
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
159+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
160+
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
161+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
162+
[]byte("configuration-properties.names="), 0644)).To(Succeed())
163+
164+
cm, err := boot.NewConfigurationMetadataFromPath(path)
165+
Expect(err).NotTo(HaveOccurred())
166+
167+
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{}))
168+
})
109169
})
110170

111171
context("from JAR", func() {
@@ -132,14 +192,14 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
132192

133193
it("returns true if the file does exist", func() {
134194
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
135-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
195+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
136196
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
137197
Expect(boot.DataFlowConfigurationExists(path)).To(BeFalse())
138198
})
139199

140200
it("return false and the error if the file cannot be read", func() {
141201
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
142-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
202+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
143203
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
144204

145205
Expect(os.Chmod(filepath.Join(path, "META-INF"), 0000)).To(Succeed())

0 commit comments

Comments
 (0)