Skip to content

Commit

Permalink
Merge pull request #436 from briheet/main
Browse files Browse the repository at this point in the history
bug: extra line bothering kcl.mod
  • Loading branch information
Peefy authored Aug 6, 2024
2 parents e567824 + b3d37bf commit fbafede
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
39 changes: 23 additions & 16 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -143,7 +151,6 @@ func (mod *ModFile) UnmarshalTOML(data interface{}) error {
return err
}
err := toml.Unmarshal(buf.Bytes(), &p)

if err != nil {
return err
}
Expand All @@ -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{})
Expand Down Expand Up @@ -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)
Expand All @@ -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]()
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/package/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fbafede

Please sign in to comment.