From f00e98585088ad4c8fca421144bf08eaa3bb3563 Mon Sep 17 00:00:00 2001 From: Victor Lyuboslavsky Date: Wed, 21 Aug 2024 10:54:42 -0500 Subject: [PATCH] Small optimization for #21073 (#21459) Small optimization for #21073 --- server/fleet/mdm.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/server/fleet/mdm.go b/server/fleet/mdm.go index ac118082aa..140d2307f5 100644 --- a/server/fleet/mdm.go +++ b/server/fleet/mdm.go @@ -753,18 +753,23 @@ func (c *MDMCommandsAlreadySent) Scan(src interface{}) error { if src == nil { return nil } - var commands MDMCommandsAlreadySent - if err := json.Unmarshal(src.([]byte), &commands); err != nil { - return err - } + raw, ok := src.([]byte) + if !ok { + return fmt.Errorf("unexpected type for MDMCommandsAlreadySent: %T", src) + } // Filter out [null] command types which MySQL returns when there are no commands_already_sent. // For details, see: https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_json-arrayagg - if len(commands) == 1 && commands[0] == "" { + if string(raw) == "[null]" { *c = nil - } else { - *c = commands + return nil } + + var commands MDMCommandsAlreadySent + if err := json.Unmarshal(raw, &commands); err != nil { + return err + } + *c = commands return nil }