Skip to content

Commit

Permalink
feat: add api 'GetFullSchemaType' to support external package in kcl …
Browse files Browse the repository at this point in the history
…doc (#188)

* feat: add api 'GetFullSchemaType' to support external package in kcl doc

Signed-off-by: zongz <[email protected]>

* chore: bump kcl artifact version

Signed-off-by: zongz <[email protected]>

* fix: remove useless changes

Signed-off-by: zongz <[email protected]>

* fix: make fmt

Signed-off-by: zongz <[email protected]>

* fix: move add kfiles code out the method ParseArgs

Signed-off-by: zongz <[email protected]>

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Nov 30, 2023
1 parent 83897a5 commit a0ae3a2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/kcl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
opts = append(opts, *NewOption().Merge(WithKFilenames(pathList...)))
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{
Expand Down
15 changes: 15 additions & 0 deletions pkg/kcl/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
5 changes: 5 additions & 0 deletions pkg/kcl/testdata/get_schema_ty/aaa/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "aaa"
edition = "0.0.1"
version = "0.0.1"

5 changes: 5 additions & 0 deletions pkg/kcl/testdata/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/kcl/testdata/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"

2 changes: 2 additions & 0 deletions pkg/kcl/testdata/get_schema_ty/bbb/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema B:
name: str

0 comments on commit a0ae3a2

Please sign in to comment.