-
Notifications
You must be signed in to change notification settings - Fork 577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix panic on format nil *fmt.Stringer type value #1200
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,84 @@ | ||
package issues | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ClickHouse/clickhouse-go/v2" | ||
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test1200(t *testing.T) { | ||
var ( | ||
conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{ | ||
"max_execution_time": 60, | ||
"allow_experimental_object_type": true, | ||
}, nil, &clickhouse.Compression{ | ||
Method: clickhouse.CompressionLZ4, | ||
}) | ||
) | ||
ctx := context.Background() | ||
require.NoError(t, err) | ||
const ddl = "CREATE TABLE test_1200 (id UInt32, null_str Nullable(FixedString(5))) Engine MergeTree() ORDER BY tuple()" | ||
require.NoError(t, conn.Exec(ctx, ddl)) | ||
defer func() { | ||
conn.Exec(ctx, "DROP TABLE IF EXISTS test_1200") | ||
}() | ||
|
||
v := "value" | ||
|
||
tests := []struct { | ||
name string | ||
value fmt.Stringer | ||
want *string | ||
}{ | ||
{ | ||
name: "fmt.Stringer implemented struct value", | ||
value: Test1200NullStr{underlying: v}, | ||
want: &v, | ||
}, | ||
{ | ||
name: "nil value", | ||
value: nil, | ||
want: nil, | ||
}, | ||
{ | ||
name: "fmt.Stringer implemented struct pointer value", | ||
value: &Test1200NullStr{underlying: v}, | ||
want: &v, | ||
}, | ||
{ | ||
name: "fmt.Stringer implemented struct typed-nil value", | ||
value: (*Test1200NullStr)(nil), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, panic this case.
|
||
want: nil, | ||
}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
id := i + 1 | ||
err = conn.Exec(ctx, "INSERT INTO test_1200 (id, null_str) VALUES (?, ?)", id, tt.value) | ||
require.NoError(t, err) | ||
|
||
var got *string | ||
err = conn.QueryRow(ctx, "SELECT null_str FROM test_1200 WHERE id = ?", id).Scan(&got) | ||
require.NoError(t, err) | ||
|
||
if tt.want == nil { | ||
require.Nil(t, got) | ||
} else { | ||
require.NotNil(t, got) | ||
require.Equal(t, *tt.want, *got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type Test1200NullStr struct { | ||
underlying string | ||
} | ||
|
||
func (nc Test1200NullStr) String() string { | ||
return nc.underlying | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a similar check using
database/sql
as a reference.This implementation is also copied
go-sql-driver/mysql
.Should I write a source code comment mentioning the reference source?