Skip to content

Commit ff4c8fb

Browse files
authored
Support logging as json all kind of goja's objects (#2511)
* Support logging as json all kind of goja's objects * More tests for console.Log supported types
1 parent eff2c7d commit ff4c8fb

File tree

2 files changed

+84
-29
lines changed

2 files changed

+84
-29
lines changed

js/console.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ package js
2323
import (
2424
"encoding/json"
2525
"os"
26-
"reflect"
2726
"strings"
2827

2928
"github.com/dop251/goja"
@@ -97,17 +96,12 @@ func (c console) Error(args ...goja.Value) {
9796
}
9897

9998
func (c console) valueString(v goja.Value) string {
100-
exptype := v.ExportType()
101-
if exptype == nil {
99+
mv, ok := v.(json.Marshaler)
100+
if !ok {
102101
return v.String()
103102
}
104103

105-
kind := exptype.Kind()
106-
if kind != reflect.Map && kind != reflect.Slice {
107-
return v.String()
108-
}
109-
110-
b, err := json.Marshal(v)
104+
b, err := json.Marshal(mv)
111105
if err != nil {
112106
return v.String()
113107
}

js/console_test.go

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
/*
2-
*
3-
* k6 - a next-generation load testing tool
4-
* Copyright (C) 2016 Load Impact
5-
*
6-
* This program is free software: you can redistribute it and/or modify
7-
* it under the terms of the GNU Affero General Public License as
8-
* published by the Free Software Foundation, either version 3 of the
9-
* License, or (at your option) any later version.
10-
*
11-
* This program is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
* GNU Affero General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU Affero General Public License
17-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18-
*
19-
*/
20-
211
package js
222

233
import (
@@ -107,6 +87,87 @@ func extractLogger(fl logrus.FieldLogger) *logrus.Logger {
10787
return nil
10888
}
10989

90+
func TestConsoleLogWithGojaNativeObject(t *testing.T) {
91+
t.Parallel()
92+
93+
rt := goja.New()
94+
rt.SetFieldNameMapper(common.FieldNameMapper{})
95+
96+
obj := rt.NewObject()
97+
err := obj.Set("text", "nativeObject")
98+
require.NoError(t, err)
99+
100+
logger := testutils.NewLogger(t)
101+
hook := logtest.NewLocal(logger)
102+
103+
c := newConsole(logger)
104+
c.Log(obj)
105+
106+
entry := hook.LastEntry()
107+
require.NotNil(t, entry, "nothing logged")
108+
assert.JSONEq(t, `{"text":"nativeObject"}`, entry.Message)
109+
}
110+
111+
func TestConsoleLogObjectsWithGoTypes(t *testing.T) {
112+
t.Parallel()
113+
114+
type value struct {
115+
Text string
116+
}
117+
118+
tests := []struct {
119+
name string
120+
in interface{}
121+
exp string
122+
}{
123+
{
124+
name: "StructLiteral",
125+
in: value{
126+
Text: "test1",
127+
},
128+
exp: `{"text":"test1"}`,
129+
},
130+
{
131+
name: "StructPointer",
132+
in: &value{
133+
Text: "test2",
134+
},
135+
exp: `{"text":"test2"}`,
136+
},
137+
{
138+
name: "Map",
139+
in: map[string]interface{}{
140+
"text": "test3",
141+
},
142+
exp: `{"text":"test3"}`,
143+
},
144+
}
145+
146+
expFields := logrus.Fields{"source": "console"}
147+
for _, tt := range tests {
148+
tt := tt
149+
150+
t.Run(tt.name, func(t *testing.T) {
151+
t.Parallel()
152+
153+
rt := goja.New()
154+
rt.SetFieldNameMapper(common.FieldNameMapper{})
155+
obj := rt.ToValue(tt.in)
156+
157+
logger := testutils.NewLogger(t)
158+
hook := logtest.NewLocal(logger)
159+
160+
c := newConsole(logger)
161+
c.Log(obj)
162+
163+
entry := hook.LastEntry()
164+
require.NotNil(t, entry, "nothing logged")
165+
assert.JSONEq(t, tt.exp, entry.Message)
166+
assert.Equal(t, expFields, entry.Data)
167+
})
168+
}
169+
}
170+
110171
func TestConsoleLog(t *testing.T) {
111172
t.Parallel()
112173

0 commit comments

Comments
 (0)