Skip to content

Commit bc54fc3

Browse files
xianingawa-wqclaude
andcommitted
fix(metadata): update tests and comment to match default fallback in GetMetadataReportByRegistry
GetMetadataReportByRegistry intentionally falls back to the 'default' report when a specific registryId is not found. This supports standalone metadata-report configs registered under 'default' being used by named registries (e.g. nacos, zk). Update tests and doc comment to reflect this behavior instead of the old 'return nil for unknown id' contract. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent aec06a0 commit bc54fc3

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

metadata/client_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,18 @@ func TestGetMetadataFromMetadataReport(t *testing.T) {
9999
assert.Equal(t, metadataInfo, got)
100100
})
101101

102-
t.Run("unknown registryId returns error instead of silently using wrong report", func(t *testing.T) {
102+
t.Run("unknown registryId falls back to default report", func(t *testing.T) {
103103
instances = make(map[string]report.MetadataReport)
104104
defaultReport := new(mockMetadataReport)
105105
defer defaultReport.AssertExpectations(t)
106106
instances["default"] = defaultReport
107107

108-
// defaultReport must NOT be called — GetMetadataReportByRegistry returns nil for unknown id
109-
_, err := GetMetadataFromMetadataReport("1", ins, "nonexistent-registry")
110-
require.Error(t, err, "should error when the specific registryId has no registered report")
108+
// When the specific registryId is not found, it falls back to "default"
109+
// so the default report's GetAppMetadata is called
110+
defaultReport.On("GetAppMetadata").Return(metadataInfo, nil).Once()
111+
got, err := GetMetadataFromMetadataReport("1", ins, "nonexistent-registry")
112+
require.NoError(t, err)
113+
assert.Equal(t, metadataInfo, got)
111114
})
112115

113116
t.Run("report error propagated", func(t *testing.T) {

metadata/report_instance.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ func GetMetadataReport() report.MetadataReport {
7878
}
7979

8080
// GetMetadataReportByRegistry returns the metadata report bound to the given
81-
// registry id. When registry is empty the caller has no registry context, so
82-
// the stable default returned by GetMetadataReport is used. When a specific
83-
// (non-empty) registry id is given but is not registered, nil is returned so
84-
// the caller receives an explicit failure rather than silently operating
85-
// against the wrong registry's report.
81+
// registry id. When the registry id is empty the caller has no registry context,
82+
// so the stable default returned by GetMetadataReport is used. When a specific
83+
// (non-empty) registry id is not found, it falls back to the "default" report
84+
// if one exists. This handles the common case where a standalone metadata-report
85+
// config is registered under "default" while named registries (e.g. nacos, zk)
86+
// need to use it. nil is returned only when neither the specific id nor "default"
87+
// is registered.
8688
func GetMetadataReportByRegistry(registry string) report.MetadataReport {
8789
if len(registry) == 0 {
8890
return GetMetadataReport()

metadata/report_instance_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ func TestGetMetadataReportByRegistry(t *testing.T) {
211211
assert.Equal(t, defaultReport, GetMetadataReportByRegistry("default"))
212212
// empty string → no registry context → falls through to GetMetadataReport() → "default"
213213
assert.Equal(t, defaultReport, GetMetadataReportByRegistry(""))
214-
// specific but unknown id → nil (not a silent wrong-registry fallback)
215-
assert.Nil(t, GetMetadataReportByRegistry("reg"))
214+
// specific but unknown id → falls back to "default"
215+
assert.Equal(t, defaultReport, GetMetadataReportByRegistry("reg"))
216216
}
217217

218218
func TestGetMetadataReportByRegistryFallsBackDeterministically(t *testing.T) {
@@ -226,7 +226,7 @@ func TestGetMetadataReportByRegistryFallsBackDeterministically(t *testing.T) {
226226
assert.Equal(t, rA, GetMetadataReportByRegistry("aaa"))
227227
assert.Equal(t, rB, GetMetadataReportByRegistry("zzz"))
228228

229-
// unknown specific id → nil, not the lex-first fallback
229+
// unknown specific id → nil when no "default" is registered
230230
assert.Nil(t, GetMetadataReportByRegistry("unknown-registry"))
231231

232232
// empty string → falls through to GetMetadataReport() → lex-first ("aaa" → rA)

0 commit comments

Comments
 (0)