From 9a4be90a7e288297ca75de47d73574fa9427a51f Mon Sep 17 00:00:00 2001 From: zongz Date: Mon, 21 Aug 2023 17:52:25 +0800 Subject: [PATCH] feat: add the real path for all the schema type. --- pkg/api/kpm_pkg.go | 91 +++++++++++- pkg/api/kpm_pkg_test.go | 132 ++++++++++++++++-- .../test_data/test_kpm_package/kcl_pkg/main.k | 12 ++ .../test_kpm_package/kcl_pkg/sub/sub1/main.k | 2 + .../test_kpm_package/kcl_pkg/sub/sub1/main2.k | 2 + 5 files changed, 223 insertions(+), 16 deletions(-) create mode 100644 pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main.k create mode 100644 pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main2.k diff --git a/pkg/api/kpm_pkg.go b/pkg/api/kpm_pkg.go index cdbf9f44..5a696d77 100644 --- a/pkg/api/kpm_pkg.go +++ b/pkg/api/kpm_pkg.go @@ -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" @@ -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 } diff --git a/pkg/api/kpm_pkg_test.go b/pkg/api/kpm_pkg_test.go index 74a25164..40b2a580 100644 --- a/pkg/api/kpm_pkg_test.go +++ b/pkg/api/kpm_pkg_test.go @@ -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") } diff --git a/pkg/api/test_data/test_kpm_package/kcl_pkg/main.k b/pkg/api/test_data/test_kpm_package/kcl_pkg/main.k index 8af67a67..82cfb7f9 100644 --- a/pkg/api/test_data/test_kpm_package/kcl_pkg/main.k +++ b/pkg/api/test_data/test_kpm_package/kcl_pkg/main.k @@ -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' } @@ -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 = [{ diff --git a/pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main.k b/pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main.k new file mode 100644 index 00000000..8ddf2a1f --- /dev/null +++ b/pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main.k @@ -0,0 +1,2 @@ +schema SchemaInSubSub1K: + msg: str diff --git a/pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main2.k b/pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main2.k new file mode 100644 index 00000000..86ba216e --- /dev/null +++ b/pkg/api/test_data/test_kpm_package/kcl_pkg/sub/sub1/main2.k @@ -0,0 +1,2 @@ +schema SchemaWithSameName: + msg: str \ No newline at end of file