diff --git a/utils/array.go b/utils/array.go index 9ec8df39d596afd6e9d0297b2073e4f96364ede9..ec8b5d202e0b53f47f42e4e640ff82845874fe21 100644 --- a/utils/array.go +++ b/utils/array.go @@ -1,8 +1,8 @@ //////////////////////////////////////////////////////////////////////////////// -// Copyright © 2022 xx network SEZC // +// Copyright © 2022 xx foundation // // // // Use of this source code is governed by a license that can be found in the // -// LICENSE file // +// LICENSE file. // //////////////////////////////////////////////////////////////////////////////// package utils diff --git a/utils/convert.go b/utils/convert.go new file mode 100644 index 0000000000000000000000000000000000000000..293e5f3703b92028747781120ac29290be5faf2e --- /dev/null +++ b/utils/convert.go @@ -0,0 +1,46 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2022 xx foundation // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file. // +//////////////////////////////////////////////////////////////////////////////// + +package utils + +import ( + "encoding/json" + "syscall/js" +) + +// CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is +// a wrapper for js.CopyBytesToGo to make it more convenient. +func CopyBytesToGo(src js.Value) []byte { + b := make([]byte, src.Length()) + js.CopyBytesToGo(b, src) + return b +} + +// CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is +// a wrapper for js.CopyBytesToJS to make it more convenient. +func CopyBytesToJS(src []byte) js.Value { + dst := Uint8Array.New(len(src)) + js.CopyBytesToJS(dst, src) + return dst +} + +// JsonToJS converts a marshalled JSON bytes to a Javascript object. +func JsonToJS(src []byte) (js.Value, error) { + var inInterface map[string]interface{} + err := json.Unmarshal(src, &inInterface) + if err != nil { + Throw(TypeError, err) + return js.ValueOf(nil), err + } + + return js.ValueOf(inInterface), nil +} + +// JsToJson converts the Javascript value to JSON. +func JsToJson(value js.Value) string { + return JSON.Call("stringify", value).String() +} diff --git a/utils/convert_test.go b/utils/convert_test.go new file mode 100644 index 0000000000000000000000000000000000000000..78d50a7090ce3aec02edf703463f1057bc729574 --- /dev/null +++ b/utils/convert_test.go @@ -0,0 +1,100 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2022 xx foundation // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file. // +//////////////////////////////////////////////////////////////////////////////// + +package utils + +import ( + "reflect" + "syscall/js" + "testing" +) + +func TestCopyBytesToGo(t *testing.T) { + type args struct { + src js.Value + } + tests := []struct { + name string + args args + want []byte + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := CopyBytesToGo(tt.args.src); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CopyBytesToGo() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCopyBytesToJS(t *testing.T) { + type args struct { + src []byte + } + tests := []struct { + name string + args args + want js.Value + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := CopyBytesToJS(tt.args.src); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CopyBytesToJS() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestJsToJson(t *testing.T) { + type args struct { + value js.Value + } + tests := []struct { + name string + args args + want string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := JsToJson(tt.args.value); got != tt.want { + t.Errorf("JsToJson() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestJsonToJS(t *testing.T) { + type args struct { + src []byte + } + tests := []struct { + name string + args args + want js.Value + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := JsonToJS(tt.args.src) + if (err != nil) != tt.wantErr { + t.Errorf("JsonToJS() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("JsonToJS() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/utils/errors.go b/utils/errors.go index b599a4480f0fa57505f4a9182da09ff4cc75670c..2b3f15723462b21c3f7bccf549960f71f0d0046e 100644 --- a/utils/errors.go +++ b/utils/errors.go @@ -1,8 +1,8 @@ //////////////////////////////////////////////////////////////////////////////// -// Copyright © 2022 xx network SEZC // +// Copyright © 2022 xx foundation // // // // Use of this source code is governed by a license that can be found in the // -// LICENSE file // +// LICENSE file. // //////////////////////////////////////////////////////////////////////////////// package utils diff --git a/utils/errors_test.go b/utils/errors_test.go index 6f48e4609a3b33b10941336b332eb0f956ac2611..94b507d7c90d8a3d3a45465cec0437a1b3011c3e 100644 --- a/utils/errors_test.go +++ b/utils/errors_test.go @@ -8,26 +8,33 @@ package utils import ( + "fmt" "github.com/pkg/errors" - "syscall/js" "testing" ) +// Tests that TestJsError returns a Javascript Error object with the expected +// message. func TestJsError(t *testing.T) { - err := errors.Errorf("test error") - - jsError := JsError(err) - - t.Logf("%+v", jsError) - t.Logf("%+v", jsError.String()) - t.Logf("%+v", js.Error{Value: jsError}) + err := errors.New("test error") + expectedErr := err.Error() + jsError := JsError(err).Get("message").String() + + if jsError != expectedErr { + t.Errorf("Failed to get expected error message."+ + "\nexpected: %s\nreceived: %s", expectedErr, jsError) + } } +// Tests that TestJsTrace returns a Javascript Error object with the expected +// message and stack trace. func TestJsTrace(t *testing.T) { -} - -func TestThrow(t *testing.T) { -} - -func Test_throw(t *testing.T) { + err := errors.New("test error") + expectedErr := fmt.Sprintf("%+v", err) + jsError := JsTrace(err).Get("message").String() + + if jsError != expectedErr { + t.Errorf("Failed to get expected error message."+ + "\nexpected: %s\nreceived: %s", expectedErr, jsError) + } } diff --git a/utils/utils.go b/utils/utils.go index 816f4f67c1d795afbab16dc6b6fc4593cd6d5132..15dcc579dbc323d99e6b20b74664ce779d0c7193 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,10 +1,3 @@ -//////////////////////////////////////////////////////////////////////////////// -// Copyright © 2020 xx network SEZC // -// // -// Use of this source code is governed by a license that can be found in the // -// LICENSE file // -//////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////// // Copyright © 2022 xx foundation // // // @@ -17,7 +10,6 @@ package utils import ( - "encoding/json" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "syscall/js" @@ -30,22 +22,6 @@ var ( Uint8Array = js.Global().Get("Uint8Array") ) -// CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is -// a wrapper for js.CopyBytesToGo to make it more convenient. -func CopyBytesToGo(src js.Value) []byte { - b := make([]byte, src.Length()) - js.CopyBytesToGo(b, src) - return b -} - -// CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is -// a wrapper for js.CopyBytesToJS to make it more convenient. -func CopyBytesToJS(src []byte) js.Value { - dst := Uint8Array.New(len(src)) - js.CopyBytesToJS(dst, src) - return dst -} - // WrapCB wraps a Javascript function in an object so that it can be called // later with only the arguments and without specifying the function name. // @@ -62,23 +38,6 @@ func WrapCB(parent js.Value, m string) func(args ...interface{}) js.Value { } } -// JsonToJS converts a marshalled JSON bytes to a Javascript object. -func JsonToJS(src []byte) (js.Value, error) { - var inInterface map[string]interface{} - err := json.Unmarshal(src, &inInterface) - if err != nil { - Throw(TypeError, err) - return js.ValueOf(nil), err - } - - return js.ValueOf(inInterface), nil -} - -// JsToJson converts the Javascript value to JSON. -func JsToJson(value js.Value) string { - return JSON.Call("stringify", value).String() -} - type PromiseFn func(resolve, reject func(args ...interface{}) js.Value) // CreatePromise creates a Javascript promise to return the value of a blocking