Skip to content

Commit

Permalink
feat: add api 'GetFullSchemaType' to support external package in kcl doc
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 27, 2023
1 parent 17b297a commit 1c28969
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 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) {

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: 1 addition & 4 deletions pkg/kcl/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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
}

Expand Down
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 1c28969

Please sign in to comment.