From cf1b014f89227eeed0d8fa325164562e8b98136a Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 29 May 2024 13:10:50 +0530 Subject: [PATCH] reverted the useless changes Signed-off-by: d4v1d03 --- pkg/package/modfile.go | 62 +----------------------------------------- pkg/package/toml.go | 8 ++++++ 2 files changed, 9 insertions(+), 61 deletions(-) diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index c8a13553..261f67d9 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -2,14 +2,11 @@ package pkg import ( - "bytes" "errors" "fmt" "net/url" "os" - "io" "path/filepath" - "sort" "strings" "github.com/BurntSushi/toml" @@ -437,15 +434,11 @@ func LoadLockDeps(homePath string) (*Dependencies, error) { } // Write the contents of 'ModFile' to 'kcl.mod' file -// StoreModFile writes the contents of 'ModFile' to 'kcl.mod' file. -// It uses an ordered dictionary for serialization/deserialization to preserve the order of dependencies. func (mfile *ModFile) StoreModFile() error { fullPath := filepath.Join(mfile.HomePath, MOD_FILE) - tomlContent := mfile.MarshalTOML() - return utils.StoreToFile(fullPath, tomlContent) + return utils.StoreToFile(fullPath, mfile.MarshalTOML()) } - // Returns the path to the kcl.mod file func (mfile *ModFile) GetModFilePath() string { return filepath.Join(mfile.HomePath, MOD_FILE) @@ -621,56 +614,3 @@ func ParseRepoFullNameFromGitSource(gitSrc Git) (string, error) { func ParseRepoNameFromGitSource(gitSrc Git) string { return utils.ParseRepoNameFromGitUrl(gitSrc.Url) } - -// SortMapByKey will sort the dependencies by key. -func (deps *Dependencies) SortMapByKey() { - keys := make([]string, 0, len(deps.Deps)) - for k := range deps.Deps { - keys = append(keys, k) - } - sort.Strings(keys) - - sortedDeps := make(map[string]Dependency) - for _, k := range keys { - sortedDeps[k] = deps.Deps[k] - } - deps.Deps = sortedDeps -} - -// StoreModFile writes the contents of 'ModFile' to 'kcl.mod' file. -// It uses an ordered dictionary for serialization/deserialization to preserve the order of dependencies. -type OrderedTomlEncoder struct { - Encoder *toml.Encoder - Writer io.Writer -} - -// NewOrderedTomlEncoder creates a new instance of OrderedTomlEncoder. -func NewOrderedTomlEncoder(buf *bytes.Buffer) *OrderedTomlEncoder { - return &OrderedTomlEncoder{ - Encoder: toml.NewEncoder(buf), - } -} - -// Encode encodes the given value into TOML format while maintaining the field order. -func (ote *OrderedTomlEncoder) Encode(v interface{}) error { - buf := new(bytes.Buffer) - ote.Encoder.Indent = "" - err := ote.Encoder.Encode(v) - if err != nil { - return err - } - _, err = buf.WriteTo(ote.Writer) - return err -} - -// MarshalTOML marshals the ModFile to TOML format using an ordered dictionary. -func (mfile *ModFile) MarshalTOML() (string) { - buf := new(bytes.Buffer) - enc := NewOrderedTomlEncoder(buf) - err := enc.Encode(mfile) - if err != nil { - return "" - } - return buf.String() -} - diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 94e5e75a..230c79a4 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -33,6 +33,14 @@ import ( const NEWLINE = "\n" +func (mod *ModFile) MarshalTOML() string { + var sb strings.Builder + sb.WriteString(mod.Pkg.MarshalTOML()) + sb.WriteString(mod.Dependencies.MarshalTOML()) + sb.WriteString(mod.Profiles.MarshalTOML()) + return sb.String() +} + const PACKAGE_PATTERN = "[package]" func (pkg *Package) MarshalTOML() string {