Skip to content

Commit 83923c7

Browse files
committed
lnwire: add MergedCopy method to CustomRecords
1 parent 0c6a155 commit 83923c7

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

lnwire/custom_records.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ func (c CustomRecords) Copy() CustomRecords {
8989
return customRecords
9090
}
9191

92+
// MergedCopy creates a copy of the records and merges them with the given
93+
// records. If the same key is present in both sets, the value from the other
94+
// records will be used.
95+
func (c CustomRecords) MergedCopy(other CustomRecords) CustomRecords {
96+
copiedRecords := make(CustomRecords, len(c))
97+
for k, v := range c {
98+
copiedRecords[k] = v
99+
}
100+
101+
for k, v := range other {
102+
copiedRecords[k] = v
103+
}
104+
105+
return copiedRecords
106+
}
107+
92108
// ExtendRecordProducers extends the given records slice with the custom
93109
// records. The resultant records slice will be sorted if the given records
94110
// slice contains TLV types greater than or equal to MinCustomRecordsTlvType.

lnwire/custom_records_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/lightningnetwork/lnd/fn"
88
"github.com/lightningnetwork/lnd/tlv"
9+
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011
)
1112

@@ -194,3 +195,54 @@ func serializeRecordProducers(t *testing.T,
194195

195196
return b.Bytes()
196197
}
198+
199+
func TestCustomRecordsMergedCopy(t *testing.T) {
200+
tests := []struct {
201+
name string
202+
c CustomRecords
203+
other CustomRecords
204+
want CustomRecords
205+
}{
206+
{
207+
name: "nil records",
208+
want: make(CustomRecords),
209+
},
210+
{
211+
name: "empty records",
212+
c: make(CustomRecords),
213+
other: make(CustomRecords),
214+
want: make(CustomRecords),
215+
},
216+
{
217+
name: "distinct records",
218+
c: CustomRecords{
219+
1: {1, 2, 3},
220+
},
221+
other: CustomRecords{
222+
2: {4, 5, 6},
223+
},
224+
want: CustomRecords{
225+
1: {1, 2, 3},
226+
2: {4, 5, 6},
227+
},
228+
},
229+
{
230+
name: "same records, different values",
231+
c: CustomRecords{
232+
1: {1, 2, 3},
233+
},
234+
other: CustomRecords{
235+
1: {4, 5, 6},
236+
},
237+
want: CustomRecords{
238+
1: {4, 5, 6},
239+
},
240+
},
241+
}
242+
for _, tt := range tests {
243+
t.Run(tt.name, func(t *testing.T) {
244+
result := tt.c.MergedCopy(tt.other)
245+
assert.Equal(t, tt.want, result)
246+
})
247+
}
248+
}

0 commit comments

Comments
 (0)