Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 5 additions & 10 deletions internal/pathmeta/vfox_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,23 +367,18 @@ func (t *ToolConfig) MarshalInline() string {
return fmt.Sprintf("{%s}", strings.Join(parts, ", "))
}

// formatAttributeValue formats an attribute value based on its type
// formatAttributeValue formats an attribute value based on its type.
// Only booleans and canonical integers are written bare; every other value is
// quoted, because a bare "17.0", "007" or "nan" would be read back as a
// different value (or fail to parse) instead of the string it was.
func formatAttributeValue(value string) string {
// Check if it's a boolean
if value == "true" || value == "false" {
return value
}

// Check if it's an integer
if _, err := strconv.ParseInt(value, 10, 64); err == nil {
if n, err := strconv.ParseInt(value, 10, 64); err == nil && strconv.FormatInt(n, 10) == value {
return value
}

// Check if it's a float
if _, err := strconv.ParseFloat(value, 64); err == nil {
return value
}

// Default: quote as string
return strconv.Quote(value)
}
28 changes: 28 additions & 0 deletions internal/pathmeta/vfox_toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,31 @@ func TestToolConfig_MarshalInline(t *testing.T) {
})
}
}

// String attributes that merely look like numbers must survive a save and
// reload unchanged, and must never produce a file that cannot be parsed.
func TestVfoxToml_StringAttrRoundTrip(t *testing.T) {
values := []string{"17.0", "1.50", "007", "1e3", "1_000", "+5", "nan", "inf", "21", "true", "openjdk"}
for _, value := range values {
t.Run(value, func(t *testing.T) {
dir := t.TempDir()
config := NewVfoxToml()
config.SetToolWithAttr("java", "21", Attr{"dist": value})
if err := config.SaveTo(dir); err != nil {
t.Fatalf("SaveTo() error: %v", err)
}

loaded, err := LoadVfoxToml(DetermineConfigPath(dir))
if err != nil {
t.Fatalf("LoadVfoxToml() error: %v", err)
}
tool, ok := loaded.GetToolConfig("java")
if !ok {
t.Fatal("java not found after reload")
}
if got := tool.Attr["dist"]; got != value {
t.Errorf("dist = %q after round trip, want %q", got, value)
}
})
}
}
Loading