denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 497 forks source link

TVP encode doesn't ignore non-public fields #621

Open Woody1193 opened 3 years ago

Woody1193 commented 3 years ago

Describe the bug A clear and concise description of what the bug is. Any struct with non-public fields that cannot be encoded will cause an error when the value is converted. This makes the TVP incompatible with protobuf.

If you are seeing an exception, include the full exceptions details (message and stack trace).

Exception message: failed to convert tvp parameter row 0 col {{} [] [] 0}: unsupported type impl.MessageState, a struct

To Reproduce Include a complete code listing that we can run to reproduce the issue.

type TestItem struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Key int
    Value string
}

func main() {

items := make([]TestValue, {
    {Key: 0, Value: "a"},
    {Key: 1, Value: "b")
}

tvp := mssql.TVP{
TypeName: "ItemsTableType",
Value:    items,
}

// Connect to a database and create a transaction
err := tx.ExecContext(context.Background(), "EXEC dbo.usp_InsertItems @TVP", sql.Names("TVP", tvp))
if err != nil {
    panic(err)
}

}

Expected behavior I expect no error to occur.

Further technical details

Operating system: Windows 10 Table schema: Not necessary

Additional context This appears to be occurring because the encode function is missing a check for whether or not the field is exported. This can be done by checking whether or not field.PkgPath is empty.