Skip to content

Commit

Permalink
feat: bump kcl lib to v0.7.0-alpha.1 and impl path selectors for the …
Browse files Browse the repository at this point in the history
…run API.

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Oct 27, 2023
1 parent d636cf5 commit 0a47da9
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 270 deletions.
1 change: 1 addition & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func ExampleLintPath() {
// Output:
// Module 'a' is reimported multiple times
// Module 'a' imported but unused
// Module 'a' imported but unused
}

func ExampleFormatCode() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v3 v3.0.1
kcl-lang.io/kcl-artifact-go v0.6.0
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.1
kcl-lang.io/kpm v0.3.5
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ 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.6.0 h1:J/1FKzgGRqzFDkq0amBWxCdXXRMrtg5oT3IGtoo9sgU=
kcl-lang.io/kcl-artifact-go v0.6.0/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.1 h1:b9EPI/JBeR7cB/cViVXHaKfKpTbMKqRAtXNlePjswsk=
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.1/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
kcl-lang.io/kpm v0.3.5 h1:KEf4k9USbVolxDeszNDJCn8vt9nmKcRr0Icv77so6Z8=
kcl-lang.io/kpm v0.3.5/go.mod h1:zs+MeqK8VsrKaJcrGIQSy2gBssbRj5td+TM2Vwx2XBE=
oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY=
Expand Down
3 changes: 3 additions & 0 deletions kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func WithOptions(key_value_list ...string) Option { return kcl.WithOptions(key_v
// WithOverrides returns a Option which hold a override list.
func WithOverrides(override_list ...string) Option { return kcl.WithOverrides(override_list...) }

// WithSelectors returns a Option which hold a path selector list.
func WithSelectors(selectors ...string) Option { return kcl.WithSelectors(selectors...) }

// WithSettings returns a Option which hold a settings file.
func WithSettings(filename string) Option { return kcl.WithSettings(filename) }

Expand Down
35 changes: 35 additions & 0 deletions kclvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,41 @@ a2 = App {
}
}

func TestWithSelectors(t *testing.T) {
const code = `
schema App:
image: str = "default"
name: str = "app"
a1 = App {
name = "a1-app"
}
a2 = App {
image = "a2-image"
name = "a2-app"
}
`
const testdata_main_k = "testdata/main_selector.k"
kfile, err := os.Create(testdata_main_k)
if err != nil {
t.Fatal(err)
}
kfile.Close()

result, err := kcl.Run(testdata_main_k,
kcl.WithCode(code),
kcl.WithSelectors("a1"),
)
if err != nil {
t.Fatal(err)
}
if expect, got := "a1-app", result.First().Get("name"); expect != got {
t.Fatalf("expect = %v, got = %v", expect, got)
}
os.Remove(testdata_main_k)
defer os.Remove(testdata_main_k)
}

func _BenchmarkRunFilesParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand Down
11 changes: 11 additions & 0 deletions pkg/kcl/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ func WithOverrides(override_list ...string) Option {
return *opt
}

// kcl -S path.to.field
func WithSelectors(selectors ...string) Option {
var opt = NewOption()
opt.PathSelector = selectors
return *opt
}

func WithPrintOverridesAST(printOverrideAst bool) Option {
var opt = NewOption()
opt.PrintOverrideAst = printOverrideAst
Expand Down Expand Up @@ -219,6 +226,10 @@ func (p *Option) Merge(opts ...Option) *Option {
p.Overrides = append(p.Overrides, opt.Overrides...)
}

if len(opt.PathSelector) > 0 {
p.PathSelector = append(p.PathSelector, opt.PathSelector...)
}

if opt.DisableYamlResult {
p.DisableYamlResult = opt.DisableYamlResult
}
Expand Down
541 changes: 276 additions & 265 deletions pkg/spec/gpyrpc/gpyrpc.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pkg/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ message ExecProgram_Args {

// Whether only compiling the program
bool compile_only = 15;

// -S --path_selector
repeated string path_selector = 16;
}

message ExecProgram_Result {
Expand Down
5 changes: 3 additions & 2 deletions scripts/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const (
)

const (
KclvmAbiVersion KclvmVersionType = KclvmVersionType_v0_6_0
KclvmVersionType_latest = KclvmVersionType_v0_6_0
KclvmAbiVersion KclvmVersionType = KclvmVersionType_v0_7_0_alpha_1
KclvmVersionType_latest = KclvmVersionType_v0_7_0_alpha_1

KclvmVersionType_v0_7_0_alpha_1 KclvmVersionType = "v0.7.0-alpha.1"
KclvmVersionType_v0_6_0 KclvmVersionType = "v0.6.0"
KclvmVersionType_v0_6_0_alpha_1 KclvmVersionType = "v0.6.0-alpha.1"
KclvmVersionType_v0_5_6 KclvmVersionType = "v0.5.6"
Expand Down

0 comments on commit 0a47da9

Please sign in to comment.