Skip to content

Commit

Permalink
feat: support filter for api to get schema type. (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe authored Aug 22, 2023
1 parent 10be407 commit 7a41919
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 105 deletions.
111 changes: 68 additions & 43 deletions pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ type KclPackage struct {
pkg *pkg.KclPkg
}

// The KCL Type

// An additional field 'Name' is added to the original 'KclType'.
//
// 'Name' is the name of the kcl type.
//
// 'RelPath' is the relative path to the package home path.
type KclType struct {
Name string
RelPath string
*gpyrpc.KclType
}

// NewKclTypes returns a new KclType.
func NewKclTypes(name, path string, tys *gpyrpc.KclType) *KclType {
return &KclType{
Name: name,
RelPath: path,
KclType: tys,
}
}

// GetKclPackage returns the kcl package infomation.
//
// 'pkgPath' is the root path of the package where the 'kcl.mod' is located in.
Expand Down Expand Up @@ -72,22 +94,31 @@ func (pkg *KclPackage) GetPkgProfile() pkg.Profile {
// 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("")
func (pkg *KclPackage) GetAllSchemaTypeMapping() (map[string]map[string]*KclType, error) {
return pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{IsSchemaType})
}

// 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)
func (pkg *KclPackage) GetSchemaTypeMappingNamed(schemaName string) (map[string]map[string]*KclType, error) {
namedFilterFunc := func(kt *KclType) bool {
return IsSchemaNamed(kt, schemaName)
}
return pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{IsSchemaType, namedFilterFunc})
}

// GetSchemaTypeMappingWithFilters returns the schema type filtered by the filter functions.
func (pkg *KclPackage) GetSchemaTypeMappingWithFilters(fileterFuncs []KclTypeFilterFunc) (map[string]map[string]*KclType, error) {
schemaTypes := make(map[string]map[string]*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)
fileteredKtypeMap := make(map[string]*KclType)
schemaTypeMap, err := kcl.GetSchemaTypeMapping(path, "", "")
if err != nil {
return err
}
Expand All @@ -97,7 +128,22 @@ func (pkg *KclPackage) GetSchemaTypeMappingNamed(schemaName string) (map[string]
return err
}

schemaTypes[relPath] = schemaTypeMap
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 len(fileteredKtypeMap) > 0 {
schemaTypes[relPath] = fileteredKtypeMap
}
}

return nil
Expand All @@ -110,45 +156,24 @@ func (pkg *KclPackage) GetSchemaTypeMappingNamed(schemaName string) (map[string]
return schemaTypes, nil
}

// 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
}
type KclTypeFilterFunc func(kt *KclType) bool

schemaTypes[relPath] = schemaType
}
// IsSchema returns true if the type is schema.
func IsSchema(kt *KclType) bool {
return kt.Type == "schema"
}

return nil
})
// IsSchemaType returns true if the type is schema type.
func IsSchemaType(kt *KclType) bool {
return IsSchema(kt) && kt.SchemaName == kt.Name
}

if err != nil {
return nil, err
}
// IsSchemaInstance returns true if the type is schema instance.
func IsSchemaInstance(kt *KclType) bool {
return IsSchema(kt) && kt.SchemaName != kt.Name
}

return schemaTypes, nil
// IsSchemaNamed returns true if the type is schema and the name is equal to the given name.
func IsSchemaNamed(kt *KclType, name string) bool {
return IsSchema(kt) && kt.Name == name
}
100 changes: 38 additions & 62 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,15 @@ func TestPackageApi(t *testing.T) {
schemas, err := pkg.GetAllSchemaTypeMapping()
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 3)
assert.Equal(t, len(schemas["."]), 6)
assert.Equal(t, len(schemas["."]), 2)
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")
Expand All @@ -78,9 +70,8 @@ func TestGetAllSchemaTypesMappingNamed(t *testing.T) {

schemas, err := pkg.GetSchemaTypeMappingNamed("SchemaWithSameName")
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 3)
assert.Equal(t, len(schemas), 2)
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
Expand All @@ -92,7 +83,8 @@ func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
}

func TestGetAllSchemaTypes(t *testing.T) {
func TestGetSchemaTypeMappingWithFilters(t *testing.T) {

pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
kcl_pkg_path, err := GetKclPkgPath()

Expand All @@ -102,60 +94,44 @@ func TestGetAllSchemaTypes(t *testing.T) {
err = pkg.pkg.ResolveDepsMetadata(kcl_pkg_path, true)
assert.Equal(t, err, nil)

schemas, err := pkg.GetAllSchemaType()
filterFunc := func(kt *KclType) bool {
return kt.Type != "schema"
}
schemas, err := pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{filterFunc})
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")
assert.Equal(t, len(schemas), 0)

// 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)
filterFunc = func(kt *KclType) bool {
return kt.SchemaName == "SchemaWithSameName"
}
schemas, err = pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{filterFunc})
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 2)
assert.Equal(t, len(schemas[filepath.Join(".")]), 3)
assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 1)
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")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")

schemas, err := pkg.GetSchemaTypeNamed("SchemaWithSameName")
filterFunc = func(kt *KclType) bool {
return kt.SchemaName == "SchemaWithSameName" && kt.Name == "SchemaWithSameName"
}
schemas, err = pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{filterFunc})
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), 2)
assert.Equal(t, len(schemas[filepath.Join(".")]), 1)
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")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Name, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].RelPath, ".")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Name, "SchemaWithSameName")
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].RelPath, filepath.Join("sub", "sub1"))
}

0 comments on commit 7a41919

Please sign in to comment.