Skip to content

Commit

Permalink
chore: bump kcl version to v0.7.0-beta.1 and use the latest APIs
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Nov 23, 2023
1 parent 9025486 commit 6964054
Show file tree
Hide file tree
Showing 16 changed files with 959 additions and 5,038 deletions.
11 changes: 1 addition & 10 deletions cmds/kcl-go/command/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/urfave/cli/v2"

"kcl-lang.io/kcl-go"
kclvm "kcl-lang.io/kcl-go"
"kcl-lang.io/kcl-go/pkg/kcl"
)

Expand Down Expand Up @@ -99,7 +98,6 @@ func NewRunCmd() *cli.Command {
fmt.Println("======== args end ========")
}

start := time.Now()
result, err := kcl.RunFiles(
c.Args().Slice(),
kcl.WithOptions(c.StringSlice("argument")...),
Expand All @@ -108,13 +106,6 @@ func NewRunCmd() *cli.Command {
kcl.WithSortKeys(c.Bool("sort-keys")),
)

if c.Bool("debug") {
fmt.Println("======== EscapedTime begin ========")
fmt.Println("Python:", result.GetPyEscapedTime())
fmt.Println("Golang:", time.Since(start).Seconds())
fmt.Println("======== EscapedTime end ========")
}

if err != nil {
fmt.Print(err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ require (
google.golang.org/grpc v1.56.3
google.golang.org/protobuf v1.30.0
gopkg.in/yaml.v3 v3.0.1
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2
kcl-lang.io/kpm v0.3.8-0.20231101092819-703dd7cf0a25
kcl-lang.io/kcl-artifact-go v0.7.0-beta.1
kcl-lang.io/kpm v0.4.1
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,10 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2 h1:ct6HPq+ssN4kNwMnyqyJAJ35YGiWPMgV4vWyyqzqpYo=
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
kcl-lang.io/kpm v0.3.8-0.20231101092819-703dd7cf0a25 h1:JyHQebmfJImAbpi5VvcgQ8ut76q2D8MwhAjLsKPNsag=
kcl-lang.io/kpm v0.3.8-0.20231101092819-703dd7cf0a25/go.mod h1:yT6B1imC2utPsnNtMmA18WyLVFKsymCH57orvQIAxAk=
kcl-lang.io/kcl-artifact-go v0.7.0-beta.1 h1:u1l/OXWblOiDHabN+6ApSHTZwNyOskuOCf90fXreGu4=
kcl-lang.io/kcl-artifact-go v0.7.0-beta.1/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
kcl-lang.io/kpm v0.4.1 h1:72kNdmMF6UNMGwa0JOsTVWwFSHyb5TAVaj3PYMmkw4Y=
kcl-lang.io/kpm v0.4.1/go.mod h1:GM6IrXwcTZHU+JLg6b0/JXoAcfwIzJkjIbGqV15jrtI=
oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY=
oras.land/oras-go v1.2.3/go.mod h1:M/uaPdYklze0Vf3AakfarnpoEckvw0ESbRdN8Z1vdJg=
oras.land/oras-go/v2 v2.3.0 h1:lqX1aXdN+DAmDTKjiDyvq85cIaI4RkIKp/PghWlAGIU=
Expand Down
15 changes: 9 additions & 6 deletions pkg/kcl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package kcl

import (
"encoding/json"
"errors"
"fmt"
"strings"

Expand All @@ -22,7 +23,6 @@ type KCLResultList struct {
list []KCLResult
raw_json_result string
raw_yaml_result string
escaped_time string
}

func (p *KCLResultList) Len() int {
Expand Down Expand Up @@ -65,10 +65,6 @@ func (p *KCLResultList) GetRawYamlResult() string {
return p.raw_yaml_result
}

func (p *KCLResultList) GetPyEscapedTime() string {
return p.escaped_time
}

type KCLResult map[string]interface{}

func (m KCLResult) Get(key string, target ...interface{}) interface{} {
Expand Down Expand Up @@ -228,6 +224,14 @@ func run(pathList []string, opts ...Option) (*KCLResultList, error) {
if err != nil {
return nil, err
}
// Output log message
logger := args.GetLogger()
if logger != nil && resp.LogMessage != "" {
logger.Info(resp.LogMessage)
}
if resp.ErrMessage != "" {
return nil, errors.New(resp.ErrMessage)
}

var result KCLResultList
if strings.TrimSpace(resp.JsonResult) == "" {
Expand All @@ -250,6 +254,5 @@ func run(pathList []string, opts ...Option) (*KCLResultList, error) {

result.raw_json_result = resp.JsonResult
result.raw_yaml_result = resp.YamlResult
result.escaped_time = resp.EscapedTime
return &result, nil
}
2 changes: 0 additions & 2 deletions pkg/kcl/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ f = 1.5
tAssert(t, result.Len() > 0)
tAssert(t, result.First().Get("name") == "kcl")

_ = result.GetPyEscapedTime()

var s string
var i int
var f float64
Expand Down
14 changes: 13 additions & 1 deletion pkg/kcl/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"path/filepath"
"strings"

"kcl-lang.io/kcl-go/pkg/logger"
"kcl-lang.io/kcl-go/pkg/settings"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
"kcl-lang.io/kcl-go/pkg/tools/override"
)

type Option struct {
*gpyrpc.ExecProgram_Args
Err error
logger logger.Logger
Err error
}

// NewOption returns a new Option.
Expand All @@ -34,6 +36,10 @@ func (p *Option) JSONString() string {
return string(x)
}

func (p *Option) GetLogger() logger.Logger {
return p.logger
}

func ParseArgs(pathList []string, opts ...Option) (Option, error) {
var tmpOptList []Option
for _, s := range pathList {
Expand Down Expand Up @@ -73,6 +79,12 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) {
return *args, nil
}

func WithLogger(l logger.Logger) Option {
var opt = NewOption()
opt.logger = l
return *opt
}

func WithWorkDir(s string) Option {
var opt = NewOption()
opt.WorkDir = s
Expand Down
16 changes: 16 additions & 0 deletions pkg/service/client_kclvm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ func (p *KclvmServiceClient) GetSchemaTypeMapping(args *gpyrpc.GetSchemaTypeMapp
return
}

func (p *KclvmServiceClient) GetFullSchemaType(args *gpyrpc.GetFullSchemaType_Args) (resp *gpyrpc.GetSchemaType_Result, err error) {
p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) {
resp, err = p.getClient(c).GetFullSchemaType(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)
Expand Down Expand Up @@ -140,3 +148,11 @@ func (p *KclvmServiceClient) RenameCode(args *gpyrpc.RenameCode_Args) (resp *gpy
})
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
}
6 changes: 6 additions & 0 deletions pkg/service/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (p *_KclvmServiceImpl) GetSchemaType(ctx context.Context, args *gpyrpc.GetS
func (p *_KclvmServiceImpl) GetSchemaTypeMapping(ctx context.Context, args *gpyrpc.GetSchemaTypeMapping_Args) (*gpyrpc.GetSchemaTypeMapping_Result, error) {
return p.c.GetSchemaTypeMapping(args)
}
func (p *_KclvmServiceImpl) GetFullSchemaType(ctx context.Context, args *gpyrpc.GetFullSchemaType_Args) (*gpyrpc.GetSchemaType_Result, error) {
return p.c.GetFullSchemaType(args)
}
func (p *_KclvmServiceImpl) ValidateCode(ctx context.Context, args *gpyrpc.ValidateCode_Args) (*gpyrpc.ValidateCode_Result, error) {
return p.c.ValidateCode(args)
}
Expand All @@ -76,3 +79,6 @@ func (p *_KclvmServiceImpl) Rename(ctx context.Context, args *gpyrpc.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)
}
2 changes: 2 additions & 0 deletions pkg/service/kclvm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ type KclvmService interface {
OverrideFile(in *gpyrpc.OverrideFile_Args) (out *gpyrpc.OverrideFile_Result, err error)
GetSchemaType(in *gpyrpc.GetSchemaType_Args) (out *gpyrpc.GetSchemaType_Result, err error)
GetSchemaTypeMapping(in *gpyrpc.GetSchemaTypeMapping_Args) (out *gpyrpc.GetSchemaTypeMapping_Result, err error)
GetFullSchemaType(in *gpyrpc.GetFullSchemaType_Args) (out *gpyrpc.GetSchemaType_Result, err error)
ValidateCode(in *gpyrpc.ValidateCode_Args) (out *gpyrpc.ValidateCode_Result, err error)
ListDepFiles(in *gpyrpc.ListDepFiles_Args) (out *gpyrpc.ListDepFiles_Result, err error)
LoadSettingsFiles(in *gpyrpc.LoadSettingsFiles_Args) (out *gpyrpc.LoadSettingsFiles_Result, err error)
Rename(in *gpyrpc.Rename_Args) (out *gpyrpc.Rename_Result, err error)
RenameCode(in *gpyrpc.RenameCode_Args) (out *gpyrpc.RenameCode_Result, err error)
Test(in *gpyrpc.Test_Args) (out *gpyrpc.Test_Result, err error)
}
18 changes: 18 additions & 0 deletions pkg/service/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func (p *restServer) initHttpRrouter() {
p.router.POST("/api:protorpc/KclvmService.OverrideFile", p.handle_OverrideFile)
p.router.POST("/api:protorpc/KclvmService.GetSchemaType", p.handle_GetSchemaType)
p.router.POST("/api:protorpc/KclvmService.GetSchemaTypeMapping", p.handle_GetSchemaTypeMapping)
p.router.POST("/api:protorpc/KclvmService.GetFullSchemaType", p.handle_GetFullSchemaType)
p.router.POST("/api:protorpc/KclvmService.ValidateCode", p.handle_ValidateCode)
p.router.POST("/api:protorpc/KclvmService.Rename", p.handle_Rename)
p.router.POST("/api:protorpc/KclvmService.RenameCode", p.handle_RenameCode)
p.router.POST("/api:protorpc/KclvmService.Test", p.handle_Test)
}

func (p *restServer) handle(
Expand Down Expand Up @@ -178,6 +182,13 @@ func (p *restServer) handle_GetSchemaTypeMapping(w http.ResponseWriter, r *http.
})
}

func (p *restServer) handle_GetFullSchemaType(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
args := new(gpyrpc.GetFullSchemaType_Args)
p.handle(w, r, args, func() (proto.Message, error) {
return p.c.GetFullSchemaType(args)
})
}

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) {
Expand Down Expand Up @@ -212,3 +223,10 @@ func (p *restServer) handle_RenameCode(w http.ResponseWriter, r *http.Request, p
return p.c.RenameCode(args)
})
}

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.c.Test(args)
})
}
Loading

0 comments on commit 6964054

Please sign in to comment.