Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add api to 'GetFullSchemaTypeMappingWithFilters' #220

Merged
merged 4 commits into from
Nov 30, 2023
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/urfave/cli/v2 v2.25.0
gotest.tools/v3 v3.4.0
kcl-lang.io/kcl-go v0.7.0-alpha.2
kcl-lang.io/kcl-go v0.7.0-beta.1.0.20231130071901-a0ae3a253d62
)

require (
Expand Down Expand Up @@ -91,7 +91,7 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2 // indirect
kcl-lang.io/kcl-artifact-go v0.7.0-beta.2 // indirect
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,10 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2 h1:ct6HPq+ssN4kNwMnyqyJAJ35YGiWPMgV4vWyyqzqpYo=
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
kcl-lang.io/kcl-go v0.7.0-alpha.2 h1:UjKyGx4OSKUnpHTTNJC6q/qYHcexA0nEBLdd/ZdxNnA=
kcl-lang.io/kcl-go v0.7.0-alpha.2/go.mod h1:GnXGUfNp+72g4ichID2VkfI679SCLEnmSuTPF7+N1H0=
kcl-lang.io/kcl-artifact-go v0.7.0-beta.2 h1:7L5qbEqD8y672XNTk2snv8/UXScABFdM3g9Bu2Ama1Q=
kcl-lang.io/kcl-artifact-go v0.7.0-beta.2/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
kcl-lang.io/kcl-go v0.7.0-beta.1.0.20231130071901-a0ae3a253d62 h1:6FQgAZyAMXUUNEF9pY/LC+doA6I2A0Pwv5iR294Hw+s=
kcl-lang.io/kcl-go v0.7.0-beta.1.0.20231130071901-a0ae3a253d62/go.mod h1:dxF3hghkxoJKOkBrXwrqc8nRT0HylrOvsI83FxdQT6M=
oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY=
oras.land/oras-go v1.2.3/go.mod h1:M/uaPdYklze0Vf3AakfarnpoEckvw0ESbRdN8Z1vdJg=
oras.land/oras-go/v2 v2.3.0 h1:lqX1aXdN+DAmDTKjiDyvq85cIaI4RkIKp/PghWlAGIU=
Expand Down
73 changes: 72 additions & 1 deletion pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package api

import (
"fmt"
"os"
"path/filepath"

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/errors"
pkg "kcl-lang.io/kpm/pkg/package"
)
Expand Down Expand Up @@ -101,7 +103,11 @@ func (pkg *KclPackage) GetPkgProfile() pkg.Profile {
//
// 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]*KclType, error) {
return pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{IsSchemaType})
cli, err := client.NewKpmClient()
if err != nil {
return nil, err
}
return pkg.GetFullSchemaTypeMappingWithFilters(cli, []KclTypeFilterFunc{IsSchemaType})
}

// GetSchemaTypeMappingNamed returns the schema type filtered by schema name.
Expand All @@ -114,6 +120,71 @@ func (pkg *KclPackage) GetSchemaTypeMappingNamed(schemaName string) (map[string]
return pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{IsSchemaType, namedFilterFunc})
}

// GetFullSchemaTypeMappingWithFilters returns the full schema type filtered by the filter functions.
func (pkg *KclPackage) GetFullSchemaTypeMappingWithFilters(kpmcli *client.KpmClient, 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() {
fileteredKtypeMap := make(map[string]*KclType)

depsMap, err := kpmcli.ResolveDepsIntoMap(pkg.pkg)
if err != nil {
return err
}

opts := kcl.NewOption()
for depName, depPath := range depsMap {
opts.Merge(kcl.WithExternalPkgs(fmt.Sprintf(constants.EXTERNAL_PKGS_ARG_PATTERN, depName, depPath)))
}

schemaTypeList, err := kcl.GetFullSchemaType([]string{path}, "", *opts)
if err != nil && err.Error() != errors.NoKclFiles.Error() {
return err
}

schemaTypeMap := make(map[string]*gpyrpc.KclType)
for _, schemaType := range schemaTypeList {
schemaTypeMap[schemaType.SchemaName] = schemaType
}

relPath, err := filepath.Rel(pkg.GetPkgHomePath(), path)
if err != nil {
return err
}
if len(schemaTypeMap) != 0 && 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 len(fileteredKtypeMap) > 0 {
schemaTypes[relPath] = fileteredKtypeMap
}
}
}

return nil
})

if err != nil {
return nil, err
}

return schemaTypes, nil
}

// 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)
Expand Down
23 changes: 22 additions & 1 deletion pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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["."]), 2)
assert.Equal(t, len(schemas["."]), 4)
assert.Equal(t, len(schemas[filepath.Join("sub")]), 1)
assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 2)

Expand Down Expand Up @@ -134,6 +134,27 @@ func TestGetSchemaTypeMappingWithFilters(t *testing.T) {
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].RelPath, filepath.Join("sub", "sub1"))
}

func TestGetFullSchemaTypeMappingWithFilters(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "get_schema_ty", "aaa")
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
kpmcli, err := client.NewKpmClient()
assert.Equal(t, err, nil)
err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true)
assert.Equal(t, err, nil)

filterFunc := func(kt *KclType) bool {
return kt.Type == "schema"
}

schemas, err := pkg.GetFullSchemaTypeMappingWithFilters(kpmcli, []KclTypeFilterFunc{filterFunc})
assert.Equal(t, err, nil)
assert.Equal(t, len(schemas), 1)

assert.Equal(t, schemas[filepath.Join(".")]["B"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["B"].SchemaName, "B")
}

func TestGetSchemaTypeUnderEmptyDir(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "no_kcl_files")
pkg, err := GetKclPackage(pkg_path)
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/test_data/test_kpm_package/get_schema_ty/aaa/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "aaa"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
bbb = { path = "../bbb" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.bbb]
name = "bbb"
full_name = "bbb_0.0.1"
version = "0.0.1"
sum = "/PGJJE6Rp0lAsq3n9RUZ34jPGfwzYIZESs17L6kSG/Y="
path = "../bbb"
5 changes: 5 additions & 0 deletions pkg/api/test_data/test_kpm_package/get_schema_ty/aaa/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import bbb as b

a = b.B {
name: "b instance in a"
}
5 changes: 5 additions & 0 deletions pkg/api/test_data/test_kpm_package/get_schema_ty/bbb/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "bbb"
edition = "0.0.1"
version = "0.0.1"

Empty file.
2 changes: 2 additions & 0 deletions pkg/api/test_data/test_kpm_package/get_schema_ty/bbb/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema B:
name: str
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ const (
DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION = "org.kcllang.package.description"
DEFAULT_KCL_OCI_MANIFEST_SUM = "org.kcllang.package.sum"
DEFAULT_CREATE_OCI_MANIFEST_TIME = "org.opencontainers.image.created"

// The pattern of the external package argument.
EXTERNAL_PKGS_ARG_PATTERN = "%s=%s"
)
Loading