Skip to content

Commit a0ae3a2

Browse files
authored
feat: add api 'GetFullSchemaType' to support external package in kcl 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]>
1 parent 83897a5 commit a0ae3a2

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

pkg/kcl/api.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ func GetSchemaType(file, code, schemaName string) ([]*gpyrpc.KclType, error) {
199199
return resp.SchemaTypeList, nil
200200
}
201201

202+
func GetFullSchemaType(pathList []string, schemaName string, opts ...Option) ([]*gpyrpc.KclType, error) {
203+
opts = append(opts, *NewOption().Merge(WithKFilenames(pathList...)))
204+
args, err := ParseArgs(pathList, opts...)
205+
if err != nil {
206+
return nil, err
207+
}
208+
209+
client := service.NewKclvmServiceClient()
210+
resp, err := client.GetFullSchemaType(&gpyrpc.GetFullSchemaType_Args{
211+
ExecArgs: args.ExecProgram_Args,
212+
SchemaName: schemaName,
213+
})
214+
215+
if err != nil {
216+
return nil, err
217+
}
218+
219+
return resp.SchemaTypeList, nil
220+
}
221+
202222
func GetSchemaTypeMapping(file, code, schemaName string) (map[string]*gpyrpc.KclType, error) {
203223
client := service.NewKclvmServiceClient()
204224
resp, err := client.GetSchemaTypeMapping(&gpyrpc.GetSchemaTypeMapping_Args{

pkg/kcl/api_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ package kcl
44

55
import (
66
"fmt"
7+
"path/filepath"
78
"reflect"
89
"sort"
910
"testing"
1011

12+
"github.com/stretchr/testify/assert"
1113
"kcl-lang.io/kcl-go/pkg/tools/list"
1214
)
1315

@@ -102,3 +104,16 @@ func TestListUpstreamFiles(t *testing.T) {
102104
t.Fatalf("\nexpect = %v\ngot = %v", expect, deps)
103105
}
104106
}
107+
108+
func TestGetFullSchemaType(t *testing.T) {
109+
testPath := filepath.Join(".", "testdata", "get_schema_ty")
110+
tys, err := GetFullSchemaType(
111+
[]string{filepath.Join(testPath, "aaa")},
112+
"",
113+
WithExternalPkgs(fmt.Sprintf("bbb=%s", filepath.Join(testPath, "bbb"))),
114+
)
115+
assert.Equal(t, err, nil)
116+
assert.Equal(t, len(tys), 1)
117+
assert.Equal(t, tys[0].Filename, filepath.Join("testdata", "get_schema_ty", "bbb", "main.k"))
118+
assert.Equal(t, tys[0].SchemaName, "B")
119+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "aaa"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import bbb as b
2+
3+
a = b.B {
4+
name: "b instance in a"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "bbb"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
schema B:
2+
name: str

0 commit comments

Comments
 (0)