diff --git a/sjson/dec.go b/cjson/dec.go similarity index 86% rename from sjson/dec.go rename to cjson/dec.go index 269da02..84d4d2e 100644 --- a/sjson/dec.go +++ b/cjson/dec.go @@ -1,4 +1,4 @@ -package sjson +package cjson import "github.com/d5/tengo/v2" import "encoding/json" @@ -28,18 +28,20 @@ func NewDecoder(args ...tengo.Object) (tengo.Object, error) { inputName = StrStdin } else if len(args) > 1 { return nil, tengo.ErrWrongNumArguments - } - - inputObject := args[0] - inputName, ok := tengo.ToString(inputObject) - if !ok { - return nil, tengo.ErrInvalidArgumentType{ - Name: "first", - Expected: "stringer", - Found: inputObject.TypeName(), + } else { + var ok bool + inputObject := args[0] + inputName, ok = tengo.ToString(inputObject) + if !ok { + return nil, tengo.ErrInvalidArgumentType{ + Name: "first", + Expected: "stringer", + Found: inputObject.TypeName(), + } } } + var reader io.Reader var err error switch { diff --git a/sjson/sjson.go b/cjson/main.go similarity index 97% rename from sjson/sjson.go rename to cjson/main.go index f53e04c..8c16261 100644 --- a/sjson/sjson.go +++ b/cjson/main.go @@ -1,4 +1,4 @@ -package sjson +package cjson import "github.com/d5/tengo/v2" import "io" diff --git a/tests/cjson.xgo b/tests/cjson.xgo new file mode 100644 index 0000000..0af89fa --- /dev/null +++ b/tests/cjson.xgo @@ -0,0 +1,16 @@ +cjson := import("cjson") +fmt := import("fmt") +fmt.println(cjson.__check()) + +dec := cjson.new_decoder("tests/data.cjson") + +for { + v := dec.decode() + if is_error(v) { + break + } + if !v { + break + } + fmt.println(v) +} diff --git a/tests/data.cjson b/tests/data.cjson new file mode 100644 index 0000000..187ebe4 --- /dev/null +++ b/tests/data.cjson @@ -0,0 +1,4 @@ +{"somename": { + "value": 1 +}} +["array",135,"integers", 135, 135] diff --git a/tests/sjson.xgo b/tests/sjson.xgo deleted file mode 100644 index cc499c3..0000000 --- a/tests/sjson.xgo +++ /dev/null @@ -1,13 +0,0 @@ -sjson := import("sjson") -fmt := import("fmt") -fmt.println(sjson.__check()) - -dec := sjson.new_decoder("") - -for { - v := dec.decode() - if !v { - break - } - fmt.println(v) -} diff --git a/xtool/main.go b/xtool/main.go index 461b688..498f404 100644 --- a/xtool/main.go +++ b/xtool/main.go @@ -19,7 +19,7 @@ import ( ) import "surdeus.su/core/cli/mtool" -import "surdeus.su/core/xgo/sjson" +import "surdeus.su/core/xgo/cjson" const ( sourceFileExt = ".xgo" @@ -53,7 +53,7 @@ func Run(flags *mtool.Flags) { return } modules := stdlib.GetModuleMap(stdlib.AllModuleNames()...) - modules.AddBuiltinModule("sjson", sjson.Module) + modules.AddBuiltinModule("cjson", cjson.Module) if len(iargs) == 0 { // REPL RunREPL(modules, os.Stdin, os.Stdout)