Skip to content

Commit

Permalink
feat: support logger opts to merge
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Nov 27, 2023
1 parent 0b7e384 commit 4dd132d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
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")

0 comments on commit 4dd132d

Please sign in to comment.