From fc4496d3175774f2e5f3ad2c663e86c2a644c6a9 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 7 Nov 2024 14:29:43 +0800 Subject: [PATCH] refactor: add 'alias' to struct 'Source' Signed-off-by: zongz --- pkg/client/add.go | 19 ++++++++++++------- pkg/client/add_test.go | 2 +- pkg/downloader/source.go | 1 + pkg/downloader/toml.go | 16 ++++++++-------- pkg/package/toml.go | 17 ++++++++++------- pkg/package/toml_test.go | 1 + 6 files changed, 33 insertions(+), 23 deletions(-) diff --git a/pkg/client/add.go b/pkg/client/add.go index 9bb88f7c..1665dbe2 100644 --- a/pkg/client/add.go +++ b/pkg/client/add.go @@ -13,16 +13,21 @@ import ( type AddOptions struct { // Source is the source of the package to be pulled. // Including git, oci, local. - Source *downloader.Source - KclPkg *pkg.KclPkg - NewPkgName string + Source *downloader.Source + KclPkg *pkg.KclPkg } type AddOption func(*AddOptions) error -func WithNewPkgName(newPkgName string) AddOption { +func WithAddPkgNameAlias(nameAlias string) AddOption { return func(opts *AddOptions) error { - opts.NewPkgName = newPkgName + if opts.Source == nil { + return fmt.Errorf("source cannot be nil") + } + if opts.Source.ModSpec.IsNil() { + return fmt.Errorf("modSpec cannot be nil") + } + opts.Source.ModSpec.Alias = nameAlias return nil } } @@ -138,8 +143,8 @@ func (c *KpmClient) Add(options ...AddOption) error { } var depName string - if opts.NewPkgName != "" { - depName = opts.NewPkgName + if opts.Source.ModSpec.Alias != "" { + depName = opts.Source.ModSpec.Alias } else { depName = depPkg.ModFile.Pkg.Name } diff --git a/pkg/client/add_test.go b/pkg/client/add_test.go index ebaa609f..8b7090d1 100644 --- a/pkg/client/add_test.go +++ b/pkg/client/add_test.go @@ -186,7 +186,7 @@ func TestAddRenameWithModSpec(t *testing.T) { err = kpmcli.Add( WithAddKclPkg(kpkg), WithAddSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.1"), - WithNewPkgName("newpkg"), + WithAddPkgNameAlias("newpkg"), ) if err != nil { diff --git a/pkg/downloader/source.go b/pkg/downloader/source.go index d32abb35..d3a33fed 100644 --- a/pkg/downloader/source.go +++ b/pkg/downloader/source.go @@ -20,6 +20,7 @@ import ( type ModSpec struct { Name string Version string + Alias string } // IsNil returns true if the ModSpec is nil. diff --git a/pkg/downloader/toml.go b/pkg/downloader/toml.go index 38cf25dd..abe37773 100644 --- a/pkg/downloader/toml.go +++ b/pkg/downloader/toml.go @@ -16,14 +16,18 @@ const SPEC_PATTERN = "%s = %q" func (ps *ModSpec) MarshalTOML() string { var sb strings.Builder if ps != nil && len(ps.Version) != 0 && len(ps.Name) != 0 { - sb.WriteString(fmt.Sprintf(SPEC_PATTERN, ps.Name, ps.Version)) + if len(ps.Alias) == 0 { + sb.WriteString(ps.Version) + } else { + sb.WriteString(fmt.Sprintf(SOURCE_PATTERN, fmt.Sprintf("package = %q, version = %q", ps.Name, ps.Version))) + } return sb.String() } return sb.String() } -func (source *Source) MarshalTOML(rename bool) string { +func (source *Source) MarshalTOML() string { var sb strings.Builder if source.SpecOnly() { return source.ModSpec.MarshalTOML() @@ -32,7 +36,7 @@ func (source *Source) MarshalTOML(rename bool) string { var tomlStr string if source.ModSpec != nil && len(source.ModSpec.Version) > 0 { - if rename { + if source.ModSpec.Alias != "" { pkgSpec = fmt.Sprintf(", package = %q, version = %q", source.ModSpec.Name, source.ModSpec.Version) } else { pkgSpec = fmt.Sprintf(", version = %q", source.ModSpec.Version) @@ -62,11 +66,7 @@ func (source *Source) MarshalTOML(rename bool) string { } } - if source.ModSpec != nil && len(source.ModSpec.Name) != 0 && !rename { - sb.WriteString(fmt.Sprintf(DEP_PATTERN, source.ModSpec.Name, tomlStr)) - } else { - sb.WriteString(tomlStr) - } + sb.WriteString(tomlStr) } return sb.String() diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 57a713b7..687c9733 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -89,16 +89,14 @@ const DEP_PATTERN = "%s = %s" func (dep *Dependency) MarshalTOML() string { var sb strings.Builder - if dep.Source.ModSpec != nil && dep.Source.ModSpec.Version != "" && dep.Source.ModSpec.Name != "" { - if dep.Source.ModSpec.Name == dep.Name { - sb.WriteString(dep.Source.MarshalTOML(false)) - } else { - sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML(true))) + depName := dep.Name + if !dep.Source.ModSpec.IsNil() { + if dep.Source.ModSpec.Alias != "" { + depName = dep.Source.ModSpec.Alias } - } else { - sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML(false))) } + sb.WriteString(fmt.Sprintf(DEP_PATTERN, depName, dep.Source.MarshalTOML())) return sb.String() } @@ -235,6 +233,11 @@ func (deps *Dependencies) UnmarshalModTOML(data interface{}) error { if err != nil { return err } + if !dep.Source.ModSpec.IsNil() { + if dep.Source.ModSpec.Name != dep.Name { + dep.Source.ModSpec.Alias = dep.Name + } + } deps.Deps.Set(k, dep) } diff --git a/pkg/package/toml_test.go b/pkg/package/toml_test.go index 27110583..cfbd9034 100644 --- a/pkg/package/toml_test.go +++ b/pkg/package/toml_test.go @@ -374,6 +374,7 @@ func TestUnMarshalRename(t *testing.T) { assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Version, "0.0.1") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Name, "subhelloworld") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Version, "0.0.1") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Alias, "newpkg") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Reg, "ghcr.io") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Repo, "kcl-lang/helloworld") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Tag, "0.1.4")