Skip to content

Commit

Permalink
fix: use field 'Name' replace 'AliasName' in kpm metadata
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Dec 19, 2023
1 parent 774bc28 commit b9dbb31
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
20 changes: 10 additions & 10 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,12 @@ func (c *KpmClient) ResolveDepsIntoMap(kclPkg *pkg.KclPkg) (map[string]string, e
return nil, err
}

depMetadatas, err := kclPkg.GetDepsMetadata()
if err != nil {
return nil, err
}
var pkgMap map[string]string = make(map[string]string)
for _, d := range kclPkg.Dependencies.Deps {
if _, ok := pkgMap[d.GetAliasName()]; ok {
return nil, reporter.NewErrorEvent(
reporter.PathIsEmpty,
fmt.Errorf("dependency name conflict, '%s' already exists", d.GetAliasName()),
"because '-' in the original dependency names is replaced with '_'\n",
"please check your dependencies with '-' or '_' in dependency name",
)
}
for _, d := range depMetadatas.Deps {
pkgMap[d.GetAliasName()] = d.GetLocalFullPath(kclPkg.HomePath)
}

Expand Down Expand Up @@ -291,7 +287,11 @@ func (c *KpmClient) ResolveDepsMetadataInJsonStr(kclPkg *pkg.KclPkg, update bool
}

// 2. Serialize to JSON
jsonData, err := json.Marshal(kclPkg.Dependencies)
depMetadatas, err := kclPkg.GetDepsMetadata()
if err != nil {
return "", err
}
jsonData, err := json.Marshal(&depMetadatas)
if err != nil {
return "", reporter.NewErrorEvent(reporter.Bug, err, "internal bug: failed to marshal the dependencies into json")
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ type Dependencies struct {
Deps map[string]Dependency `json:"packages" toml:"dependencies,omitempty"`
}

// ToDepMetadata will transform the dependencies into metadata.
// And check whether the dependency name conflicts.
func (deps *Dependencies) ToDepMetadata(pkgHome string) (*Dependencies, error) {
depMetadata := Dependencies{
Deps: make(map[string]Dependency),
}
for _, d := range deps.Deps {
if _, ok := depMetadata.Deps[d.GetAliasName()]; ok {
return nil, reporter.NewErrorEvent(
reporter.PathIsEmpty,
fmt.Errorf("dependency name conflict, '%s' already exists", d.GetAliasName()),
"because '-' in the original dependency names is replaced with '_'\n",
"please check your dependencies with '-' or '_' in dependency name",
)
}
d.Name = d.GetAliasName()
depMetadata.Deps[d.GetAliasName()] = d
}

return &depMetadata, nil
}

type Dependency struct {
Name string `json:"name" toml:"name,omitempty"`
FullName string `json:"-" toml:"full_name,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type KclPkg struct {
NoSumCheck bool
}

func (p *KclPkg) GetDepsMetadata() (*Dependencies, error) {
return p.Dependencies.ToDepMetadata(p.HomePath)
}

func NewKclPkg(opts *opt.InitOptions) KclPkg {
return KclPkg{
ModFile: *NewModFile(opts),
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"packages":{"dep-with-line":{"name":"dep-with-line","manifest_path":"<workspace>/test_kpm_metadata/dep-with-line"}}}
{"packages":{"dep_with_line":{"name":"dep_with_line","manifest_path":"<workspace>/test_kpm_metadata/dep-with-line"}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
because '-' in the original dependency names is replaced with '_'
please check your dependencies with '-' or '_' in dependency name
dependency name conflict, 'dep_with_line' already exists
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
{"packages":{"dep-with-line":{"name":"dep-with-line","manifest_path":"<workspace>/test_kpm_metadata_duplication/dep-with-line"},"dep_with-line":{"name":"dep_with-line","manifest_path":"<workspace>/test_kpm_metadata_duplication/dep_with-line"}}}

0 comments on commit b9dbb31

Please sign in to comment.