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 'GetFullSchemaType' to support external package in kcl doc #188

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading