From 3769ce52683a5cdbce7396b52a3070fcf5decb0f Mon Sep 17 00:00:00 2001 From: Briheet Singh Yadav Date: Tue, 6 Aug 2024 15:54:06 +0530 Subject: [PATCH 1/2] extra line bug kcl.mod Signed-off-by: Briheet Singh Yadav --- pkg/package/toml.go | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/pkg/package/toml.go b/pkg/package/toml.go index b966663e..76569b8d 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -38,10 +38,16 @@ const NEWLINE = "\n" func (mod *ModFile) MarshalTOML() string { var sb strings.Builder sb.WriteString(mod.Pkg.MarshalTOML()) - sb.WriteString(NEWLINE) - sb.WriteString(mod.Dependencies.MarshalTOML()) - sb.WriteString(NEWLINE) - sb.WriteString(mod.Profiles.MarshalTOML()) + dependencies := mod.Dependencies.MarshalTOML() + if dependencies != "" { + sb.WriteString(NEWLINE) + sb.WriteString(dependencies) + } + profiles := mod.Profiles.MarshalTOML() + if profiles != "" { + sb.WriteString(NEWLINE) + sb.WriteString(profiles) + } return sb.String() } @@ -106,9 +112,11 @@ func (p *Profile) MarshalTOML() string { return sb.String() } -const PACKAGE_FLAG = "package" -const DEPS_FLAG = "dependencies" -const PROFILES_FLAG = "profile" +const ( + PACKAGE_FLAG = "package" + DEPS_FLAG = "dependencies" + PROFILES_FLAG = "profile" +) func (mod *ModFile) UnmarshalTOML(data interface{}) error { meta, ok := data.(map[string]interface{}) @@ -143,7 +151,6 @@ func (mod *ModFile) UnmarshalTOML(data interface{}) error { return err } err := toml.Unmarshal(buf.Bytes(), &p) - if err != nil { return err } @@ -152,12 +159,14 @@ func (mod *ModFile) UnmarshalTOML(data interface{}) error { return nil } -const NAME_FLAG = "name" -const EDITION_FLAG = "edition" -const VERSION_FLAG = "version" -const DESCRIPTION_FLAG = "description" -const INCLUDE_FLAG = "include" -const EXCLUDE_FLAG = "exclude" +const ( + NAME_FLAG = "name" + EDITION_FLAG = "edition" + VERSION_FLAG = "version" + DESCRIPTION_FLAG = "description" + INCLUDE_FLAG = "include" + EXCLUDE_FLAG = "exclude" +) func (pkg *Package) UnmarshalTOML(data interface{}) error { meta, ok := data.(map[string]interface{}) @@ -258,7 +267,6 @@ type DependenciesUI struct { } func (dep *Dependencies) MarshalLockTOML() (string, error) { - marshaledDeps := make(map[string]Dependency) for _, depKey := range dep.Deps.Keys() { dep, ok := dep.Deps.Get(depKey) @@ -280,7 +288,6 @@ func (dep *Dependencies) MarshalLockTOML() (string, error) { } func (dep *Dependencies) UnmarshalLockTOML(data string) error { - if dep.Deps == nil { dep.Deps = orderedmap.NewOrderedMap[string, Dependency]() } From b3d37bf9212b540c43ab283fc48af423215b1c90 Mon Sep 17 00:00:00 2001 From: Briheet Singh Yadav Date: Tue, 6 Aug 2024 17:07:46 +0530 Subject: [PATCH 2/2] updated the toml_test.go Signed-off-by: Briheet Singh Yadav --- pkg/package/toml_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/package/toml_test.go b/pkg/package/toml_test.go index ec362101..54cf225a 100644 --- a/pkg/package/toml_test.go +++ b/pkg/package/toml_test.go @@ -335,6 +335,13 @@ func TestInitEmptyPkg(t *testing.T) { expected_toml = strings.ReplaceAll(expected_toml, "\r\n", "\n") got_data = strings.ReplaceAll(got_data, "\r\n", "\n") + expected_toml = strings.TrimSpace(expected_toml) + got_data = strings.TrimSpace(got_data) + + // Ensure there's no extra newlines between sections + expected_toml = strings.Join(strings.Fields(expected_toml), "\n") + got_data = strings.Join(strings.Fields(got_data), "\n") + fmt.Printf("expected_toml: '%q'\n", expected_toml) fmt.Printf("modfile: '%q'\n", got_data)