Skip to content

Commit 056d5c0

Browse files
committed
[confmap] Add Conf.Delete method
1 parent ee2c784 commit 056d5c0

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add a `Conf.Delete` method to remove a path from the configuration map.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13064]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

confmap/confmap.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ func (l *Conf) Merge(in *Conf) error {
181181
return l.k.Merge(in.k)
182182
}
183183

184+
// Delete a path from the Conf.
185+
// Return true if the path is deleted.
186+
// If the path is not set (so the operation is a no-op), it returns false.
187+
func (l *Conf) Delete(key string) bool {
188+
wasSet := l.IsSet(key)
189+
l.k.Delete(key)
190+
return wasSet
191+
}
192+
184193
// mergeAppend merges the input given configuration into the existing config.
185194
// Note that the given map may be modified.
186195
// Additionally, mergeAppend performs deduplication when merging lists.

confmap/confmap_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,3 +1053,40 @@ func TestStringyTypes(t *testing.T) {
10531053
assert.Equal(t, tt.isStringy, isStringyStructure(to))
10541054
}
10551055
}
1056+
1057+
func TestConfDelete(t *testing.T) {
1058+
tests := []struct {
1059+
path string
1060+
stringMap map[string]any
1061+
}{
1062+
{
1063+
path: "key",
1064+
stringMap: map[string]any{"key": "value"},
1065+
},
1066+
{
1067+
path: "map::expanded",
1068+
stringMap: map[string]any{"map": map[string]any{
1069+
"expanded": expandedValue{
1070+
Value: 01234,
1071+
Original: "01234",
1072+
}}},
1073+
},
1074+
}
1075+
1076+
for _, test := range tests {
1077+
t.Run(test.path, func(t *testing.T) {
1078+
cm := NewFromStringMap(test.stringMap)
1079+
1080+
assert.True(t, cm.IsSet(test.path))
1081+
deleted := cm.Delete(test.path)
1082+
assert.Nil(t, cm.Get(test.path))
1083+
assert.True(t, deleted)
1084+
assert.False(t, cm.IsSet(test.path))
1085+
1086+
deleted = cm.Delete(test.path)
1087+
assert.Nil(t, cm.Get(test.path))
1088+
assert.False(t, deleted)
1089+
assert.False(t, cm.IsSet(test.path))
1090+
})
1091+
}
1092+
}

0 commit comments

Comments
 (0)