Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions sourcefile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,24 @@ func adaptFlattenedKeys(flattened map[string]any, originalKeys map[string]string

for key, value := range flattened {
adaptedKey := adaptKeyShape(key, opts)
if _, exists := adapted[adaptedKey]; exists {
return nil, nil, fmt.Errorf("sourcefile: adapted key collision for %q", adaptedKey)
currentOriginal := originalKeys[key]
if currentOriginal == "" {
currentOriginal = key
Comment thread
asadijabar marked this conversation as resolved.
}

if existingOriginal, exists := adaptedOriginalKeys[adaptedKey]; exists {
if existingOriginal == "" {
existingOriginal = adaptedKey
Comment thread
asadijabar marked this conversation as resolved.
Outdated
}
return nil, nil, fmt.Errorf(
"sourcefile: adapted key collision for %q (from %q and %q)",
adaptedKey,
existingOriginal,
currentOriginal,
)
}
adapted[adaptedKey] = value
adaptedOriginalKeys[adaptedKey] = originalKeys[key]
adaptedOriginalKeys[adaptedKey] = currentOriginal
}

return adapted, adaptedOriginalKeys, nil
Expand Down
28 changes: 28 additions & 0 deletions sourcefile/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ root:
}
}

func TestFileSource_Load_Root_KeyAdaptationCollision(t *testing.T) {
tmpDir := t.TempDir()
yamlFile := filepath.Join(tmpDir, "config.yaml")
yamlContent := `
root:
section:
apiKey: first
api_key: second
`
err := os.WriteFile(yamlFile, []byte(yamlContent), 0644)
require.NoError(t, err)

src := New(yamlFile, Options{
Root: "root.section",
SnakeCaseKeys: true,
})
srcWithKeys, ok := src.(rigging.SourceWithKeys)
require.True(t, ok, "source does not implement rigging.SourceWithKeys")

data, originalKeys, err := srcWithKeys.LoadWithKeys(context.Background())
require.Error(t, err)
assert.Nil(t, data)
assert.Nil(t, originalKeys)
assert.Contains(t, err.Error(), `sourcefile: adapted key collision for "api_key"`)
assert.Contains(t, err.Error(), "root.section.apiKey")
assert.Contains(t, err.Error(), "root.section.api_key")
}

func TestFileSource_Load_RootErrors(t *testing.T) {
tmpDir := t.TempDir()
yamlFile := filepath.Join(tmpDir, "config.yaml")
Expand Down