Skip to content

Commit 57e4557

Browse files
committed
dev: enhanse custom marshaler
1 parent 00273b4 commit 57e4557

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

pkg/apis/clickhouse.altinity.com/v1/type_settings.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,14 @@ func (settings Settings) MarshalJSON() ([]byte, error) {
168168

169169
// unmarshalScalar
170170
func unmarshalScalar(untyped interface{}) (string, bool) {
171+
var res string
172+
var ok bool
173+
171174
typeOf := reflect.TypeOf(untyped)
172-
str := typeOf.String()
173-
log.V(3).Infof("%v", str)
175+
if typeOf == nil {
176+
log.V(3).Infof("unmarshalScalar() typeOf==nil")
177+
return "", false
178+
}
174179

175180
switch untyped.(type) {
176181
case // scalar
@@ -181,39 +186,57 @@ func unmarshalScalar(untyped interface{}) (string, bool) {
181186
int64, uint64,
182187
bool,
183188
string:
184-
return fmt.Sprintf("%v", untyped), true
189+
res = fmt.Sprintf("%v", untyped)
190+
ok = true
185191
case // scalar
186192
float32:
187193
floatVal := untyped.(float32)
188194
_, frac := math.Modf(float64(floatVal))
189-
if frac < ignoreThreshold {
190-
// consider it int
195+
if frac > ignoreThreshold {
196+
// Consider it float
197+
res = fmt.Sprintf("%f", untyped)
198+
} else {
199+
// Consider it int
191200
intVal := int64(floatVal)
192-
return fmt.Sprintf("%v", intVal), true
201+
res = fmt.Sprintf("%v", intVal)
193202
}
194-
return fmt.Sprintf("%f", untyped), true
203+
ok = true
195204
case // scalar
196205
float64:
197206
floatVal := untyped.(float64)
198207
_, frac := math.Modf(floatVal)
199-
if frac < ignoreThreshold {
200-
// consider it int
208+
if frac > ignoreThreshold {
209+
// Consider it float
210+
res = fmt.Sprintf("%f", untyped)
211+
} else {
212+
// Consider it int
201213
intVal := int64(floatVal)
202-
return fmt.Sprintf("%v", intVal), true
214+
res = fmt.Sprintf("%v", intVal)
203215
}
204-
return fmt.Sprintf("%f", untyped), true
216+
ok = true
205217
}
206218

207-
return "", false
219+
str := typeOf.String()
220+
if ok {
221+
log.V(3).Infof("unmarshalScalar() type=%v value=%s", str, res)
222+
return res, true
223+
} else {
224+
log.V(3).Infof("unmarshalScalar() type=%v - UNABLE to unmarshal", str)
225+
return "", false
226+
}
208227
}
209228

210229
// unmarshalVector
211230
func unmarshalVector(untyped interface{}) ([]string, bool) {
231+
var res []string
232+
var ok bool
233+
212234
typeOf := reflect.TypeOf(untyped)
213-
str := typeOf.String()
214-
log.V(3).Infof("%v", str)
235+
if typeOf == nil {
236+
log.V(3).Infof("unmarshalVector() typeOf==nil")
237+
return nil, false
238+
}
215239

216-
var res []string
217240
switch untyped.(type) {
218241
case // vector
219242
[]interface{}:
@@ -222,10 +245,17 @@ func unmarshalVector(untyped interface{}) ([]string, bool) {
222245
res = append(res, scalar)
223246
}
224247
}
225-
return res, true
248+
ok = true
226249
}
227250

228-
return nil, false
251+
str := typeOf.String()
252+
if ok {
253+
log.V(3).Infof("unmarshalVector() type=%v value=%s", str, res)
254+
return res, true
255+
} else {
256+
log.V(3).Infof("unmarshalVector type=%v - UNABLE to unmarshal", str)
257+
return nil, false
258+
}
229259
}
230260

231261
// getValueAsScalar

0 commit comments

Comments
 (0)