Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support logger opts merge #187

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ KCL Go SDK
package kclvm

import (
"io"

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/kclvm_runtime"
"kcl-lang.io/kcl-go/pkg/tools/format"
Expand Down Expand Up @@ -122,6 +124,11 @@ func WithSortKeys(sortKeys bool) Option {
return kcl.WithSortKeys(sortKeys)
}

// WithLogger returns a Option which hold a logger.
func WithLogger(l io.Writer) Option {
return kcl.WithLogger(l)
}

// FormatCode returns the formatted code.
func FormatCode(code interface{}) ([]byte, error) {
return format.FormatCode(code)
Expand Down
15 changes: 15 additions & 0 deletions kclvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package kclvm_test

import (
"bytes"
"flag"
"os"
"path/filepath"
Expand Down Expand Up @@ -489,3 +490,17 @@ func TestWithExternalpkg(t *testing.T) {
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 TestWithLogger(t *testing.T) {
file, err := filepath.Abs("./testdata/test_print/main.k")
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
result, err := kcl.Run(file, kcl.WithLogger(&buf))
if err != nil {
t.Fatal(err)
}
assert2.Equal(t, "hello: world", result.GetRawYamlResult())
assert2.Equal(t, "Hello world\n", buf.String())
}
4 changes: 3 additions & 1 deletion pkg/kcl/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ func (p *Option) Merge(opts ...Option) *Option {
if opt.ExternalPkgs != nil {
p.ExternalPkgs = append(p.ExternalPkgs, opt.ExternalPkgs...)
}
p.logger = opt.logger
if opt.logger != nil {
p.logger = opt.logger
}
}
return p
}
3 changes: 3 additions & 0 deletions testdata/test_print/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "test_print"

2 changes: 2 additions & 0 deletions testdata/test_print/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello = "world"
print("Hello world")
Loading