Skip to content

Commit

Permalink
Merge branch 'master' into refactor-how-we-calculate-time
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Aug 14, 2024
2 parents ffb3987 + cbf0bc0 commit ac263fa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gha-go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
go-version: 1.22
- name: Run staticcheck
env:
SC_VERSION: 2023.1.7
SC_VERSION: "2024.1"
run: |
make generate
SC_URL="https://github.com/dominikh/go-tools/releases/download/$SC_VERSION/staticcheck_linux_amd64.tar.gz"
Expand Down
15 changes: 8 additions & 7 deletions clients/bigquery/storagewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ func rowToMessage(row map[string]any, columns []columns.Column, messageDescripto

switch column.KindDetails.Kind {
case typing.Boolean.Kind:
if boolValue, ok := value.(bool); ok {
message.Set(field, protoreflect.ValueOfBool(boolValue))
} else {
return nil, fmt.Errorf("expected bool received %T with value %v", value, value)
boolValue, err := typing.AssertType[bool](value)
if err != nil {
return nil, err
}

message.Set(field, protoreflect.ValueOfBool(boolValue))
case typing.Integer.Kind:
switch value := value.(type) {
case int:
Expand Down Expand Up @@ -160,11 +161,11 @@ func rowToMessage(row map[string]any, columns []columns.Column, messageDescripto
}
case typing.String.Kind:
var stringValue string
switch value := value.(type) {
switch castedValue := value.(type) {
case string:
stringValue = value
stringValue = castedValue
case *decimal.Decimal:
stringValue = value.String()
stringValue = castedValue.String()
default:
return nil, fmt.Errorf("expected string/decimal.Decimal received %T with value %v", value, value)
}
Expand Down
12 changes: 12 additions & 0 deletions lib/typing/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package typing

import "fmt"

func AssertType[T any](val any) (T, error) {
castedVal, isOk := val.(T)
if !isOk {
var zero T
return zero, fmt.Errorf("expected type %T, got %T", zero, val)
}
return castedVal, nil
}
32 changes: 32 additions & 0 deletions lib/typing/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package typing

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAssertType(t *testing.T) {
{
// String to string
val, err := AssertType[string]("hello")
assert.NoError(t, err)
assert.Equal(t, "hello", val)
}
{
// Int to string
_, err := AssertType[string](1)
assert.ErrorContains(t, err, "expected type string, got int")
}
{
// Boolean to boolean
val, err := AssertType[bool](true)
assert.NoError(t, err)
assert.Equal(t, true, val)
}
{
// String to boolean
_, err := AssertType[bool]("true")
assert.ErrorContains(t, err, "expected type bool, got string")
}
}

0 comments on commit ac263fa

Please sign in to comment.