From 1c2896945b7a18871533ae9c0940d21cbb9b2486 Mon Sep 17 00:00:00 2001 From: zongz Date: Mon, 27 Nov 2023 16:48:00 +0800 Subject: [PATCH 1/5] feat: add api 'GetFullSchemaType' to support external package in kcl doc Signed-off-by: zongz --- pkg/kcl/api.go | 20 ++++++++++++++++++++ pkg/kcl/api_test.go | 15 +++++++++++++++ pkg/kcl/opt.go | 5 +---- pkg/kcl/testdata/get_schema_ty/aaa/kcl.mod | 5 +++++ pkg/kcl/testdata/get_schema_ty/aaa/main.k | 5 +++++ pkg/kcl/testdata/get_schema_ty/bbb/kcl.mod | 5 +++++ pkg/kcl/testdata/get_schema_ty/bbb/main.k | 2 ++ 7 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 pkg/kcl/testdata/get_schema_ty/aaa/kcl.mod create mode 100644 pkg/kcl/testdata/get_schema_ty/aaa/main.k create mode 100644 pkg/kcl/testdata/get_schema_ty/bbb/kcl.mod create mode 100644 pkg/kcl/testdata/get_schema_ty/bbb/main.k diff --git a/pkg/kcl/api.go b/pkg/kcl/api.go index a93119f1..dbcb49ce 100644 --- a/pkg/kcl/api.go +++ b/pkg/kcl/api.go @@ -199,6 +199,26 @@ func GetSchemaType(file, code, schemaName string) ([]*gpyrpc.KclType, error) { return resp.SchemaTypeList, nil } +func GetFullSchemaType(pathList []string, schemaName string, opts ...Option) ([]*gpyrpc.KclType, error) { + + args, err := ParseArgs(pathList, opts...) + if err != nil { + return nil, err + } + + client := service.NewKclvmServiceClient() + resp, err := client.GetFullSchemaType(&gpyrpc.GetFullSchemaType_Args{ + ExecArgs: args.ExecProgram_Args, + SchemaName: schemaName, + }) + + if err != nil { + return nil, err + } + + return resp.SchemaTypeList, nil +} + func GetSchemaTypeMapping(file, code, schemaName string) (map[string]*gpyrpc.KclType, error) { client := service.NewKclvmServiceClient() resp, err := client.GetSchemaTypeMapping(&gpyrpc.GetSchemaTypeMapping_Args{ diff --git a/pkg/kcl/api_test.go b/pkg/kcl/api_test.go index 330cb235..5baaafbe 100644 --- a/pkg/kcl/api_test.go +++ b/pkg/kcl/api_test.go @@ -4,10 +4,12 @@ package kcl import ( "fmt" + "path/filepath" "reflect" "sort" "testing" + "github.com/stretchr/testify/assert" "kcl-lang.io/kcl-go/pkg/tools/list" ) @@ -102,3 +104,16 @@ func TestListUpstreamFiles(t *testing.T) { t.Fatalf("\nexpect = %v\ngot = %v", expect, deps) } } + +func TestGetFullSchemaType(t *testing.T) { + testPath := filepath.Join(".", "testdata", "get_schema_ty") + tys, err := GetFullSchemaType( + []string{filepath.Join(testPath, "aaa")}, + "", + WithExternalPkgs(fmt.Sprintf("bbb=%s", filepath.Join(testPath, "bbb"))), + ) + assert.Equal(t, err, nil) + assert.Equal(t, len(tys), 1) + assert.Equal(t, tys[0].Filename, filepath.Join("testdata", "get_schema_ty", "bbb", "main.k")) + assert.Equal(t, tys[0].SchemaName, "B") +} diff --git a/pkg/kcl/opt.go b/pkg/kcl/opt.go index 57846c7a..012d8648 100644 --- a/pkg/kcl/opt.go +++ b/pkg/kcl/opt.go @@ -50,6 +50,7 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) { tmpOptList = append(tmpOptList, WithSettings(s)) case isDir(s): tmpOptList = append(tmpOptList, WithWorkDir(s)) + tmpOptList = append(tmpOptList, WithKFilenames(s)) default: tmpOptList = append(tmpOptList, WithKFilenames(s)) } @@ -72,10 +73,6 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) { } } - if len(args.KFilenameList) == 0 { - return Option{}, fmt.Errorf("kcl.Run: no kcl file") - } - return *args, nil } diff --git a/pkg/kcl/testdata/get_schema_ty/aaa/kcl.mod b/pkg/kcl/testdata/get_schema_ty/aaa/kcl.mod new file mode 100644 index 00000000..1e29d9eb --- /dev/null +++ b/pkg/kcl/testdata/get_schema_ty/aaa/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "aaa" +edition = "0.0.1" +version = "0.0.1" + diff --git a/pkg/kcl/testdata/get_schema_ty/aaa/main.k b/pkg/kcl/testdata/get_schema_ty/aaa/main.k new file mode 100644 index 00000000..9468b496 --- /dev/null +++ b/pkg/kcl/testdata/get_schema_ty/aaa/main.k @@ -0,0 +1,5 @@ +import bbb as b + +a = b.B { + name: "b instance in a" +} diff --git a/pkg/kcl/testdata/get_schema_ty/bbb/kcl.mod b/pkg/kcl/testdata/get_schema_ty/bbb/kcl.mod new file mode 100644 index 00000000..e9ea10a5 --- /dev/null +++ b/pkg/kcl/testdata/get_schema_ty/bbb/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "bbb" +edition = "0.0.1" +version = "0.0.1" + diff --git a/pkg/kcl/testdata/get_schema_ty/bbb/main.k b/pkg/kcl/testdata/get_schema_ty/bbb/main.k new file mode 100644 index 00000000..21dad4f9 --- /dev/null +++ b/pkg/kcl/testdata/get_schema_ty/bbb/main.k @@ -0,0 +1,2 @@ +schema B: + name: str \ No newline at end of file From a3a4de821428d82572f934b8b93e581fb6bfbacf Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 30 Nov 2023 14:38:43 +0800 Subject: [PATCH 2/5] chore: bump kcl artifact version Signed-off-by: zongz --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c5622cec..4562f560 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( google.golang.org/grpc v1.56.3 google.golang.org/protobuf v1.30.0 gopkg.in/yaml.v3 v3.0.1 - kcl-lang.io/kcl-artifact-go v0.7.0-beta.1 + kcl-lang.io/kcl-artifact-go v0.7.0-beta.2 kcl-lang.io/kpm v0.4.1 ) diff --git a/go.sum b/go.sum index d6eafc4b..c8071020 100644 --- a/go.sum +++ b/go.sum @@ -750,8 +750,8 @@ 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-beta.1 h1:u1l/OXWblOiDHabN+6ApSHTZwNyOskuOCf90fXreGu4= -kcl-lang.io/kcl-artifact-go v0.7.0-beta.1/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M= +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/kpm v0.4.1 h1:72kNdmMF6UNMGwa0JOsTVWwFSHyb5TAVaj3PYMmkw4Y= kcl-lang.io/kpm v0.4.1/go.mod h1:GM6IrXwcTZHU+JLg6b0/JXoAcfwIzJkjIbGqV15jrtI= oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY= From b76537c5b7bea4894f60ddfecedfeefbc930137c Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 30 Nov 2023 14:41:37 +0800 Subject: [PATCH 3/5] fix: remove useless changes Signed-off-by: zongz --- pkg/kcl/opt.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/kcl/opt.go b/pkg/kcl/opt.go index 012d8648..43ffad2d 100644 --- a/pkg/kcl/opt.go +++ b/pkg/kcl/opt.go @@ -73,6 +73,9 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) { } } + if len(args.KFilenameList) == 0 { + return Option{}, fmt.Errorf("kcl.Run: no kcl file") + } return *args, nil } From 67a01bbb2d7e41c4c429e67852d24eefb610f143 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 30 Nov 2023 14:42:48 +0800 Subject: [PATCH 4/5] fix: make fmt Signed-off-by: zongz --- pkg/kcl/opt.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/kcl/opt.go b/pkg/kcl/opt.go index 43ffad2d..f188fe1a 100644 --- a/pkg/kcl/opt.go +++ b/pkg/kcl/opt.go @@ -76,6 +76,7 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) { if len(args.KFilenameList) == 0 { return Option{}, fmt.Errorf("kcl.Run: no kcl file") } + return *args, nil } From 8e712991bd625edcfc6a6c825018bf12b6f56be2 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 30 Nov 2023 15:06:17 +0800 Subject: [PATCH 5/5] fix: move add kfiles code out the method ParseArgs Signed-off-by: zongz --- pkg/kcl/api.go | 2 +- pkg/kcl/opt.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/kcl/api.go b/pkg/kcl/api.go index dbcb49ce..6ae1c167 100644 --- a/pkg/kcl/api.go +++ b/pkg/kcl/api.go @@ -200,7 +200,7 @@ func GetSchemaType(file, code, schemaName string) ([]*gpyrpc.KclType, error) { } func GetFullSchemaType(pathList []string, schemaName string, opts ...Option) ([]*gpyrpc.KclType, error) { - + opts = append(opts, *NewOption().Merge(WithKFilenames(pathList...))) args, err := ParseArgs(pathList, opts...) if err != nil { return nil, err diff --git a/pkg/kcl/opt.go b/pkg/kcl/opt.go index f188fe1a..57846c7a 100644 --- a/pkg/kcl/opt.go +++ b/pkg/kcl/opt.go @@ -50,7 +50,6 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) { tmpOptList = append(tmpOptList, WithSettings(s)) case isDir(s): tmpOptList = append(tmpOptList, WithWorkDir(s)) - tmpOptList = append(tmpOptList, WithKFilenames(s)) default: tmpOptList = append(tmpOptList, WithKFilenames(s)) }