Skip to content

Commit

Permalink
client test an records minimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Dec 17, 2024
1 parent 5d88565 commit c3f1d6b
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 176 deletions.
2 changes: 1 addition & 1 deletion cmd/kwil-cli/cmds/database/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type respCall struct {

func (r *respCall) MarshalJSON() ([]byte, error) {
if !r.PrintLogs {
return json.Marshal(r.Data.Records.ExportString()) // this is for backwards compatibility
return json.Marshal(r.Data.Records.ToStrings()) // this is for backwards compatibility
}

bts, err := json.Marshal(r.Data)
Expand Down
12 changes: 6 additions & 6 deletions cmd/kwil-cli/cmds/database/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func (d *respDBList) MarshalText() ([]byte, error) {
// of a database in cli
type respRelations struct {
// to avoid recursive call of MarshalJSON
Data *clientType.Records
Data clientType.Records
}

func (r *respRelations) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Data.Export())
return json.Marshal(r.Data)
}

func (r *respRelations) MarshalText() ([]byte, error) {
Expand All @@ -69,15 +69,15 @@ func (r *respRelations) MarshalText() ([]byte, error) {

// recordsToTable converts records to a formatted table structure
// that can be printed
func recordsToTable(r *clientType.Records) []byte {
data := r.ExportString()
func recordsToTable(r clientType.Records) []byte {
data := r.ToStrings()

if len(data) == 0 {
return []byte("No data to display.")
}

// collect headers
headers := make([]string, 0)
headers := make([]string, 0, len(data[0]))
for k := range data[0] {
headers = append(headers, k)
}
Expand All @@ -93,7 +93,7 @@ func recordsToTable(r *clientType.Records) []byte {
tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})

for _, row := range data {
rs := make([]string, 0)
rs := make([]string, 0, len(headers))
for _, h := range headers {
rs = append(rs, row[h])
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kwil-cli/cmds/database/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Example_respDBlist_json() {

func Example_respRelations_text() {
display.Print(&respRelations{
Data: clientType.NewRecordsFromMaps([]map[string]any{{"a": "1", "b": "2"}, {"a": "3", "b": "4"}})},
Data: clientType.Records([]map[string]any{{"a": "1", "b": "2"}, {"a": "3", "b": "4"}})},
nil, "text")
// Output:
// | a | b |
Expand All @@ -106,7 +106,7 @@ func Example_respRelations_text() {

func Example_respRelations_json() {
display.Print(&respRelations{
Data: clientType.NewRecordsFromMaps([]map[string]any{{"a": "1", "b": "2"}, {"a": "3", "b": "4"}})},
Data: clientType.Records([]map[string]any{{"a": "1", "b": "2"}, {"a": "3", "b": "4"}})},
nil, "json")
// Output:
// {
Expand Down
16 changes: 3 additions & 13 deletions core/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,6 @@ func (c *Client) Execute(ctx context.Context, dbid string, procedure string, tup
return c.txClient.Broadcast(ctx, tx, syncBcastFlag(txOpts.SyncBcast))
}

// DEPRECATED: Use Call instead.
func (c *Client) CallAction(ctx context.Context, dbid string, action string, inputs []any) (*clientType.Records, error) {
r, err := c.Call(ctx, dbid, action, inputs)
if err != nil {
return nil, err
}

return r.Records, nil
}

// Call calls a procedure or action. It returns the result records.
func (c *Client) Call(ctx context.Context, dbid string, procedure string, inputs []any) (*clientType.CallResult, error) {
encoded, err := encodeTuple(inputs)
Expand Down Expand Up @@ -361,19 +351,19 @@ func (c *Client) Call(ctx context.Context, dbid string, procedure string, inputs
}

return &clientType.CallResult{
Records: clientType.NewRecordsFromMaps(res),
Records: clientType.Records(res),
Logs: logs,
}, nil
}

// Query executes a query.
func (c *Client) Query(ctx context.Context, dbid string, query string) (*clientType.Records, error) {
func (c *Client) Query(ctx context.Context, dbid string, query string) (clientType.Records, error) {
res, err := c.txClient.Query(ctx, dbid, query)
if err != nil {
return nil, err
}

return clientType.NewRecordsFromMaps(res), nil
return clientType.Records(res), nil
}

// ListDatabases lists databases belonging to an owner.
Expand Down
8 changes: 2 additions & 6 deletions core/client/types/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,25 @@ import (

// Client defines methods are used to talk to a Kwil provider.
type Client interface {
// CallAction. Deprecated: Use Call instead.
CallAction(ctx context.Context, dbid string, action string, inputs []any) (*Records, error)
Call(ctx context.Context, dbid string, procedure string, inputs []any) (*CallResult, error)
ChainID() string
ChainInfo(ctx context.Context) (*types.ChainInfo, error)
DeployDatabase(ctx context.Context, payload *types.Schema, opts ...TxOpt) (types.Hash, error)
DropDatabase(ctx context.Context, name string, opts ...TxOpt) (types.Hash, error)
DropDatabaseID(ctx context.Context, dbid string, opts ...TxOpt) (types.Hash, error)
// DEPRECATED: Use Execute instead.
// ExecuteAction(ctx context.Context, dbid string, action string, tuples [][]any, opts ...TxOpt) (types.Hash, error)
Execute(ctx context.Context, dbid string, action string, tuples [][]any, opts ...TxOpt) (types.Hash, error)
GetAccount(ctx context.Context, pubKey []byte, status types.AccountStatus) (*types.Account, error)
GetSchema(ctx context.Context, dbid string) (*types.Schema, error)
ListDatabases(ctx context.Context, owner []byte) ([]*types.DatasetIdentifier, error)
Ping(ctx context.Context) (string, error)
Query(ctx context.Context, dbid string, query string) (*Records, error)
Query(ctx context.Context, dbid string, query string) (Records, error)
TxQuery(ctx context.Context, txHash types.Hash) (*types.TxQueryResponse, error)
WaitTx(ctx context.Context, txHash types.Hash, interval time.Duration) (*types.TxQueryResponse, error)
Transfer(ctx context.Context, to []byte, amount *big.Int, opts ...TxOpt) (types.Hash, error)
}

// CallResult is the result of a call to a procedure.
type CallResult struct {
Records *Records `json:"records"`
Records Records `json:"records"`
Logs []string `json:"logs,omitempty"`
}
145 changes: 0 additions & 145 deletions core/client/types/iter.go

This file was deleted.

30 changes: 30 additions & 0 deletions core/client/types/records.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package client

import (
"fmt"
)

// Records provides an export helper for a set of records.
type Records []map[string]any

// ToStrings converts the values in each map to strings.
func (r Records) ToStrings() []map[string]string {
if r == nil {
return nil
}

records := make([]map[string]string, len(r))
for i, record := range r {
records[i] = stringifyMap(record)
}
return records
}

// stringifyMap converts a Records instance into a map with the values converted to strings.
func stringifyMap(r map[string]any) map[string]string {
rec := make(map[string]string, len(r))
for k, v := range r {
rec[k] = fmt.Sprintf("%v", v)
}
return rec
}
2 changes: 1 addition & 1 deletion core/gatewayclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func NewClient(ctx context.Context, target string, opts *GatewayOptions) (*Gatew

// CallAction calls an action. It returns the result records. If authentication is needed,
// Deprecated: Use Call instead.
func (c *GatewayClient) CallAction(ctx context.Context, dbid string, action string, inputs []any) (*clientType.Records, error) {
func (c *GatewayClient) CallAction(ctx context.Context, dbid string, action string, inputs []any) (clientType.Records, error) {
r, err := c.Call(ctx, dbid, action, inputs)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit c3f1d6b

Please sign in to comment.