Skip to content

Commit

Permalink
reverted the useless changes
Browse files Browse the repository at this point in the history
Signed-off-by: d4v1d03 <a_pandey1@ce.iitr.ac.in>
  • Loading branch information
d4v1d03 committed May 29, 2024
1 parent a79f156 commit cf1b014
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 61 deletions.
62 changes: 1 addition & 61 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
package pkg

import (
"bytes"
"errors"
"fmt"
"net/url"
"os"
"io"
"path/filepath"
"sort"
"strings"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}

8 changes: 8 additions & 0 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cf1b014

Please sign in to comment.