Skip to content

Commit

Permalink
feat: sync kcl 0.8 abi spec (#237)
Browse files Browse the repository at this point in the history
* feat: add _type attribute hooks

Signed-off-by: peefy <[email protected]>

* feat: sync kcl 0.8 abi spec

Signed-off-by: peefy <[email protected]>

* feat: sync kcl 0.8 abi spec

Signed-off-by: peefy <[email protected]>

* refactor: raw result to KCLResult

Signed-off-by: peefy <[email protected]>

---------

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Feb 22, 2024
1 parent e5e87bd commit d03d3a9
Show file tree
Hide file tree
Showing 14 changed files with 668 additions and 480 deletions.
38 changes: 24 additions & 14 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ x1 = Person {
- [func WithPrintOverridesAST\(printOverridesAST bool\) Option](<#WithPrintOverridesAST>)
- [func WithSelectors\(selectors ...string\) Option](<#WithSelectors>)
- [func WithSettings\(filename string\) Option](<#WithSettings>)
- [func WithShowHidden\(showHidden bool\) Option](<#WithShowHidden>)
- [func WithSortKeys\(sortKeys bool\) Option](<#WithSortKeys>)
- [func WithWorkDir\(workDir string\) Option](<#WithWorkDir>)
- [type TestCaseInfo](<#TestCaseInfo>)
Expand All @@ -146,7 +147,7 @@ const KclvmAbiVersion = scripts.KclvmAbiVersion
```

<a name="FormatCode"></a>
## func [FormatCode](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L133>)
## func [FormatCode](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L138>)

```go
func FormatCode(code interface{}) ([]byte, error)
Expand Down Expand Up @@ -189,7 +190,7 @@ a = 1 + 2
</details>

<a name="FormatPath"></a>
## func [FormatPath](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L145>)
## func [FormatPath](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L150>)

```go
func FormatPath(path string) (changedPaths []string, err error)
Expand Down Expand Up @@ -227,7 +228,7 @@ func main() {
</details>

<a name="GetSchemaTypeMapping"></a>
## func [GetSchemaTypeMapping](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L227>)
## func [GetSchemaTypeMapping](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L232>)

```go
func GetSchemaTypeMapping(file, code, schemaName string) (map[string]*KclType, error)
Expand Down Expand Up @@ -272,7 +273,7 @@ func InitKclvmRuntime(n int)
InitKclvmRuntime init kclvm process.

<a name="LintPath"></a>
## func [LintPath](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L165>)
## func [LintPath](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L170>)

```go
func LintPath(paths []string) (results []string, err error)
Expand Down Expand Up @@ -322,7 +323,7 @@ Module 'a' imported but unused
</details>

<a name="ListDepFiles"></a>
## func [ListDepFiles](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L150>)
## func [ListDepFiles](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L155>)

```go
func ListDepFiles(workDir string, opt *ListDepFilesOption) (files []string, err error)
Expand All @@ -331,7 +332,7 @@ func ListDepFiles(workDir string, opt *ListDepFilesOption) (files []string, err
ListDepFiles return the depend files from the given path

<a name="ListDownStreamFiles"></a>
## func [ListDownStreamFiles](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L160>)
## func [ListDownStreamFiles](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L165>)

```go
func ListDownStreamFiles(workDir string, opt *ListDepsOptions) ([]string, error)
Expand All @@ -340,7 +341,7 @@ func ListDownStreamFiles(workDir string, opt *ListDepsOptions) ([]string, error)
ListDownStreamFiles return a list of downstream depend files from the given changed path list.

<a name="ListUpStreamFiles"></a>
## func [ListUpStreamFiles](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L155>)
## func [ListUpStreamFiles](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L160>)

```go
func ListUpStreamFiles(workDir string, opt *ListDepsOptions) (deps []string, err error)
Expand All @@ -349,7 +350,7 @@ func ListUpStreamFiles(workDir string, opt *ListDepsOptions) (deps []string, err
ListUpStreamFiles return a list of upstream depend files from the given path list

<a name="OverrideFile"></a>
## func [OverrideFile](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L177>)
## func [OverrideFile](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L182>)

```go
func OverrideFile(file string, specs, importPaths []string) (bool, error)
Expand All @@ -365,7 +366,7 @@ When the pkgpath is '__main__', it can be omitted.
importPaths. List of import statements that need to be added

<a name="Validate"></a>
## func [Validate](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L188>)
## func [Validate](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L193>)

```go
func Validate(dataFile, schemaFile string, opts *ValidateOptions) (ok bool, err error)
Expand All @@ -374,7 +375,7 @@ func Validate(dataFile, schemaFile string, opts *ValidateOptions) (ok bool, err
Validate validates the given data file against the specified schema file with the provided options.

<a name="ValidateCode"></a>
## func [ValidateCode](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L182>)
## func [ValidateCode](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L187>)

```go
func ValidateCode(data, code string, opts *ValidateOptions) (ok bool, err error)
Expand Down Expand Up @@ -731,7 +732,7 @@ type KclType = kcl.KclType
```

<a name="GetSchemaType"></a>
### func [GetSchemaType](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L210>)
### func [GetSchemaType](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L215>)

```go
func GetSchemaType(file, code, schemaName string) ([]*KclType, error)
Expand Down Expand Up @@ -830,7 +831,7 @@ func WithKFilenames(filenames ...string) Option
WithKFilenames returns a Option which hold a filenames list.

<a name="WithLogger"></a>
### func [WithLogger](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L128>)
### func [WithLogger](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L133>)

```go
func WithLogger(l io.Writer) Option
Expand Down Expand Up @@ -925,14 +926,23 @@ func WithSettings(filename string) Option

WithSettings returns a Option which hold a settings file.

<a name="WithShowHidden"></a>
### func [WithShowHidden](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L128>)

```go
func WithShowHidden(showHidden bool) Option
```

WithShowHidden returns a Option which holds a showHidden switch.

<a name="WithSortKeys"></a>
### func [WithSortKeys](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L123>)

```go
func WithSortKeys(sortKeys bool) Option
```

WithSortKeys returns a Option which hold a sortKeys switch.
WithSortKeys returns a Option which holds a sortKeys switch.

<a name="WithWorkDir"></a>
### func [WithWorkDir](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L107>)
Expand Down Expand Up @@ -971,7 +981,7 @@ type TestResult = testing.TestResult
```

<a name="Test"></a>
### func [Test](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L193>)
### func [Test](<https://github.com/kcl-lang/kcl-go/blob/main/kclvm.go#L198>)

```go
func Test(testOpts *TestOptions, opts ...Option) (TestResult, error)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v3 v3.0.1
kcl-lang.io/kpm v0.7.0
kcl-lang.io/lib v0.7.8
kcl-lang.io/lib v0.8.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 @@ -787,8 +787,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
kcl-lang.io/kpm v0.7.0 h1:WZ7JGCpUSHQVidOPp+BnHLDxyMkWZGPvPWDRyQuTeAE=
kcl-lang.io/kpm v0.7.0/go.mod h1:s9w+2itB6AcCNjdppZv2Tc9vGhKk/axtJ5XJYaWNR+0=
kcl-lang.io/lib v0.7.8 h1:kOYlrFqZkwzCG/CFs63cwbX3pPCNUci1Xvj7zqqnLno=
kcl-lang.io/lib v0.7.8/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY=
kcl-lang.io/lib v0.8.0-alpha.1 h1:ohKfF+U13EgkWAgZimZTrLrpFqciJfe5A8QjdKhAyMg=
kcl-lang.io/lib v0.8.0-alpha.1/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY=
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
12 changes: 11 additions & 1 deletion kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,26 @@ func WithIncludeSchemaTypePath(includeSchemaTypePath bool) Option {
return kcl.WithIncludeSchemaTypePath(includeSchemaTypePath)
}

// WithFullTypePath returns a Option which hold a include full type string in the `_type` attribute.
func WithFullTypePath(fullTypePath bool) Option {
return kcl.WithFullTypePath(fullTypePath)
}

// WithPrintOverridesAST returns a Option which hold a printOverridesAST switch.
func WithPrintOverridesAST(printOverridesAST bool) Option {
return kcl.WithPrintOverridesAST(printOverridesAST)
}

// WithSortKeys returns a Option which hold a sortKeys switch.
// WithSortKeys returns a Option which holds a sortKeys switch.
func WithSortKeys(sortKeys bool) Option {
return kcl.WithSortKeys(sortKeys)
}

// WithShowHidden returns a Option which holds a showHidden switch.
func WithShowHidden(showHidden bool) Option {
return kcl.WithShowHidden(showHidden)
}

// WithLogger returns a Option which hold a logger.
func WithLogger(l io.Writer) Option {
return kcl.WithLogger(l)
Expand Down
62 changes: 48 additions & 14 deletions kclvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestRunFiles(t *testing.T) {
}
}

func TestIndlu(t *testing.T) {
func TestWithTypePath(t *testing.T) {
const code = `
schema App:
image: str = "default"
Expand All @@ -81,24 +81,18 @@ a2 = App {
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()

const testdata_main_k = "test.k"
result, err := kcl.Run(testdata_main_k,
kcl.WithCode(code),
kcl.WithIncludeSchemaTypePath(true),
kcl.WithFullTypePath(true),
)
if err != nil {
t.Fatal(err)
}
if expect, got := "App", result.First().Get("a1._type"); expect != got {
if expect, got := "__main__.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 {
if expect, got := "__main__.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 {
Expand All @@ -108,8 +102,19 @@ a2 = App {
t.Fatalf("expect = %v, got = %v", expect, got)
}

os.Remove(testdata_main_k)
defer os.Remove(testdata_main_k)
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)
}
}

func TestWithOverrides(t *testing.T) {
Expand Down Expand Up @@ -529,10 +534,39 @@ func TestWithExternalpkg(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert2.Equal(t, "[{\"a\": \"Hello External_1 World!\", \"b\": \"Hello External_2 World!\"}]", result.GetRawJsonResult())
assert2.Equal(t, "{\"a\": \"Hello External_1 World!\", \"b\": \"Hello External_2 World!\"}", result.GetRawJsonResult())
assert2.Equal(t, "a: Hello External_1 World!\nb: Hello External_2 World!", result.GetRawYamlResult())
}

func TestWithSortKeys(t *testing.T) {
file, err := filepath.Abs("./testdata/test_plan/main.k")
if err != nil {
t.Fatal(err)
}
result, err := kcl.Run(file, kcl.WithSortKeys(true))
if err != nil {
t.Fatal(err)
}
assert2.Equal(t, "a: 2\nb: 1", result.GetRawYamlResult())
}

func TestWithShowHidden(t *testing.T) {
file, err := filepath.Abs("./testdata/test_plan/main.k")
if err != nil {
t.Fatal(err)
}
result, err := kcl.Run(file, kcl.WithShowHidden(true))
if err != nil {
t.Fatal(err)
}
assert2.Equal(t, "b: 1\na: 2\n_c: 3", result.GetRawYamlResult())
result, err = kcl.Run(file, kcl.WithShowHidden(true), kcl.WithSortKeys(true))
if err != nil {
t.Fatal(err)
}
assert2.Equal(t, "_c: 3\na: 2\nb: 1", result.GetRawYamlResult())
}

func TestWithLogger(t *testing.T) {
file, err := filepath.Abs("./testdata/test_print/main.k")
if err != nil {
Expand Down
29 changes: 23 additions & 6 deletions pkg/kcl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ func GetSchemaTypeMapping(file, code, schemaName string) (map[string]*gpyrpc.Kcl
}

func run(pathList []string, opts ...Option) (*KCLResultList, error) {
return runWithHooks(pathList, []Hook{
&typeAttributeHook{},
}, opts...)
}

func runWithHooks(pathList []string, hooks Hooks, opts ...Option) (*KCLResultList, error) {
args, err := ParseArgs(pathList, opts...)
if err != nil {
return nil, err
Expand All @@ -335,6 +341,9 @@ func run(pathList []string, opts ...Option) (*KCLResultList, error) {
if err != nil {
return nil, err
}
for _, hook := range hooks {
hook.Do(&args, resp)
}
// Output log message
logger := args.GetLogger()
if logger != nil && resp.LogMessage != "" {
Expand All @@ -359,15 +368,23 @@ func run(pathList []string, opts ...Option) (*KCLResultList, error) {
if err := json.Unmarshal([]byte(resp.JsonResult), &result.result); err != nil {
return nil, err
}
if err != nil {
return nil, err
}
}
result.list = make([]KCLResult, 0, len(mList))
for _, m := range mList {
if len(m) != 0 {

// Store raw result to KCLResult
if len(mList) == 0 && result.result != nil {
// Scalar or map result
m, err := result.ToMap()
if err == nil {
result.list = append(result.list, m)
}
} else {
// Stream result
result.list = make([]KCLResult, 0, len(mList))
for _, m := range mList {
if len(m) != 0 {
result.list = append(result.list, m)
}
}
}

result.raw_json_result = resp.JsonResult
Expand Down
Loading

0 comments on commit d03d3a9

Please sign in to comment.