Skip to content

Commit

Permalink
chore: bump kcl lib version to v0.10.0-alpha.1 and sync the C API in …
Browse files Browse the repository at this point in the history
…the native client (#355)

* chore: bump kcl lib version to v0.10.0-alpha.1 and sync the C API in the native client

Signed-off-by: peefy <xpf6677@163.com>

* chore: deprecated the BuildProgram and ExecArtifact functions

Signed-off-by: peefy <xpf6677@163.com>

---------

Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy authored Jul 24, 2024
1 parent 7c26a8b commit 76ecc85
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 81 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
kcl-lang.io/kpm v0.9.2
kcl-lang.io/lib v0.9.3
kcl-lang.io/lib v0.10.0-alpha.1
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1289,8 +1289,8 @@ k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 h1:kmDqav+P+/5e1i9tFfHq1qcF3sOrD
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kcl-lang.io/kpm v0.9.2 h1:TNQLFWEYj9MEFZXpHGhp4/PHAv3eBogOjpYDyMIFZsU=
kcl-lang.io/kpm v0.9.2/go.mod h1:4HTbPhB4DVtkamnIv5lrRcHtCVwzp4Pf/b+d3TN4nzs=
kcl-lang.io/lib v0.9.3 h1:Mlr+H0oaxujUQi6QD6eP+Fbi1waVsRWXddIzQhddK+Y=
kcl-lang.io/lib v0.9.3/go.mod h1:tu+tzwGgHLzYZSIxUG/ntipStrxZd6OvutWYPTxS7cs=
kcl-lang.io/lib v0.10.0-alpha.1 h1:GMVB75Pc1W29gcl1WlVgdgUscH6h+d9No0fzBH0rNYg=
kcl-lang.io/lib v0.10.0-alpha.1/go.mod h1:tu+tzwGgHLzYZSIxUG/ntipStrxZd6OvutWYPTxS7cs=
oras.land/oras-go v1.2.5 h1:XpYuAwAb0DfQsunIyMfeET92emK8km3W4yEzZvUbsTo=
oras.land/oras-go v1.2.5/go.mod h1:PuAwRShRZCsZb7g8Ar3jKKQR/2A/qN+pkYxIOd/FAoo=
oras.land/oras-go/v2 v2.5.0 h1:o8Me9kLY74Vp5uw07QXPiitjsw7qNXi8Twd+19Zf02c=
Expand Down
40 changes: 36 additions & 4 deletions kclvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,39 @@ func TestGetSchemaType(t *testing.T) {
}

func TestGetSchemaTypeMapping(t *testing.T) {
result, err := kcl.GetSchemaTypeMapping("test.k", "schema Person:\n name: str", "")
result, err := kcl.GetSchemaTypeMapping("test.k", "schema Person:\n name: str\n\nschema Sub(Person):\n count: int\n", "")
if err != nil {
t.Fatal(err)
}
personSchema := gpyrpc.KclType{
Filename: "test.k",
PkgPath: "__main__",
Type: "schema",
SchemaName: "Person",
Properties: map[string]*gpyrpc.KclType{
"name": {
Type: "str",
Line: 1,
Properties: map[string]*gpyrpc.KclType{},
Required: []string{},
UnionTypes: []*gpyrpc.KclType{},
Decorators: []*gpyrpc.Decorator{},
Examples: map[string]*gpyrpc.Example{},
},
},
Required: []string{"name"},
UnionTypes: []*gpyrpc.KclType{},
Decorators: []*gpyrpc.Decorator{},
Examples: map[string]*gpyrpc.Example{},
}
assert2.Equal(t, map[string]*gpyrpc.KclType{
"Person": {
"Person": &personSchema,
"Sub": {
Filename: "test.k",
PkgPath: "__main__",
Type: "schema",
SchemaName: "Person",
SchemaName: "Sub",
BaseSchema: &personSchema,
Properties: map[string]*gpyrpc.KclType{
"name": {
Type: "str",
Expand All @@ -400,8 +423,17 @@ func TestGetSchemaTypeMapping(t *testing.T) {
Decorators: []*gpyrpc.Decorator{},
Examples: map[string]*gpyrpc.Example{},
},
"count": {
Type: "int",
Line: 2,
Properties: map[string]*gpyrpc.KclType{},
Required: []string{},
UnionTypes: []*gpyrpc.KclType{},
Decorators: []*gpyrpc.Decorator{},
Examples: map[string]*gpyrpc.Example{},
},
},
Required: []string{"name"},
Required: []string{"count", "name"},
UnionTypes: []*gpyrpc.KclType{},
Decorators: []*gpyrpc.Decorator{},
Examples: map[string]*gpyrpc.Example{},
Expand Down
12 changes: 6 additions & 6 deletions pkg/native/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ void kclvm_service_free_string(void *f,const char * res) {
free_string = (void (*)(const char *))f;
return free_string(res);
}
const char* kclvm_service_call_with_length(void *f,kclvm_service* c,const char * method,const char * args,size_t * result_len){
const char* (*service_call_with_length)(kclvm_service*,const char *,const char *,size_t *);
service_call_with_length = (const char* (*)(kclvm_service*,const char *,const char *,size_t *))f;
return service_call_with_length(c,method,args,result_len);
const char* kclvm_service_call_with_length(void *f,kclvm_service* c,const char * method,const char * args,size_t args_len,size_t * result_len){
const char* (*service_call_with_length)(kclvm_service*,const char *,const char *,size_t,size_t *);
service_call_with_length = (const char* (*)(kclvm_service*,const char *,const char *,size_t,size_t *))f;
return service_call_with_length(c,method,args,args_len,result_len);
}
*/
import "C"
Expand Down Expand Up @@ -75,7 +75,7 @@ func KclvmServiceFreeString(str *C.char) {

// KclvmServiceCall calls kclvm service by c api
// args should be serialized as protobuf byte stream
func KclvmServiceCall(serv *C.kclvm_service, method *C.char, args *C.char) (*C.char, C.size_t) {
func KclvmServiceCall(serv *C.kclvm_service, method *C.char, args *C.char, args_len C.size_t) (*C.char, C.size_t) {
const fnName = "kclvm_service_call_with_length"

serviceCall, err := lib.GetSymbolPointer(fnName)
Expand All @@ -85,6 +85,6 @@ func KclvmServiceCall(serv *C.kclvm_service, method *C.char, args *C.char) (*C.c
}

var size C.size_t = C.SIZE_MAX
buf := C.kclvm_service_call_with_length(serviceCall, serv, method, args, &size)
buf := C.kclvm_service_call_with_length(serviceCall, serv, method, args, args_len, &size)
return buf, size
}
6 changes: 5 additions & 1 deletion pkg/native/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"kcl-lang.io/kcl-go/pkg/3rdparty/dlopen"
"kcl-lang.io/kcl-go/pkg/env"
"kcl-lang.io/kcl-go/pkg/plugin"
"kcl-lang.io/kcl-go/pkg/service"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
Expand Down Expand Up @@ -85,7 +86,7 @@ func cApiCall[I interface {

defer C.free(unsafe.Pointer(cIn))

cOut, cOutSize := KclvmServiceCall(c.client, cCallName, cIn)
cOut, cOutSize := KclvmServiceCall(c.client, cCallName, cIn, C.size_t(len(inBytes)))

defer KclvmServiceFreeString(cOut)

Expand Down Expand Up @@ -117,10 +118,13 @@ func (c *NativeServiceClient) ExecProgram(in *gpyrpc.ExecProgram_Args) (*gpyrpc.
return cApiCall[*gpyrpc.ExecProgram_Args, *gpyrpc.ExecProgram_Result](c, "KclvmService.ExecProgram", in)
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (c *NativeServiceClient) BuildProgram(in *gpyrpc.BuildProgram_Args) (*gpyrpc.BuildProgram_Result, error) {
env.EnableFastEvalMode()
return cApiCall[*gpyrpc.BuildProgram_Args, *gpyrpc.BuildProgram_Result](c, "KclvmService.BuildProgram", in)
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (c *NativeServiceClient) ExecArtifact(in *gpyrpc.ExecArtifact_Args) (*gpyrpc.ExecProgram_Result, error) {
return cApiCall[*gpyrpc.ExecArtifact_Args, *gpyrpc.ExecProgram_Result](c, "KclvmService.ExecArtifact", in)
}
Expand Down
65 changes: 0 additions & 65 deletions pkg/native/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package native
import (
"fmt"
"io"
"path"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -60,70 +59,6 @@ func TestExecProgramWithPluginError(t *testing.T) {
}
}

func TestExecArtifactWithPlugin(t *testing.T) {
output := path.Join(t.TempDir(), "example")
client := NewNativeServiceClient()
// BuildProgram
buildResult, err := client.BuildProgram(&gpyrpc.BuildProgram_Args{
ExecArgs: &gpyrpc.ExecProgram_Args{
KFilenameList: []string{"main.k"},
KCodeList: []string{code},
},
Output: output,
})
if err != nil {
t.Fatal(err)
}
// ExecArtifact
execResult, err := client.ExecArtifact(&gpyrpc.ExecArtifact_Args{
ExecArgs: &gpyrpc.ExecProgram_Args{
Args: []*gpyrpc.Argument{
{
Name: "a",
Value: "1",
},
{
Name: "b",
Value: "2",
},
},
},
Path: buildResult.Path,
})
if err != nil {
t.Fatal(err)
}
if execResult.ErrMessage != "" {
t.Fatal("error message must be empty")
}
}

func TestBuildProgramError(t *testing.T) {
src := `
a = 1
b = 2
`
output := path.Join(t.TempDir(), "example")
client := NewNativeServiceClient()
// BuildProgram
buildResult, err := client.BuildProgram(&gpyrpc.BuildProgram_Args{
ExecArgs: &gpyrpc.ExecProgram_Args{
KFilenameList: []string{"main.k"},
KCodeList: []string{src},
},
Output: output,
})
if err == nil {
t.Errorf("The BuildProgram should return compilation failure reason")
}
if !strings.Contains(err.Error(), "InvalidSyntax") {
t.Errorf("Unexpected error message: %q", err.Error())
}
if buildResult != nil {
t.Errorf("The BuildProgram should return nil if compilation fails")
}
}

func TestParseFile(t *testing.T) {
// Example: Test with string source
src := `schema Name:
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"kcl-lang.io/kcl-go/pkg/env"
"kcl-lang.io/kcl-go/pkg/logger"
"kcl-lang.io/kcl-go/pkg/path"
"kcl-lang.io/lib"
"kcl-lang.io/lib/go/install"
)

func init() {
Expand Down Expand Up @@ -46,7 +46,7 @@ func installKclArtifact() {
logger.GetLogger().Warningf("install kclvm failed: %s", err.Error())
}
// Install lib
err = lib.InstallKclvm(path)
err = install.InstallKclvm(path)
if err != nil {
logger.GetLogger().Warningf("install kclvm failed: %s", err.Error())
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/service/client_kclvm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (p *KclvmServiceClient) ExecProgram(args *gpyrpc.ExecProgram_Args) (resp *g
return
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (p *KclvmServiceClient) BuildProgram(args *gpyrpc.BuildProgram_Args) (resp *gpyrpc.BuildProgram_Result, err error) {
p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) {
resp, err = p.getClient(c).BuildProgram(args)
Expand All @@ -68,6 +69,7 @@ func (p *KclvmServiceClient) BuildProgram(args *gpyrpc.BuildProgram_Args) (resp
return
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (p *KclvmServiceClient) ExecArtifact(args *gpyrpc.ExecArtifact_Args) (resp *gpyrpc.ExecProgram_Result, err error) {
p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) {
resp, err = p.getClient(c).ExecArtifact(args)
Expand Down
4 changes: 4 additions & 0 deletions pkg/service/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *_KclvmServiceImpl) Ping(ctx context.Context, args *gpyrpc.Ping_Args) (*
func (p *_KclvmServiceImpl) ExecProgram(ctx context.Context, args *gpyrpc.ExecProgram_Args) (*gpyrpc.ExecProgram_Result, error) {
return p.c.ExecProgram(args)
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (p *_KclvmServiceImpl) BuildProgram(ctx context.Context, args *gpyrpc.BuildProgram_Args) (*gpyrpc.BuildProgram_Result, error) {
return p.c.BuildProgram(args)
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (p *_KclvmServiceImpl) ExecArtifact(ctx context.Context, args *gpyrpc.ExecArtifact_Args) (*gpyrpc.ExecProgram_Result, error) {
return p.c.ExecArtifact(args)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/service/kclvm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import "kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
type KclvmService interface {
Ping(in *gpyrpc.Ping_Args) (out *gpyrpc.Ping_Result, err error)
ExecProgram(in *gpyrpc.ExecProgram_Args) (out *gpyrpc.ExecProgram_Result, err error)
// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
BuildProgram(in *gpyrpc.BuildProgram_Args) (out *gpyrpc.BuildProgram_Result, err error)
// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
ExecArtifact(in *gpyrpc.ExecArtifact_Args) (out *gpyrpc.ExecProgram_Result, err error)
ParseFile(in *gpyrpc.ParseFile_Args) (out *gpyrpc.ParseFile_Result, err error)
ParseProgram(in *gpyrpc.ParseProgram_Args) (out *gpyrpc.ParseProgram_Result, err error)
Expand Down
2 changes: 2 additions & 0 deletions pkg/service/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,15 @@ func (p *restServer) handle_ExecProgram(w http.ResponseWriter, r *http.Request,
})
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (p *restServer) handle_BuildProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var args = new(gpyrpc.BuildProgram_Args)
p.handle(w, r, args, func() (proto.Message, error) {
return p.c.BuildProgram(args)
})
}

// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0.
func (p *restServer) handle_ExecArtifact(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var args = new(gpyrpc.ExecArtifact_Args)
p.handle(w, r, args, func() (proto.Message, error) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/spec/gpyrpc/gpyrpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 76ecc85

Please sign in to comment.