Skip to content

Commit

Permalink
refactor: move the json.Marshal into BuildJSON makes it easier to use
Browse files Browse the repository at this point in the history
  • Loading branch information
daviderli614 committed Nov 12, 2024
1 parent acfc40f commit beafd99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 1 addition & 6 deletions examples/jsondata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"context"
"encoding/json"
"log"
"time"

Expand Down Expand Up @@ -88,10 +87,6 @@ func initData() ([]*table.Table, error) {
IsStudent: false,
Courses: []string{"math", "history", "chemistry"},
}
jsonData, err := json.Marshal(p)
if err != nil {
return nil, err
}

// add column at first. This is to define the schema of the table.
if err := itbl.AddFieldColumn("my_json", types.JSON); err != nil {
Expand All @@ -100,7 +95,7 @@ func initData() ([]*table.Table, error) {
if err := itbl.AddTimestampColumn("timestamp", types.TIMESTAMP_MICROSECOND); err != nil {
return nil, err
}
if err := itbl.AddRow(string(jsonData), time1); err != nil {
if err := itbl.AddRow(p, time1); err != nil {
return nil, err
}

Expand Down
7 changes: 6 additions & 1 deletion table/cell/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cell

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -499,7 +500,11 @@ func BuildJSON(v any) (*gpb.Value, error) {
case *string:
val = *t
default:
return nil, fmt.Errorf(formatter+"string", t, v)
jsonData, err := json.Marshal(v)
if err != nil {
return nil, err
}
val = string(jsonData)
}

return &gpb.Value{ValueData: &gpb.Value_StringValue{StringValue: val}}, nil
Expand Down

0 comments on commit beafd99

Please sign in to comment.