This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go thunder/livesql: serialize and restore sqlgen.Tester
This commit adds conversion between thunderpb.SQLFilter and sqlgen.Tester. It takes each primitive typed valued in filter and casts them into canonical types so that equality check in tester still works after we convert serialized filter back into tester.
- Loading branch information
1 parent
a22b4d3
commit 7547ade
Showing
3 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package livesql | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/samsarahq/thunder/sqlgen" | ||
"github.com/samsarahq/thunder/thunderpb" | ||
) | ||
|
||
func valueToField(value interface{}) (*thunderpb.Field, error) { | ||
switch column := sqlgen.MakeCanonical(value).(type) { | ||
case nil: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_Null}, nil | ||
case bool: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_Bool, Bool: column}, nil | ||
case int64: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_Int, Int: column}, nil | ||
case uint64: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_Uint, Uint: column}, nil | ||
case string: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_String, String_: column}, nil | ||
case []byte: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_Bytes, Bytes: column}, nil | ||
case float64: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_Float64, Float64: column}, nil | ||
case time.Time: | ||
return &thunderpb.Field{Kind: thunderpb.FieldKind_Time, Time: column}, nil | ||
default: | ||
return nil, fmt.Errorf("unknown type %s", reflect.TypeOf(column)) | ||
} | ||
} | ||
|
||
func fieldToValue(field *thunderpb.Field) (interface{}, error) { | ||
switch field.Kind { | ||
case thunderpb.FieldKind_Null: | ||
return nil, nil | ||
case thunderpb.FieldKind_Bool: | ||
return field.Bool, nil | ||
case thunderpb.FieldKind_Int: | ||
return field.Int, nil | ||
case thunderpb.FieldKind_Uint: | ||
return field.Uint, nil | ||
case thunderpb.FieldKind_String: | ||
return field.String, nil | ||
case thunderpb.FieldKind_Bytes: | ||
return field.Bytes, nil | ||
case thunderpb.FieldKind_Float64: | ||
return field.Float64, nil | ||
case thunderpb.FieldKind_Time: | ||
return field.Time, nil | ||
default: | ||
return nil, fmt.Errorf("unknown kind %s", field.Kind.String()) | ||
} | ||
} | ||
|
||
func filterToProto(table string, filter sqlgen.Filter) (*thunderpb.SQLFilter, error) { | ||
fields := make(map[string]*thunderpb.Field, len(filter)) | ||
for col, val := range filter { | ||
field, err := valueToField(val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fields[col] = field | ||
} | ||
return &thunderpb.SQLFilter{Table: table, Fields: fields}, nil | ||
} | ||
|
||
func filterFromProto(proto *thunderpb.SQLFilter) (string, sqlgen.Filter, error) { | ||
filter := make(sqlgen.Filter, len(proto.Fields)) | ||
for col, field := range proto.Fields { | ||
val, err := fieldToValue(field) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
filter[col] = val | ||
} | ||
return proto.Table, filter, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters