Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions platform/model/query_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ package model
import (
"context"
"fmt"
"reflect"
"slices"
"strings"
"time"

"github.com/QuesmaOrg/quesma/platform/common_table"
"github.com/QuesmaOrg/quesma/platform/logger"
"github.com/QuesmaOrg/quesma/platform/schema"
"github.com/QuesmaOrg/quesma/platform/util"
"github.com/goccy/go-json"
"reflect"
"slices"
"strings"
"time"
)

type (
Expand All @@ -38,23 +39,25 @@ func (c *QueryResultCol) String(ctx context.Context) string {
if valueExtracted == nil {
return ""
}
switch valueExtracted.(type) {
switch v := valueExtracted.(type) {
case string:
processed, err := json.Marshal(valueExtracted)
processed, err := json.Marshal(v)
if err != nil {
logger.ErrorWithCtx(ctx).Err(err).Msgf("failed to marshal value %v", valueExtracted)
logger.ErrorWithCtx(ctx).Err(err).Msgf("failed to marshal value %v", v)
}
return fmt.Sprintf(`"%s": %s`, c.ColName, string(processed))
case time.Time:
return fmt.Sprintf(`"%s": "%v"`, c.ColName, valueExtracted)
// Format timestamp with consistent microsecond precision
formattedTime := v.Format("2006-01-02 15:04:05.000000 -0700 MST")
return fmt.Sprintf(`"%s": "%s"`, c.ColName, formattedTime)
case int, int64, float64, uint64, bool:
return fmt.Sprintf(`"%s": %v`, c.ColName, valueExtracted)
return fmt.Sprintf(`"%s": %v`, c.ColName, v)
default:
// Probably good to only use marshaller when necessary, so for arrays/maps,
// and try to handle simple cases without it
marshalled, err := json.Marshal(valueExtracted)
marshalled, err := json.Marshal(v)
if err != nil {
logger.ErrorWithCtx(ctx).Err(err).Msgf("failed to marshal value %v", valueExtracted)
logger.ErrorWithCtx(ctx).Err(err).Msgf("failed to marshal value %v", v)
}
return fmt.Sprintf(`"%s": %v`, c.ColName, string(marshalled))
}
Expand Down
9 changes: 6 additions & 3 deletions platform/model/query_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ package model

import (
"context"
"github.com/QuesmaOrg/quesma/platform/util"
"strconv"
"testing"
"time"

"github.com/QuesmaOrg/quesma/platform/util"
)

func TestQueryResultCol_String(t *testing.T) {
Expand All @@ -33,17 +34,19 @@ func TestQueryResultCol_String(t *testing.T) {
value any
expected string
}{
{time.Date(2023, 10, 5, 14, 30, 45, 000000000, time.UTC), `"name": "2023-10-05 14:30:45.000000 +0000 UTC"`},
{time.Date(2023, 10, 5, 14, 30, 45, 123456000, time.UTC), `"name": "2023-10-05 14:30:45.123456 +0000 UTC"`},
{"test", `"name": "test"`},
{`test "GET"`, `"name": "test \"GET\""`},
{1, `"name": 1`},
{1.0, `"name": 1`},
{int64(1), `"name": 1`},
{uint64(1), `"name": 1`},
{true, `"name": true`},
{time.Time{}, `"name": "0001-01-01 00:00:00 +0000 UTC"`},
{time.Time{}, `"name": "0001-01-01 00:00:00.000000 +0000 UTC"`},
{strPtr, `"name": ""`},
{strPtrNil, ``},
{timePtr, `"name": "0001-01-01 00:00:00 +0000 UTC"`},
{timePtr, `"name": "0001-01-01 00:00:00.000000 +0000 UTC"`},
{timePtrNil, ``},
{int64Ptr, `"name": 1`},
{int64PtrNil, ``},
Expand Down
Loading