Skip to content

Commit

Permalink
fix: fix the 'No kcl files' error in api.
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe committed Aug 22, 2023
1 parent 7a41919 commit 3f9b848
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
30 changes: 16 additions & 14 deletions pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
"kcl-lang.io/kpm/pkg/errors"
pkg "kcl-lang.io/kpm/pkg/package"
)

Expand Down Expand Up @@ -119,31 +120,32 @@ func (pkg *KclPackage) GetSchemaTypeMappingWithFilters(fileterFuncs []KclTypeFil
if info.IsDir() {
fileteredKtypeMap := make(map[string]*KclType)
schemaTypeMap, err := kcl.GetSchemaTypeMapping(path, "", "")
if err != nil {
if err != nil && err.Error() != errors.NoKclFiles.Error() {
return err
}

relPath, err := filepath.Rel(pkg.GetPkgHomePath(), path)
if err != nil {
return err
}

for kName, kType := range schemaTypeMap {
kTy := NewKclTypes(kName, relPath, kType)
filterPassed := true
for _, filterFunc := range fileterFuncs {
if !filterFunc(kTy) {
filterPassed = false
break
if schemaTypeMap != nil {
for kName, kType := range schemaTypeMap {
kTy := NewKclTypes(kName, relPath, kType)
filterPassed := true
for _, filterFunc := range fileterFuncs {
if !filterFunc(kTy) {
filterPassed = false
break
}
}
if filterPassed {
fileteredKtypeMap[kName] = kTy
}
}
if filterPassed {
fileteredKtypeMap[kName] = kTy
if len(fileteredKtypeMap) > 0 {
schemaTypes[relPath] = fileteredKtypeMap
}
}
if len(fileteredKtypeMap) > 0 {
schemaTypes[relPath] = fileteredKtypeMap
}
}

return nil
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,19 @@ func TestGetSchemaTypeMappingWithFilters(t *testing.T) {
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Name, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].RelPath, filepath.Join("sub", "sub1"))
}

func TestGetSchemaTypeUnderEmptyDir(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "no_kcl_files")
kcl_pkg_path, err := GetKclPkgPath()

assert.Equal(t, err, nil)
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
err = pkg.pkg.ResolveDepsMetadata(kcl_pkg_path, true)
assert.Equal(t, err, nil)
schemas, err := pkg.GetSchemaTypeMappingNamed("SchemaInMain")
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 1)
assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].SchemaName, "SchemaInMain")
}
Empty file.
5 changes: 5 additions & 0 deletions pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "no_kcl_files"
edition = "0.0.1"
version = "0.0.1"

Empty file.
2 changes: 2 additions & 0 deletions pkg/api/test_data/test_kpm_package/no_kcl_files/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema SchemaInMain:
msg: str
3 changes: 3 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ var PathIsEmpty = errors.New("path is empty.")
var InvalidPkg = errors.New("invalid kcl package.")
var InvalidOciUrl = errors.New("invalid oci url.")
var UnknownEnv = errors.New("invalid environment variable.")

// No kcl files
var NoKclFiles = errors.New("No input KCL files")

0 comments on commit 3f9b848

Please sign in to comment.