From 2d6097b68b4746958c33f366240de6dfc4910744 Mon Sep 17 00:00:00 2001 From: peefy Date: Wed, 18 Sep 2024 16:31:32 +0800 Subject: [PATCH 1/2] refactor: client code Signed-off-by: peefy --- README.md | 2 + go.mod | 5 +- go.sum | 8 +- pkg/kcl/rpc_service.go | 16 -- pkg/kcl/service.go | 5 - pkg/native/client.go | 9 - pkg/runtime/builtin_service_client.go | 46 ----- pkg/runtime/init.go | 63 ------ pkg/runtime/kclvm.go | 122 ------------ pkg/runtime/proc.go | 127 ------------ pkg/runtime/runtime.go | 146 -------------- pkg/server/rest_server.go | 53 ++--- pkg/service/client_builtin_service.go | 18 -- pkg/service/client_kclvm_service.go | 226 ---------------------- pkg/service/grpc_server.go | 112 ----------- pkg/service/http_post.go | 51 ----- pkg/service/kcl_error.go | 24 --- pkg/service/rest_client.go | 23 --- pkg/service/types.go | 9 - pkg/spec/gpyrpc/Makefile | 8 - pkg/spec/gpyrpc/{service.go => server.go} | 0 runtime.go | 16 -- 22 files changed, 38 insertions(+), 1051 deletions(-) delete mode 100644 pkg/kcl/rpc_service.go delete mode 100644 pkg/runtime/builtin_service_client.go delete mode 100644 pkg/runtime/init.go delete mode 100644 pkg/runtime/kclvm.go delete mode 100644 pkg/runtime/proc.go delete mode 100644 pkg/runtime/runtime.go delete mode 100644 pkg/service/client_builtin_service.go delete mode 100644 pkg/service/client_kclvm_service.go delete mode 100644 pkg/service/grpc_server.go delete mode 100644 pkg/service/http_post.go delete mode 100644 pkg/service/kcl_error.go delete mode 100644 pkg/service/rest_client.go delete mode 100644 pkg/service/types.go delete mode 100644 pkg/spec/gpyrpc/Makefile rename pkg/spec/gpyrpc/{service.go => server.go} (100%) delete mode 100644 runtime.go diff --git a/README.md b/README.md index e93f450c..d2b34274 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,8 @@ three = hello.add(1,2) # hello.add is written by Go ` ``` +> Note: CGO is required when using plugins + ## Documents See the [KCL website](https://kcl-lang.io) diff --git a/go.mod b/go.mod index 892137cf..324a9928 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/chai2010/protorpc v1.1.4 github.com/getkin/kin-openapi v0.127.0 github.com/goccy/go-yaml v1.12.0 - github.com/gofrs/flock v0.12.1 github.com/golang/protobuf v1.5.4 github.com/google/go-cmp v0.6.0 github.com/iancoleman/strcase v0.3.0 @@ -23,9 +22,11 @@ require ( google.golang.org/grpc v1.66.2 google.golang.org/protobuf v1.34.2 gopkg.in/yaml.v3 v3.0.1 - kcl-lang.io/lib v0.10.0 + kcl-lang.io/lib v0.10.1-rc.1 ) +require github.com/ebitengine/purego v0.7.1 // indirect + require ( github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/buger/jsonparser v1.1.1 // indirect diff --git a/go.sum b/go.sum index 4ea58839..ddb91bfe 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/chai2010/protorpc v1.1.4 h1:CTtFUhzXRoeuR7FtgQ2b2vdT/KgWVpCM+sIus8zJj github.com/chai2010/protorpc v1.1.4/go.mod h1:/wO0kiyVdu7ug8dCMrA2yDr2vLfyhsLEuzLa9J2HJ+I= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA= +github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/emicklei/proto v1.13.2 h1:z/etSFO3uyXeuEsVPzfl56WNgzcvIr42aQazXaQmFZY= github.com/emicklei/proto v1.13.2/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= @@ -28,8 +30,6 @@ github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/goccy/go-yaml v1.12.0 h1:/1WHjnMsI1dlIBQutrvSMGZRQufVO3asrHfTwfACoPM= github.com/goccy/go-yaml v1.12.0/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU= -github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E= -github.com/gofrs/flock v0.12.1/go.mod h1:9zxTsyu5xtJ9DK+1tFZyibEV7y3uwDxPPfbxeeHCoD0= github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= @@ -115,5 +115,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -kcl-lang.io/lib v0.10.0 h1:VLwZTMfRZyaGfIJc8qxLn7bKr24PSgZLc3SxMRxUaN4= -kcl-lang.io/lib v0.10.0/go.mod h1:tu+tzwGgHLzYZSIxUG/ntipStrxZd6OvutWYPTxS7cs= +kcl-lang.io/lib v0.10.1-rc.1 h1:GJwg9PuKn+jaSITc9vEDACljRihgwYlwWPoijl7W9q8= +kcl-lang.io/lib v0.10.1-rc.1/go.mod h1:MfVEiCo3Yr6jZqwSo84WmjFZdqNRtXMbGtDwUM27kTY= diff --git a/pkg/kcl/rpc_service.go b/pkg/kcl/rpc_service.go deleted file mode 100644 index 4bfeb683..00000000 --- a/pkg/kcl/rpc_service.go +++ /dev/null @@ -1,16 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -package kcl - -import ( - "kcl-lang.io/kcl-go/pkg/service" - "kcl-lang.io/lib/go/api" -) - -// Service returns the interaction interface between KCL Go SDK and KCL Rust core. -// When `go build tags=rpc` is opened, use the default RPC interaction logic to avoid CGO usage. -// When closed, use CGO and dynamic libraries to interact. -func Service() api.ServiceClient { - return service.NewKclvmServiceClient() -} diff --git a/pkg/kcl/service.go b/pkg/kcl/service.go index f57090f6..e53ec2de 100644 --- a/pkg/kcl/service.go +++ b/pkg/kcl/service.go @@ -1,6 +1,3 @@ -//go:build !rpc && cgo -// +build !rpc,cgo - package kcl import ( @@ -9,8 +6,6 @@ import ( ) // Service returns the interaction interface between KCL Go SDK and KCL Rust core. -// When `go build tags=rpc` is opened, use the default RPC interaction logic to avoid CGO usage. -// When closed, use CGO and dynamic libraries to interact. func Service() api.ServiceClient { return native.NewNativeServiceClient() } diff --git a/pkg/native/client.go b/pkg/native/client.go index 7f7b39a6..2049256b 100644 --- a/pkg/native/client.go +++ b/pkg/native/client.go @@ -1,14 +1,5 @@ -//go:build cgo -// +build cgo - package native -/* -#include -#include -typedef struct kclvm_service kclvm_service; -*/ -import "C" import ( "kcl-lang.io/lib/go/native" ) diff --git a/pkg/runtime/builtin_service_client.go b/pkg/runtime/builtin_service_client.go deleted file mode 100644 index e6ae3c25..00000000 --- a/pkg/runtime/builtin_service_client.go +++ /dev/null @@ -1,46 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package runtime - -import ( - "fmt" - "io" - "net/rpc" - - "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" -) - -type BuiltinServiceClient struct { - *Runtime -} - -func (p *BuiltinServiceClient) getClient(c *rpc.Client) *gpyrpc.PROTORPC_BuiltinServiceClient { - return &gpyrpc.PROTORPC_BuiltinServiceClient{Client: c} -} -func (p *BuiltinServiceClient) wrapErr(err error, stderr io.Reader) error { - if err != nil { - if data, _ := io.ReadAll(stderr); len(data) != 0 { - return fmt.Errorf("%w: stderr = %s", err, string(data)) - } - } - return err -} - -func (p *BuiltinServiceClient) Ping(args *gpyrpc.Ping_Args) (resp *gpyrpc.Ping_Result, err error) { - p.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).Ping(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *BuiltinServiceClient) ListMethod(args *gpyrpc.ListMethod_Args) (resp *gpyrpc.ListMethod_Result, err error) { - p.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ListMethod(args) - err = p.wrapErr(err, stderr) - }) - return -} diff --git a/pkg/runtime/init.go b/pkg/runtime/init.go deleted file mode 100644 index f549c873..00000000 --- a/pkg/runtime/init.go +++ /dev/null @@ -1,63 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package runtime - -import ( - "fmt" - "runtime" - "sync" - - "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" -) - -var ( - Debug bool - rpcRuntime *Runtime - once sync.Once -) - -const tip = "Tip: Have you used a binary version of KCL in your PATH that is not consistent with the KCL Go SDK? You can upgrade or reduce the KCL version or delete the KCL in your PATH" - -func InitRuntime(maxProc int) { - once.Do(func() { initRuntime(maxProc) }) -} - -func GetRuntime() *Runtime { - once.Do(func() { initRuntime(0) }) - return rpcRuntime -} - -func initRuntime(maxProc int) { - if maxProc <= 0 { - maxProc = 2 - } - if maxProc > runtime.NumCPU()*2 { - maxProc = runtime.NumCPU() * 2 - } - - if g_KclvmRoot == "" { - panic(ErrKclvmRootNotFound) - } - - rpcRuntime = NewRuntime(int(maxProc), "kclvm_cli", "server") - rpcRuntime.Start() - - client := &BuiltinServiceClient{ - Runtime: rpcRuntime, - } - - // ping - { - args := &gpyrpc.Ping_Args{Value: "ping: kcl-go rest-server"} - resp, err := client.Ping(args) - if err != nil || resp.Value != args.Value { - fmt.Println("Init kcl runtime failed, path: ", MustGetKclvmPath()) - fmt.Println(tip) - fmt.Printf("If not, you can run `rm -r %s/bin` to fix this issue\n", MustGetKclvmPath()) - panic(err) - } - } -} diff --git a/pkg/runtime/kclvm.go b/pkg/runtime/kclvm.go deleted file mode 100644 index 5a4bb2bf..00000000 --- a/pkg/runtime/kclvm.go +++ /dev/null @@ -1,122 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package runtime - -import ( - "context" - _ "embed" - "errors" - "os" - "os/exec" - "path/filepath" - "runtime" - "time" - - "github.com/gofrs/flock" - "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/go/install" -) - -func init() { - if !env.GetDisableInstallArtifact() { - installKclArtifact() - } - g_KclvmRoot = findKclvmRoot() -} - -func installKclArtifact() { - // Get the install lib path. - path := path.LibPath() - err := os.MkdirAll(path, 0777) - if err != nil { - logger.GetLogger().Warningf("install kclvm failed: %s", err.Error()) - } - // Acquire a file lock for process synchronization - lockPath := filepath.Join(path, "init.lock") - fileLock := flock.New(lockPath) - lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() - locked, err := fileLock.TryLockContext(lockCtx, time.Second) - if err == nil && locked { - defer fileLock.Unlock() - } - if err != nil { - logger.GetLogger().Warningf("install kclvm failed: %s", err.Error()) - } - // Install lib - err = install.InstallKclvm(path) - if err != nil { - logger.GetLogger().Warningf("install kclvm failed: %s", err.Error()) - } -} - -var ( - g_KclvmRoot string - ErrKclvmRootNotFound = errors.New("kclvm root not found, please ensure kcl is in your PATH") -) - -func InitKclvmRoot(kclvmRoot string) { - g_KclvmRoot = kclvmRoot -} - -// GetKclvmRoot return kclvm root directory, return error if kclvm not found. -func GetKclvmRoot() (string, error) { - if g_KclvmRoot == "" { - return "", ErrKclvmRootNotFound - } - return g_KclvmRoot, nil -} - -// GetKclvmRoot return kclvm root directory, panic if kclvm not found. -func MustGetKclvmRoot() string { - s, err := GetKclvmRoot() - if err != nil { - panic(err) - } - return s -} - -// GetKclvmPath return kclvm/python3 executable path, return error if not found. -func GetKclvmPath() (string, error) { - if g_KclvmRoot == "" { - return "", ErrKclvmRootNotFound - } - return g_KclvmRoot, nil -} - -// MustGetKclvmPath return kclvm/python3 executable path, panic if not found. -func MustGetKclvmPath() string { - s, err := GetKclvmPath() - if err != nil { - panic(err) - } - return s -} - -func findKclvmRoot() string { - if env.GetDisableUseArtifactInPath() { - return filepath.Dir(findInstalledArtifactRoot()) - } else { - kclvm_cli_exe := "kclvm_cli" - if runtime.GOOS == "windows" { - kclvm_cli_exe += ".exe" - } - if path, err := exec.LookPath(kclvm_cli_exe); err == nil { - if runtime.GOOS == "windows" { - return filepath.Dir(path) - } else { - return filepath.Dir(filepath.Dir(path)) - } - } - return "" - } -} - -func findInstalledArtifactRoot() string { - return filepath.Join(path.LibPath(), "bin") -} diff --git a/pkg/runtime/proc.go b/pkg/runtime/proc.go deleted file mode 100644 index 3e3a5a77..00000000 --- a/pkg/runtime/proc.go +++ /dev/null @@ -1,127 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package runtime - -import ( - "bytes" - "io" - "net/rpc" - "os/exec" - - "github.com/powerman/rpc-codec/jsonrpc2" -) - -type _Process struct { - busy bool - - cmd *exec.Cmd - - stdin io.WriteCloser - stdout io.ReadCloser - stderr *limitBuffer - c *rpc.Client - - done chan error -} - -func createProcess(exe string, arg ...string) (p *_Process, err error) { - p = new(_Process) - p.cmd = exec.Command(exe, arg...) - p.stdin, err = p.cmd.StdinPipe() - if err != nil { - return nil, err - } - p.stdout, err = p.cmd.StdoutPipe() - if err != nil { - return nil, err - } - p.stderr = newLimitBuffer(10 * 1024) - p.cmd.Stderr = p.stderr - // Start the process - if err := p.cmd.Start(); err != nil { - return nil, err - } - // Wait for the exit result (2 buffers, corresponding to Wait and Kill return values) - p.done = make(chan error, 2) - go func() { - p.done <- p.cmd.Wait() - }() - // NewXxxServiceClient will occupy the channel (only one can be selected), multiple clients need to manually build the client - conn := &procReadWriteCloser{proc: p, r: p.stdout, w: p.stdin} - p.c = rpc.NewClientWithCodec(jsonrpc2.NewClientCodec(conn)) - return p, nil -} - -func (p *_Process) IsExited() bool { return len(p.done) > 0 } - -func (p *_Process) IsFree() bool { return !p.IsExited() && !p.busy } -func (p *_Process) SetFree() { p.busy = false } -func (p *_Process) SetBusy() { p.busy = true } - -func (p *_Process) GetClient() *rpc.Client { return p.c } -func (p *_Process) GetStderr() io.Reader { return io.LimitReader(p.stderr, int64(p.stderr.cap)) } - -func (p *_Process) Kill() error { - if p.IsExited() { - return nil - } - err := p.cmd.Process.Kill() - p.done <- err - return err -} - -type procReadWriteCloser struct { - proc *_Process - r io.ReadCloser - w io.WriteCloser -} - -func (p *procReadWriteCloser) Read(data []byte) (n int, err error) { - return p.r.Read(data) -} - -func (p *procReadWriteCloser) Write(data []byte) (n int, err error) { - return p.w.Write(data) -} - -func (p *procReadWriteCloser) Close() error { - return p.proc.Kill() -} - -type limitBuffer struct { - buf bytes.Buffer - cap int -} - -func newLimitBuffer(cap int) *limitBuffer { - return &limitBuffer{cap: cap} -} - -func (b *limitBuffer) Write(p []byte) (int, error) { - n := b.cap - b.buf.Len() - if n > 0 { - if n > len(p) { - n = len(p) - } - var err error - n, err = b.buf.Write(p[:n]) - if err != nil { - return n, err - } - } - if n < len(p) { - return n, io.ErrShortWrite - } - return n, nil -} - -func (b *limitBuffer) Read(p []byte) (n int, err error) { - return b.buf.Read(p) -} - -func (b *limitBuffer) String() string { - return b.buf.String() -} diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go deleted file mode 100644 index 10494444..00000000 --- a/pkg/runtime/runtime.go +++ /dev/null @@ -1,146 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package runtime - -import ( - "fmt" - "io" - "net/rpc" - "sync" - "sync/atomic" - - "github.com/chai2010/protorpc" -) - -var _ = fmt.Sprint - -func init() { - protorpc.UseSnappy = false - protorpc.UseCrc32ChecksumIEEE = false -} - -type Runtime struct { - maxProc int - exe string - args []string - - stoped int32 - procs []*_Process - limit chan struct{} - - wg sync.WaitGroup - mu sync.Mutex -} - -func NewRuntime(maxProc int, exe string, args ...string) *Runtime { - return &Runtime{ - maxProc: maxProc, - exe: exe, - args: args, - - procs: make([]*_Process, maxProc), - limit: make(chan struct{}, maxProc), - } -} - -func (p *Runtime) Start() { - p.mu.Lock() - defer p.mu.Unlock() - - for i, proc := range p.procs { - if proc == nil || proc.IsExited() { - proc, err := createProcess(p.exe, p.args...) - if err != nil { - panic(err) - } - p.procs[i] = proc - } - } -} - -func (p *Runtime) enter() { p.limit <- struct{}{} } -func (p *Runtime) leave() { <-p.limit } - -func (p *Runtime) isStoped() bool { return atomic.LoadInt32(&p.stoped) != 0 } -func (p *Runtime) setStop() { atomic.StoreInt32(&p.stoped, 1) } - -func (p *Runtime) Close() error { - p.setStop() - defer p.wg.Wait() - - var lastErr error - for _, proc := range p.procs { - if err := proc.Kill(); err != nil { - lastErr = err - } - } - if lastErr != nil { - return lastErr - } - - return nil -} - -func (p *Runtime) DoTask(task func(c *rpc.Client, stderr io.Reader)) { - if p.isStoped() { - return - } - - p.enter() - defer p.leave() - - proc := p.mustGetFreeProc() - defer p.freeProc(proc) - - p.wg.Add(1) - defer p.wg.Done() - - task(proc.GetClient(), proc.GetStderr()) -} - -func (p *Runtime) mustGetFreeProc() *_Process { - p.mu.Lock() - defer p.mu.Unlock() - - for _, proc := range p.procs { - if proc != nil && proc.IsFree() { - proc.SetBusy() - return proc - } - } - - for i, proc := range p.procs { - if proc == nil || proc.IsExited() { - if proc, err := createProcess(p.exe, p.args...); err == nil { - p.procs[i] = proc - proc.SetBusy() - return proc - } - } - } - - if len(p.procs) < p.maxProc { - proc, err := createProcess(p.exe, p.args...) - if err != nil { - return nil - } - p.procs = append(p.procs, proc) - proc.SetBusy() - return proc - } - - fmt.Println("runtime.Runtime.mustGetFreeProc: unreachable") - return nil -} - -func (p *Runtime) freeProc(proc *_Process) { - p.mu.Lock() - defer p.mu.Unlock() - - if proc != nil { - proc.SetFree() - } -} diff --git a/pkg/server/rest_server.go b/pkg/server/rest_server.go index 26ccfaf0..cb1468f2 100644 --- a/pkg/server/rest_server.go +++ b/pkg/server/rest_server.go @@ -14,13 +14,18 @@ import ( "kcl-lang.io/kcl-go/pkg/3rdparty/grpc_gateway_util" "kcl-lang.io/kcl-go/pkg/kcl" - "kcl-lang.io/kcl-go/pkg/service" "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" "kcl-lang.io/lib/go/api" ) var _ = fmt.Sprint +// Client represents an restful method result. +type RestfulResult struct { + Error string `json:"error"` + Result proto.Message `json:"result"` +} + type restServer struct { address string router *httprouter.Router @@ -115,7 +120,7 @@ func (p *restServer) handle( w.Header().Set("Content-Type", "application/json") - var result service.RestfulResult + var result RestfulResult if x, err := fn(); err != nil { result.Error = err.Error() } else { @@ -131,14 +136,14 @@ func (p *restServer) handle( } } -func (p *restServer) handle_Ping(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_Ping(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.Ping_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.Ping(args) }) } -func (p *restServer) handle_ExecProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_ExecProgram(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.ExecProgram_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.ExecProgram(args) @@ -146,7 +151,7 @@ func (p *restServer) handle_ExecProgram(w http.ResponseWriter, r *http.Request, } // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. -func (p *restServer) handle_BuildProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +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.service.BuildProgram(args) @@ -154,133 +159,133 @@ func (p *restServer) handle_BuildProgram(w http.ResponseWriter, r *http.Request, } // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. -func (p *restServer) handle_ExecArtifact(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +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) { return p.service.ExecArtifact(args) }) } -func (p *restServer) handle_ParseFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_ParseFile(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.ParseFile_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.ParseFile(args) }) } -func (p *restServer) handle_ParseProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_ParseProgram(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.ParseProgram_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.ParseProgram(args) }) } -func (p *restServer) handle_ListOptions(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_ListOptions(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.ParseProgram_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.ListOptions(args) }) } -func (p *restServer) handle_ListVariables(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_ListVariables(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.ListVariables_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.ListVariables(args) }) } -func (p *restServer) handle_LoadPackage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_LoadPackage(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.LoadPackage_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.LoadPackage(args) }) } -func (p *restServer) handle_FormatCode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_FormatCode(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.FormatCode_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.FormatCode(args) }) } -func (p *restServer) handle_FormatPath(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_FormatPath(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { var args = new(gpyrpc.FormatPath_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.FormatPath(args) }) } -func (p *restServer) handle_LintPath(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_LintPath(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.LintPath_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.LintPath(args) }) } -func (p *restServer) handle_OverrideFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_OverrideFile(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.OverrideFile_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.OverrideFile(args) }) } -func (p *restServer) handle_GetSchemaTypeMapping(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_GetSchemaTypeMapping(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.GetSchemaTypeMapping_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.GetSchemaTypeMapping(args) }) } -func (p *restServer) handle_ValidateCode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_ValidateCode(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.ValidateCode_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.ValidateCode(args) }) } -func (p *restServer) handle_ListDepFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_ListDepFiles(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.ListDepFiles_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.ListDepFiles(args) }) } -func (p *restServer) handle_LoadSettingsFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_LoadSettingsFiles(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.LoadSettingsFiles_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.LoadSettingsFiles(args) }) } -func (p *restServer) handle_Rename(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_Rename(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.Rename_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.Rename(args) }) } -func (p *restServer) handle_RenameCode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_RenameCode(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.RenameCode_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.RenameCode(args) }) } -func (p *restServer) handle_Test(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_Test(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.Test_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.Test(args) }) } -func (p *restServer) handle_UpdateDependencies(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_UpdateDependencies(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.UpdateDependencies_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.UpdateDependencies(args) }) } -func (p *restServer) handle_GetVersion(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (p *restServer) handle_GetVersion(w http.ResponseWriter, r *http.Request, _ps httprouter.Params) { args := new(gpyrpc.GetVersion_Args) p.handle(w, r, args, func() (proto.Message, error) { return p.service.GetVersion(args) diff --git a/pkg/service/client_builtin_service.go b/pkg/service/client_builtin_service.go deleted file mode 100644 index ba83805c..00000000 --- a/pkg/service/client_builtin_service.go +++ /dev/null @@ -1,18 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package service - -import ( - "kcl-lang.io/kcl-go/pkg/runtime" -) - -type BuiltinServiceClient = runtime.BuiltinServiceClient - -func NewBuiltinServiceClient() *BuiltinServiceClient { - return &BuiltinServiceClient{ - Runtime: runtime.GetRuntime(), - } -} diff --git a/pkg/service/client_kclvm_service.go b/pkg/service/client_kclvm_service.go deleted file mode 100644 index 4c92b2f3..00000000 --- a/pkg/service/client_kclvm_service.go +++ /dev/null @@ -1,226 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package service - -import ( - "fmt" - "io" - "net/rpc" - - "kcl-lang.io/kcl-go/pkg/runtime" - "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" -) - -type KclvmServiceClient struct { - Runtime *runtime.Runtime -} - -var _ KclvmService = (*KclvmServiceClient)(nil) - -func NewKclvmServiceClient() KclvmService { - c := &KclvmServiceClient{ - Runtime: runtime.GetRuntime(), - } - return c -} - -func newKclvmServiceClient() *KclvmServiceClient { - c := &KclvmServiceClient{ - Runtime: runtime.GetRuntime(), - } - return c -} - -func (p *KclvmServiceClient) getClient(c *rpc.Client) KclvmService { - return &gpyrpc.PROTORPC_KclvmServiceClient{Client: c} -} -func (p *KclvmServiceClient) wrapErr(err error, stderr io.Reader) error { - if err != nil { - err = wrapKclvmServerError(err) - if data, _ := io.ReadAll(stderr); len(data) != 0 { - return fmt.Errorf("%w: stderr = %s", err, string(data)) - } - } - return err -} - -func (p *KclvmServiceClient) Ping(args *gpyrpc.Ping_Args) (resp *gpyrpc.Ping_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).Ping(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) ExecProgram(args *gpyrpc.ExecProgram_Args) (resp *gpyrpc.ExecProgram_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ExecProgram(args) - err = p.wrapErr(err, stderr) - }) - return -} - -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram 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) - err = p.wrapErr(err, stderr) - }) - return -} - -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram 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) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) ParseFile(args *gpyrpc.ParseFile_Args) (resp *gpyrpc.ParseFile_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ParseFile(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) ParseProgram(args *gpyrpc.ParseProgram_Args) (resp *gpyrpc.ParseProgram_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ParseProgram(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) ListOptions(args *gpyrpc.ParseProgram_Args) (resp *gpyrpc.ListOptions_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ListOptions(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) ListVariables(args *gpyrpc.ListVariables_Args) (resp *gpyrpc.ListVariables_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ListVariables(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) LoadPackage(args *gpyrpc.LoadPackage_Args) (resp *gpyrpc.LoadPackage_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).LoadPackage(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) FormatCode(args *gpyrpc.FormatCode_Args) (resp *gpyrpc.FormatCode_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).FormatCode(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) FormatPath(args *gpyrpc.FormatPath_Args) (resp *gpyrpc.FormatPath_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).FormatPath(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) LintPath(args *gpyrpc.LintPath_Args) (resp *gpyrpc.LintPath_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).LintPath(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) OverrideFile(args *gpyrpc.OverrideFile_Args) (resp *gpyrpc.OverrideFile_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).OverrideFile(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) GetSchemaTypeMapping(args *gpyrpc.GetSchemaTypeMapping_Args) (resp *gpyrpc.GetSchemaTypeMapping_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).GetSchemaTypeMapping(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) ValidateCode(args *gpyrpc.ValidateCode_Args) (resp *gpyrpc.ValidateCode_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ValidateCode(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) ListDepFiles(args *gpyrpc.ListDepFiles_Args) (resp *gpyrpc.ListDepFiles_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).ListDepFiles(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) LoadSettingsFiles(args *gpyrpc.LoadSettingsFiles_Args) (resp *gpyrpc.LoadSettingsFiles_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).LoadSettingsFiles(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) Rename(args *gpyrpc.Rename_Args) (resp *gpyrpc.Rename_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).Rename(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) RenameCode(args *gpyrpc.RenameCode_Args) (resp *gpyrpc.RenameCode_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).RenameCode(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) Test(args *gpyrpc.Test_Args) (resp *gpyrpc.Test_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).Test(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) UpdateDependencies(args *gpyrpc.UpdateDependencies_Args) (resp *gpyrpc.UpdateDependencies_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).UpdateDependencies(args) - err = p.wrapErr(err, stderr) - }) - return -} - -func (p *KclvmServiceClient) GetVersion(args *gpyrpc.GetVersion_Args) (resp *gpyrpc.GetVersion_Result, err error) { - p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { - resp, err = p.getClient(c).GetVersion(args) - err = p.wrapErr(err, stderr) - }) - return -} diff --git a/pkg/service/grpc_server.go b/pkg/service/grpc_server.go deleted file mode 100644 index cfd4e5b6..00000000 --- a/pkg/service/grpc_server.go +++ /dev/null @@ -1,112 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package service - -import ( - "context" - "fmt" - "net" - - "google.golang.org/grpc" - - "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" -) - -var _ = fmt.Sprint - -func RunGrpcServer(address string) error { - grpcServer := grpc.NewServer() - gpyrpc.RegisterKclvmServiceServer(grpcServer, newKclvmServiceImpl()) - - lis, err := net.Listen("tcp", address) - if err != nil { - return err - } - - grpcServer.Serve(lis) - return nil -} - -type _KclvmServiceImpl struct { - c *KclvmServiceClient -} - -func newKclvmServiceImpl() *_KclvmServiceImpl { - return &_KclvmServiceImpl{ - c: newKclvmServiceClient(), - } -} - -func (p *_KclvmServiceImpl) Ping(ctx context.Context, args *gpyrpc.Ping_Args) (*gpyrpc.Ping_Result, error) { - return p.c.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.ExecuteProgram 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.ExecuteProgram 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) -} -func (p *_KclvmServiceImpl) ParseFile(ctx context.Context, args *gpyrpc.ParseFile_Args) (*gpyrpc.ParseFile_Result, error) { - return p.c.ParseFile(args) -} -func (p *_KclvmServiceImpl) ParseProgram(ctx context.Context, args *gpyrpc.ParseProgram_Args) (*gpyrpc.ParseProgram_Result, error) { - return p.c.ParseProgram(args) -} -func (p *_KclvmServiceImpl) ListOptions(ctx context.Context, args *gpyrpc.ParseProgram_Args) (*gpyrpc.ListOptions_Result, error) { - return p.c.ListOptions(args) -} -func (p *_KclvmServiceImpl) ListVariables(ctx context.Context, args *gpyrpc.ListVariables_Args) (*gpyrpc.ListVariables_Result, error) { - return p.c.ListVariables(args) -} -func (p *_KclvmServiceImpl) LoadPackage(ctx context.Context, args *gpyrpc.LoadPackage_Args) (*gpyrpc.LoadPackage_Result, error) { - return p.c.LoadPackage(args) -} -func (p *_KclvmServiceImpl) FormatCode(ctx context.Context, args *gpyrpc.FormatCode_Args) (*gpyrpc.FormatCode_Result, error) { - return p.c.FormatCode(args) -} -func (p *_KclvmServiceImpl) FormatPath(ctx context.Context, args *gpyrpc.FormatPath_Args) (*gpyrpc.FormatPath_Result, error) { - return p.c.FormatPath(args) -} -func (p *_KclvmServiceImpl) LintPath(ctx context.Context, args *gpyrpc.LintPath_Args) (*gpyrpc.LintPath_Result, error) { - return p.c.LintPath(args) -} -func (p *_KclvmServiceImpl) OverrideFile(ctx context.Context, args *gpyrpc.OverrideFile_Args) (*gpyrpc.OverrideFile_Result, error) { - return p.c.OverrideFile(args) -} -func (p *_KclvmServiceImpl) GetSchemaTypeMapping(ctx context.Context, args *gpyrpc.GetSchemaTypeMapping_Args) (*gpyrpc.GetSchemaTypeMapping_Result, error) { - return p.c.GetSchemaTypeMapping(args) -} -func (p *_KclvmServiceImpl) ValidateCode(ctx context.Context, args *gpyrpc.ValidateCode_Args) (*gpyrpc.ValidateCode_Result, error) { - return p.c.ValidateCode(args) -} -func (p *_KclvmServiceImpl) ListDepFiles(ctx context.Context, args *gpyrpc.ListDepFiles_Args) (*gpyrpc.ListDepFiles_Result, error) { - return p.c.ListDepFiles(args) -} -func (p *_KclvmServiceImpl) LoadSettingsFiles(ctx context.Context, args *gpyrpc.LoadSettingsFiles_Args) (*gpyrpc.LoadSettingsFiles_Result, error) { - return p.c.LoadSettingsFiles(args) -} -func (p *_KclvmServiceImpl) Rename(ctx context.Context, args *gpyrpc.Rename_Args) (*gpyrpc.Rename_Result, error) { - return p.c.Rename(args) -} -func (p *_KclvmServiceImpl) RenameCode(ctx context.Context, args *gpyrpc.RenameCode_Args) (*gpyrpc.RenameCode_Result, error) { - return p.c.RenameCode(args) -} -func (p *_KclvmServiceImpl) Test(ctx context.Context, args *gpyrpc.Test_Args) (*gpyrpc.Test_Result, error) { - return p.c.Test(args) -} -func (p *_KclvmServiceImpl) UpdateDependencies(ctx context.Context, args *gpyrpc.UpdateDependencies_Args) (*gpyrpc.UpdateDependencies_Result, error) { - return p.c.UpdateDependencies(args) -} -func (p *_KclvmServiceImpl) GetVersion(ctx context.Context, args *gpyrpc.GetVersion_Args) (*gpyrpc.GetVersion_Result, error) { - return p.c.GetVersion(args) -} diff --git a/pkg/service/http_post.go b/pkg/service/http_post.go deleted file mode 100644 index b1621e05..00000000 --- a/pkg/service/http_post.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright The KCL Authors. All rights reserved. - -package service - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "net/http" -) - -func httpPost(urlpath string, input, output interface{}) error { - const method = "POST" - - reqBody, err := json.Marshal(input) - if err != nil { - return err - } - - req, err := http.NewRequest(method, urlpath, bytes.NewReader(reqBody)) - if err != nil { - return err - } - - // python -m kclvm.program.rpc-server -http= do not support application/json Content-Type - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - - client := &http.Client{ - Transport: &http.Transport{ - DisableCompression: true, - }, - } - - r, err := client.Do(req) - if err != nil { - return err - } - defer r.Body.Close() - - bodyData, err := io.ReadAll(r.Body) - if err != nil { - return err - } - err = json.Unmarshal(bodyData, output) - if err != nil { - return fmt.Errorf("json.Unmarshal failed: bodyData = %v", string(bodyData)) - } - - return nil -} diff --git a/pkg/service/kcl_error.go b/pkg/service/kcl_error.go deleted file mode 100644 index 44fb9b6b..00000000 --- a/pkg/service/kcl_error.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright The KCL Authors. All rights reserved. - -package service - -import ( - "encoding/json" - "fmt" -) - -const KCLVM_SERVER_ERROR_CODE int64 = 0x4B434C // the ASCII code of "KCL" - -type ServerError struct { - Code int64 `json:"code,omitempty"` - Msg string `json:"message,omitempty"` -} - -func wrapKclvmServerError(err error) error { - error_with_code := ServerError{} - serde_error := json.Unmarshal([]byte(err.Error()), &error_with_code) - if serde_error == nil && error_with_code.Code == KCLVM_SERVER_ERROR_CODE { - err = fmt.Errorf("%s", error_with_code.Msg) - } - return err -} diff --git a/pkg/service/rest_client.go b/pkg/service/rest_client.go deleted file mode 100644 index 1504932e..00000000 --- a/pkg/service/rest_client.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -package service - -import ( - "errors" - - "github.com/golang/protobuf/proto" -) - -// CallRestMethod call an restful method. -func CallRestMethod(host, method string, input, output proto.Message) error { - var result RestfulResult - result.Result = output - if err := httpPost(host+"/api:protorpc/"+method, input, &result); err != nil { - return err - } - if result.Error != "" { - return errors.New(result.Error) - } - return nil -} diff --git a/pkg/service/types.go b/pkg/service/types.go deleted file mode 100644 index 3b390bca..00000000 --- a/pkg/service/types.go +++ /dev/null @@ -1,9 +0,0 @@ -package service - -import "github.com/golang/protobuf/proto" - -// Client represents an restful method result. -type RestfulResult struct { - Error string `json:"error"` - Result proto.Message `json:"result"` -} diff --git a/pkg/spec/gpyrpc/Makefile b/pkg/spec/gpyrpc/Makefile deleted file mode 100644 index 75efe7f2..00000000 --- a/pkg/spec/gpyrpc/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright 2023 The KCL Authors. All rights reserved. - -default: - go install github.com/golang/protobuf/protoc-gen-go - protoc --go_out=plugins=grpc:. gpyrpc.proto - - cp ./kcl-lang.io/kcl-go/pkg/spec/gpyrpc/gpyrpc.pb.go gpyrpc.pb.go - -rm -rf ./kcl-lang.io diff --git a/pkg/spec/gpyrpc/service.go b/pkg/spec/gpyrpc/server.go similarity index 100% rename from pkg/spec/gpyrpc/service.go rename to pkg/spec/gpyrpc/server.go diff --git a/runtime.go b/runtime.go deleted file mode 100644 index f498eef7..00000000 --- a/runtime.go +++ /dev/null @@ -1,16 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -package kcl - -import "kcl-lang.io/kcl-go/pkg/runtime" - -// InitKclvmPath init kclvm path. -func InitKclvmPath(kclvmRoot string) { - runtime.InitKclvmRoot(kclvmRoot) -} - -// InitKclvmRuntime init kclvm process. -func InitKclvmRuntime(n int) { - runtime.InitRuntime(n) -} From 4046dcb116fe9fc7109f3f5df968e85d64c326ee Mon Sep 17 00:00:00 2001 From: peefy Date: Wed, 18 Sep 2024 18:22:17 +0800 Subject: [PATCH 2/2] chore: bump kcl lib to v0.10.1-rc.3 Signed-off-by: peefy --- examples/plugin/main.go | 1 - go.mod | 6 ++---- go.sum | 6 ++---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/examples/plugin/main.go b/examples/plugin/main.go index ffff370a..f8f36e06 100644 --- a/examples/plugin/main.go +++ b/examples/plugin/main.go @@ -11,7 +11,6 @@ import ( ) func main() { - // Note we use `native.MustRun` here instead of `kcl.MustRun`, because it needs the cgo feature. yaml := kcl.MustRun("main.k", kcl.WithCode(code)).GetRawYamlResult() fmt.Println(yaml) } diff --git a/go.mod b/go.mod index 324a9928..b8d70cf5 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/iancoleman/strcase v0.3.0 github.com/julienschmidt/httprouter v1.3.0 github.com/mitchellh/mapstructure v1.5.0 - github.com/powerman/rpc-codec v1.2.2 github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc github.com/qri-io/jsonpointer v0.1.1 github.com/stretchr/testify v1.9.0 @@ -22,15 +21,14 @@ require ( google.golang.org/grpc v1.66.2 google.golang.org/protobuf v1.34.2 gopkg.in/yaml.v3 v3.0.1 - kcl-lang.io/lib v0.10.1-rc.1 + kcl-lang.io/lib v0.10.1-rc.3 ) -require github.com/ebitengine/purego v0.7.1 // indirect - require ( github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/buger/jsonparser v1.1.1 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/ebitengine/purego v0.7.1 // indirect github.com/emicklei/proto v1.13.2 github.com/fatih/color v1.15.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect diff --git a/go.sum b/go.sum index ddb91bfe..c77fb595 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,6 @@ github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/powerman/rpc-codec v1.2.2 h1:BK0JScZivljhwW/vLLhZLtUgqSxc/CD3sHEs8LiwwKw= -github.com/powerman/rpc-codec v1.2.2/go.mod h1:3Qr/y/+u3CwcSww9tfJMRn/95lB2qUdUeIQe7BYlLDo= github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc h1:DRZwH75/E4a2SOr7+gKZ99OEhmjzBzAhgyTnzo1TepY= github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c= github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA= @@ -115,5 +113,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -kcl-lang.io/lib v0.10.1-rc.1 h1:GJwg9PuKn+jaSITc9vEDACljRihgwYlwWPoijl7W9q8= -kcl-lang.io/lib v0.10.1-rc.1/go.mod h1:MfVEiCo3Yr6jZqwSo84WmjFZdqNRtXMbGtDwUM27kTY= +kcl-lang.io/lib v0.10.1-rc.3 h1:+3sCOLV3ay9JHwvaZKOJgX9hwb2MO5OiGbAtsNLH5E4= +kcl-lang.io/lib v0.10.1-rc.3/go.mod h1:MfVEiCo3Yr6jZqwSo84WmjFZdqNRtXMbGtDwUM27kTY=