protoc-gen-map
protoc-gen-map copied to clipboard
varchar not converted to string
Following the code under "examples" in this repository, I've brought up a MySQL DB and a simple server based on "server.go". I've created the "blog" table as per the "testdata/initdb/sql/init_db.sql" example and added some values. Finally, I've created a test client. Connections and mapping work for integer values and I get a correct BlogResponse. However, the varchar "title" field is blank.
After some debugging, I found that it was not correctly converted. The varchar was read as a []uint8 from the DB. I modified the function "MapResponse" in "mapper.go" to include a conversion to a string. Something like this:
func B2S(bs []uint8) string {
b := make([]byte, len(bs))
for i, v := range bs {
b[i] = byte(v)
}
return string(b)
}
func (m *Mapper) MapResponse(respMap *ResponseMapping) error {
....
arrayUint8, ok := sqlMapVals.ProtoValues[i].([]uint8)
if ok == true {
str := B2S(arrayUint8)
setProto(respField, str)
} else {
setProto(respField, sqlMapVals.ProtoValues[i])
}
With that fix, the full BlogResponse object is returned correctly.
Is this a known problem? I have configured the DB (MySql / MariaDB) wrong? Or maybe it's a driver or configuration problem?
Which version are you using ?
I predominately work with Postgres, but From what I remember, MySQL needs a prepare statement to return correct data type. When using query() directly, the MySQL will return your data in form of bytes. This is not the case with Postgres. The fix would involve adding a prepare statement in the generator. Until I do that and thoroughly test it, feel free to use https://github.com/jackskj/carta if you are using MySQL, just make sure to use prepare statement.