Created
September 20, 2020 16:35
-
-
Save seamusv/b70f8bc9df4930d4405c41aeecc20791 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package pgsql | |
import ( | |
"database/sql" | |
"errors" | |
"github.com/gogo/protobuf/jsonpb" | |
"github.com/gogo/protobuf/proto" | |
"strings" | |
) | |
type SerdeJsonFunc func (input proto.Message, output proto.Message, callback func(jsonString string) *sql.Row) error | |
func SerdeJson(marshaller *jsonpb.Marshaler, unmarshaller *jsonpb.Unmarshaler) SerdeJsonFunc { | |
return func (input proto.Message, output proto.Message, callback func(jsonString string) *sql.Row) error { | |
jsonString, err := marshaller.MarshalToString(input) | |
if err != nil { | |
return err | |
} | |
var data sql.NullString | |
if err := callback(jsonString).Scan(&data); err != nil { | |
return err | |
} | |
if !data.Valid { | |
return errors.New("no result returned") | |
} | |
if err := unmarshaller.Unmarshal(strings.NewReader(data.String), output); err != nil { | |
return err | |
} | |
return nil | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment