Skip to content

Commit

Permalink
feat: sync kcl v0.5.4 APIs and update test cases. (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Aug 16, 2023
1 parent da2bba4 commit afc1b9f
Show file tree
Hide file tree
Showing 11 changed files with 383 additions and 292 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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.5.5
kcl-lang.io/kcl-artifact-go v0.5.6
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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/kcl-artifact-go v0.5.5 h1:jsN6JDUDLhreT79ZhIes9G4+8KfwehWFJ6DTK/B29Sc=
kcl-lang.io/kcl-artifact-go v0.5.5/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
kcl-lang.io/kcl-artifact-go v0.5.6 h1:PdANr42tKq5Myoqg47LSxiyH9DbxomU5Q+sdOp0CJ90=
kcl-lang.io/kcl-artifact-go v0.5.6/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
5 changes: 5 additions & 0 deletions kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func WithWorkDir(workDir string) Option { return kcl.WithWorkDir(workDir) }
// WithDisableNone returns a Option which hold a disable none switch.
func WithDisableNone(disableNone bool) Option { return kcl.WithDisableNone(disableNone) }

// WithIncludeSchemaTypePath returns a Option which hold a include schema type path switch.
func WithIncludeSchemaTypePath(includeSchemaTypePath bool) Option {
return kcl.WithIncludeSchemaTypePath(includeSchemaTypePath)
}

// WithPrintOverridesAST returns a Option which hold a printOverridesAST switch.
func WithPrintOverridesAST(printOverridesAST bool) Option {
return kcl.WithPrintOverridesAST(printOverridesAST)
Expand Down
53 changes: 49 additions & 4 deletions kclvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,51 @@ func TestRunFiles(t *testing.T) {
}
}

func TestIndlu(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_include_schema_type_path.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.WithIncludeSchemaTypePath(true),
)
if err != nil {
t.Fatal(err)
}
if expect, got := "App", result.First().Get("a1._type"); expect != got {
t.Fatalf("expect = %v, got = %v", expect, got)
}
if expect, got := "App", result.First().Get("a2._type"); expect != got {
t.Fatalf("expect = %v, got = %v", expect, got)
}
if expect, got := "default", result.First().Get("a1.image"); expect != got {
t.Fatalf("expect = %v, got = %v", expect, got)
}
if expect, got := "a2-image", result.First().Get("a2.image"); expect != got {
t.Fatalf("expect = %v, got = %v", expect, got)
}

os.Remove(testdata_main_k)
defer os.Remove(testdata_main_k)
}

func TestWithOverrides(t *testing.T) {
const code = `
schema App:
Expand Down Expand Up @@ -245,7 +290,7 @@ func TestGetSchemaType(t *testing.T) {
"name": {
Type: "str",
Line: 1,
Default: "",
Default: "\"kcl\"",
Properties: map[string]*gpyrpc.KclType{},
Required: []string{},
UnionTypes: []*gpyrpc.KclType{},
Expand All @@ -255,7 +300,7 @@ func TestGetSchemaType(t *testing.T) {
"age": {
Type: "int",
Line: 2,
Default: "",
Default: "1",
Properties: map[string]*gpyrpc.KclType{},
Required: []string{},
UnionTypes: []*gpyrpc.KclType{},
Expand Down Expand Up @@ -318,7 +363,7 @@ func TestGetSchemaTypeMapping(t *testing.T) {
"name": {
Type: "str",
Line: 1,
Default: "",
Default: "\"kcl\"",
Properties: map[string]*gpyrpc.KclType{},
Required: []string{},
UnionTypes: []*gpyrpc.KclType{},
Expand All @@ -328,7 +373,7 @@ func TestGetSchemaTypeMapping(t *testing.T) {
"age": {
Type: "int",
Line: 2,
Default: "",
Default: "1",
Properties: map[string]*gpyrpc.KclType{},
Required: []string{},
UnionTypes: []*gpyrpc.KclType{},
Expand Down
10 changes: 10 additions & 0 deletions pkg/kcl/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ func WithDisableNone(disableNone bool) Option {
return *opt
}

// WithIncludeSchemaTypePath returns a Option which hold a include schema type path switch.
func WithIncludeSchemaTypePath(includeSchemaTypePath bool) Option {
var opt = NewOption()
opt.IncludeSchemaTypePath = includeSchemaTypePath
return *opt
}

// kcl -k --sort_keys
func WithSortKeys(sortKeys bool) Option {
var opt = NewOption()
Expand Down Expand Up @@ -234,6 +241,9 @@ func (p *Option) Merge(opts ...Option) *Option {
if opt.SortKeys {
p.SortKeys = opt.SortKeys
}
if opt.IncludeSchemaTypePath {
p.IncludeSchemaTypePath = opt.IncludeSchemaTypePath
}
if opt.ExternalPkgs != nil {
p.ExternalPkgs = append(p.ExternalPkgs, opt.ExternalPkgs...)
}
Expand Down
Loading

0 comments on commit afc1b9f

Please sign in to comment.