Skip to content

Commit

Permalink
wip(typed key-value): failed to regress, need to check Stdio.ReceiveW…
Browse files Browse the repository at this point in the history
…ithPostProcess
  • Loading branch information
KevRiver committed Dec 13, 2024
1 parent ebf2662 commit 0515aed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
21 changes: 15 additions & 6 deletions pkg/sink/stdiosink/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ func (std *Stdio) ReceiveWithPostProcess(ctx context.Context, ev *typesv1.LogEve
kvs := make(map[string]string, len(data.Kvs))
for _, kv := range data.Kvs {
key := kv.Key
// value could be one of the following:
// - string
// - int64
// - float64
// - bool
// - time.Time
// - time.Duration
// - []any
// - map[string]any
value, err := logql.ResolveVal(kv.Value, logql.MakeFlatGoMap, logql.MakeFlatMapGoSlice)
if err != nil {
return err
Expand All @@ -249,17 +258,17 @@ func toString(value *typesv1.Val) (string, error) {
}
switch t := v.(type) {
case string:
return t, nil
return fmt.Sprintf("%q", t), nil
case int64:
return fmt.Sprintf("%d", t), nil
case float64:
return fmt.Sprintf("%g", t), nil
case bool:
return fmt.Sprintf("%t", t), nil
case time.Time:
return t.Format(time.RFC3339Nano), nil
return fmt.Sprintf("%q", t.Format(time.RFC3339Nano)), nil
case time.Duration:
return t.String(), nil
return fmt.Sprintf("%q", t.String()), nil
default:
return "", fmt.Errorf("unsupported type: %T", t)
}
Expand All @@ -268,17 +277,17 @@ func toString(value *typesv1.Val) (string, error) {
func put(ref *map[string]string, key string, value any) {
switch t := value.(type) {
case string:
(*ref)[key] = t
(*ref)[key] = fmt.Sprintf("%q", t)
case int64:
(*ref)[key] = fmt.Sprintf("%d", t)
case float64:
(*ref)[key] = fmt.Sprintf("%g", t)
case bool:
(*ref)[key] = fmt.Sprintf("%t", t)
case time.Time:
(*ref)[key] = t.Format(time.RFC3339Nano)
(*ref)[key] = fmt.Sprintf("%q", t.Format(time.RFC3339Nano))
case time.Duration:
(*ref)[key] = t.String()
(*ref)[key] = fmt.Sprintf("%q", t.String())
case map[string]any:
for k, v := range t {
put(ref, key+"."+k, v)
Expand Down
13 changes: 8 additions & 5 deletions pkg/sink/stdiosink/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stdiosink

import (
"testing"
"time"

"github.com/humanlogio/api/go/pkg/logql"
typesv1 "github.com/humanlogio/api/go/types/v1"
Expand Down Expand Up @@ -38,23 +39,25 @@ func TestPutKV(t *testing.T) {
typesv1.ValF64(float64(5.3)),
typesv1.ValBool(true),
),
typesv1.ValTime(time.Date(2024, 12, 13, 19, 36, 0, 0, time.UTC)),
),
))),
want: map[string]string{
"root.a": "lorem",
"root.a": "\"lorem\"",
"root.b": "1",
"root.c": "3.14",
"root.d": "true",
"root.e.f": "foo",
"root.g.0": "bar",
"root.e.f": "\"foo\"",
"root.g.0": "\"bar\"",
"root.g.1": "2",
"root.g.2": "4.2",
"root.g.3": "false",
"root.g.4.h": "baz",
"root.g.5.0": "qux",
"root.g.4.h": "\"baz\"",
"root.g.5.0": "\"qux\"",
"root.g.5.1": "3",
"root.g.5.2": "5.3",
"root.g.5.3": "true",
"root.g.6": "\"2024-12-13T19:36:00Z\"",
},
},
}
Expand Down

0 comments on commit 0515aed

Please sign in to comment.