Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 85 additions & 6 deletions pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package api

import (
"os"
"path/filepath"

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
pkg "kcl-lang.io/kpm/pkg/package"
Expand Down Expand Up @@ -64,12 +67,88 @@ func (pkg *KclPackage) GetPkgProfile() pkg.Profile {
return pkg.pkg.GetPkgProfile()
}

// GetAllSchemaTypes returns all the schema types of the package.
func (pkg *KclPackage) GetAllSchemaTypes() (map[string]*gpyrpc.KclType, error) {
return kcl.GetSchemaTypeMapping(pkg.GetPkgHomePath(), "", "")
// GetAllSchemaTypeMapping returns all the schema types of the package.
//
// It will return a map of schema types, the key is the relative path to the package home path.
//
// And, the value is a map of schema types, the key is the schema name, the value is the schema type.
func (pkg *KclPackage) GetAllSchemaTypeMapping() (map[string]map[string]*gpyrpc.KclType, error) {
return pkg.GetSchemaTypeMappingNamed("")
}

// GetSchemaTypeMappingNamed returns the schema type filtered by schema name.
//
// If 'schemaName' is empty, it will return all the schema types.
func (pkg *KclPackage) GetSchemaTypeMappingNamed(schemaName string) (map[string]map[string]*gpyrpc.KclType, error) {
schemaTypes := make(map[string]map[string]*gpyrpc.KclType)
err := filepath.Walk(pkg.GetPkgHomePath(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
schemaTypeMap, err := kcl.GetSchemaTypeMapping(path, "", schemaName)
if err != nil {
return err
}

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

schemaTypes[relPath] = schemaTypeMap
}

return nil
})

if err != nil {
return nil, err
}

return schemaTypes, nil
}

// GetSchemaType returns the schema type filtered by schema name.
func (pkg *KclPackage) GetSchemaType(schemaName string) ([]*gpyrpc.KclType, error) {
return kcl.GetSchemaType(pkg.GetPkgHomePath(), "", schemaName)
// GetAllSchemaType returns all the schema types of the package.
//
// It will return a map of schema types, the key is the relative path to the package home path.
//
// And, the value is a slice of schema types.
func (pkg *KclPackage) GetAllSchemaType() (map[string][]*gpyrpc.KclType, error) {
return pkg.GetSchemaTypeNamed("")
}

// GetSchemaTypeNamed returns the schema type filtered by schema name.
//
// If 'schemaName' is empty, it will return all the schema types.
func (pkg *KclPackage) GetSchemaTypeNamed(schemaName string) (map[string][]*gpyrpc.KclType, error) {
schemaTypes := make(map[string][]*gpyrpc.KclType)
err := filepath.Walk(pkg.GetPkgHomePath(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
schemaType, err := kcl.GetSchemaType(path, "", schemaName)
if err != nil {
return err
}

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

schemaTypes[relPath] = schemaType
}

return nil
})

if err != nil {
return nil, err
}

return schemaTypes, nil
}
132 changes: 122 additions & 10 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,128 @@ func TestPackageApi(t *testing.T) {

assert.Equal(t, dep.GetLocalFullPath(), filepath.Join(kcl_pkg_path, "k8s_1.27"))

schemas, err := pkg.GetAllSchemaTypes()
schemas, err := pkg.GetAllSchemaTypeMapping()
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 3)
assert.Equal(t, schemas["SchemaInMainK"].Type, "schema")
assert.Equal(t, schemas["SchemaInMainK"].SchemaName, "SchemaInMainK")
assert.Equal(t, schemas["SchemaInMainK"].Properties["msg"].Type, "str")
assert.Equal(t, schemas["schema_in_main_k"].Type, "schema")
assert.Equal(t, schemas["schema_in_main_k"].SchemaName, "SchemaInMainK")
assert.Equal(t, schemas["schema_in_main_k"].Properties["msg"].Type, "str")
assert.Equal(t, schemas["schema_in_sub_k"].Type, "schema")
assert.Equal(t, schemas["schema_in_sub_k"].SchemaName, "SchemaInSubK")
assert.Equal(t, schemas["schema_in_sub_k"].Properties["msg"].Type, "str")
assert.Equal(t, len(schemas["."]), 6)
assert.Equal(t, len(schemas[filepath.Join("sub")]), 1)
assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 2)

// All schema types under the root path
assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMainK"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMainK"].SchemaName, "SchemaInMainK")
assert.Equal(t, schemas[filepath.Join(".")]["schema_in_main_k"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["schema_in_main_k"].SchemaName, "SchemaInMainK")
assert.Equal(t, schemas[filepath.Join(".")]["schema_in_sub_k"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["schema_in_sub_k"].SchemaName, "SchemaInSubK")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name"].SchemaName, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name_in_sub"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name_in_sub"].SchemaName, "SchemaWithSameName")

// All schema types under the root_path/sub path
assert.Equal(t, schemas[filepath.Join("sub")]["SchemaInSubK"].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub")]["SchemaInSubK"].SchemaName, "SchemaInSubK")

// All schema types under the root_path/sub/sub1 path
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaInSubSub1K"].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaInSubSub1K"].SchemaName, "SchemaInSubSub1K")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
}

func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
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("SchemaWithSameName")
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 3)
assert.Equal(t, len(schemas["."]), 1)
assert.Equal(t, len(schemas[filepath.Join("sub")]), 0)
assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 1)

// // All schema types under the root path
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")

// // All schema types under the root_path/sub/sub1 path
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
}

func TestGetAllSchemaTypes(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
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.GetAllSchemaType()
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 3)
assert.Equal(t, len(schemas["."]), 6)
assert.Equal(t, len(schemas[filepath.Join("sub")]), 1)
assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 2)

// All schema types under the root path
assert.Equal(t, schemas[filepath.Join(".")][0].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")][0].SchemaName, "SchemaInMainK")
assert.Equal(t, schemas[filepath.Join(".")][1].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")][1].SchemaName, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join(".")][2].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")][2].SchemaName, "SchemaInMainK")
assert.Equal(t, schemas[filepath.Join(".")][3].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")][3].SchemaName, "SchemaInSubK")
assert.Equal(t, schemas[filepath.Join(".")][4].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")][4].SchemaName, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join(".")][5].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")][5].SchemaName, "SchemaWithSameName")

// All schema types under the root_path/sub path
assert.Equal(t, schemas[filepath.Join("sub")][0].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub")][0].SchemaName, "SchemaInSubK")

// All schema types under the root_path/sub/sub1 path
assert.Equal(t, schemas[filepath.Join("sub", "sub1")][0].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")][0].SchemaName, "SchemaInSubSub1K")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")][1].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")][1].SchemaName, "SchemaWithSameName")
}

func TestGetAllSchemaTypesNamed(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
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.GetSchemaTypeNamed("SchemaWithSameName")
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 3)
assert.Equal(t, len(schemas["."]), 1)
assert.Equal(t, len(schemas[filepath.Join("sub")]), 0)
assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 1)

// // All schema types under the root path
assert.Equal(t, schemas[filepath.Join(".")][0].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")][0].SchemaName, "SchemaWithSameName")

// // All schema types under the root_path/sub/sub1 path
assert.Equal(t, schemas[filepath.Join("sub", "sub1")][0].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")][0].SchemaName, "SchemaWithSameName")
}
12 changes: 12 additions & 0 deletions pkg/api/test_data/test_kpm_package/kcl_pkg/main.k
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import sub
import sub.sub1 as s1
import k8s.api.core.v1 as k8core

schema SchemaInMainK:
msg: str

schema SchemaWithSameName:
msg: str

schema_in_main_k = SchemaInMainK {
msg='I am the instance of SchemaInMainK'
}
Expand All @@ -12,6 +16,14 @@ schema_in_sub_k = sub.SchemaInSubK {
msg='I am the instance of SchemaInSubK'
}

schema_with_same_name = SchemaWithSameName {
msg='I am the instance of SchemaWithSameName in main.k'
}

schema_with_same_name_in_sub = s1.SchemaWithSameName {
msg='I am the instance of SchemaWithSameName in sub.k'
}

schema_in_k8s = k8core.Pod {
metadata.name = "web-app"
spec.containers = [{
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema SchemaInSubSub1K:
msg: str
2 changes: 2 additions & 0 deletions pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main2.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema SchemaWithSameName:
msg: str