@@ -20,6 +20,7 @@ import (
20
20
"fmt"
21
21
"io"
22
22
"os"
23
+ "reflect"
23
24
"strings"
24
25
25
26
"github.com/PaesslerAG/jsonpath"
@@ -108,6 +109,40 @@ func (opts *OutputOpts) IsCygwinTerminal() bool {
108
109
return terminal .IsCygwinTerminal (opts .OutWriter )
109
110
}
110
111
112
+ func isNil (o any ) bool {
113
+ if o == nil {
114
+ return true
115
+ }
116
+ ot := reflect .TypeOf (o )
117
+ otk := ot .Kind ()
118
+ switch otk { //nolint:exhaustive // clearer code
119
+ case reflect .Array , reflect .Slice , reflect .Map , reflect .Chan , reflect .Pointer , reflect .UnsafePointer , reflect .Interface :
120
+ return reflect .ValueOf (o ).IsNil ()
121
+ default :
122
+ return false
123
+ }
124
+ }
125
+
126
+ func isOrPtrToSliceOrArray (o any ) bool {
127
+ ot := reflect .TypeOf (o )
128
+ if ot == nil {
129
+ return false
130
+ }
131
+ otk := ot .Kind ()
132
+ switch otk { //nolint:exhaustive // clearer code
133
+ case reflect .Array , reflect .Slice :
134
+ return true
135
+ case reflect .Pointer :
136
+ opt := reflect .PointerTo (ot )
137
+ optk := opt .Kind ()
138
+ switch optk { //nolint:exhaustive // clearer code
139
+ case reflect .Array , reflect .Slice :
140
+ return true
141
+ }
142
+ }
143
+ return false
144
+ }
145
+
111
146
// Print will evaluate the defined format and try to parse it accordingly outputting to the set writer.
112
147
func (opts * OutputOpts ) Print (o any ) error {
113
148
if opts .ConfigOutput () == jsonFormat {
@@ -125,6 +160,13 @@ func (opts *OutputOpts) Print(o any) error {
125
160
}
126
161
127
162
if t != "" {
163
+ if isNil (o ) {
164
+ if isOrPtrToSliceOrArray (o ) {
165
+ o = []map [string ]any {}
166
+ } else {
167
+ o = map [string ]any {}
168
+ }
169
+ }
128
170
return templatewriter .Print (opts .ConfigWriter (), t , o )
129
171
}
130
172
_ , err = fmt .Fprintln (opts .ConfigWriter (), o )
0 commit comments