Accept config files without mappings as long as mappings are loaded in general
Change-Id: Ib16454b83c981aa0b93e46ee1bee15599deda027
diff --git a/config/config.go b/config/config.go
index e029c63..b44259b 100644
--- a/config/config.go
+++ b/config/config.go
@@ -64,8 +64,8 @@
}
// Try to unmarshal as new format first (object with optional sdk/server and lists)
- if err := yaml.Unmarshal(data, &globalConfig); err == nil && len(globalConfig.Lists) > 0 {
- // Successfully parsed as new format with lists field
+ if err := yaml.Unmarshal(data, &globalConfig); err == nil {
+ // Successfully parsed as new format - accept it regardless of whether it has lists
for _, list := range globalConfig.Lists {
if seenIDs[list.ID] {
return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID)
diff --git a/config/config_test.go b/config/config_test.go
index a3f1d0c..329712c 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -200,7 +200,7 @@
id: test
mappings:
- "[A] <> [B]"`,
- wantErr: "cannot unmarshal",
+ wantErr: "no mapping lists found",
},
{
name: "Missing required fields",
@@ -797,3 +797,52 @@
require.Error(t, err)
assert.Contains(t, err.Error(), "duplicate mapping list ID found: duplicate-id")
}
+
+func TestLoadFromSourcesConfigWithOnlyPort(t *testing.T) {
+ // Create config file with only port (no lists)
+ configContent := `
+port: 8080
+loglevel: debug
+`
+ configFile, err := os.CreateTemp("", "config-*.yaml")
+ require.NoError(t, err)
+ defer os.Remove(configFile.Name())
+
+ _, err = configFile.WriteString(configContent)
+ require.NoError(t, err)
+ err = configFile.Close()
+ require.NoError(t, err)
+
+ // Create mapping file
+ mappingContent := `
+id: test-mapper
+mappings:
+ - "[A] <> [B]"
+`
+ mappingFile, err := os.CreateTemp("", "mapping-*.yaml")
+ require.NoError(t, err)
+ defer os.Remove(mappingFile.Name())
+
+ _, err = mappingFile.WriteString(mappingContent)
+ require.NoError(t, err)
+ err = mappingFile.Close()
+ require.NoError(t, err)
+
+ // This should work: config file has only port, mapping file provides the mapping list
+ config, err := LoadFromSources(configFile.Name(), []string{mappingFile.Name()})
+ require.NoError(t, err)
+ require.NotNil(t, config)
+
+ // Check that port and log level from config file are preserved
+ assert.Equal(t, 8080, config.Port)
+ assert.Equal(t, "debug", config.LogLevel)
+
+ // Check that mapping from mapping file is loaded
+ require.Len(t, config.Lists, 1)
+ assert.Equal(t, "test-mapper", config.Lists[0].ID)
+
+ // Check that defaults are applied for other fields
+ assert.Equal(t, defaultSDK, config.SDK)
+ assert.Equal(t, defaultServer, config.Server)
+ assert.Equal(t, defaultServiceURL, config.ServiceURL)
+}