pbind

package module
v0.0.0-...-52e7d0f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2021 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Build Status codecov PkgGoDev Go Report Card

PBIND

Package pbind provides means of binding protocol buffer message types to sql.Rows output.

A common design challange with Go is scanning sql.Rows results into structs. Ussualy, one would scan into the indivudual fields or local variables. But what if the the selected columns are a variable in the application? In such cases developers have to resort to reflection, an ORM based on reflection or a code generator like SQLBoiler with tons of adaptor code.

Pbind is for protocol buffer developers which like to avoid the usage of ORMs or reflection. It uses protoreflect, which uses the embedded protobuf descriptors in the generated Go code. This should give a potential performance edge over Go reflection.

Example

func query() []*pb.Msg {
    // Scanning will match the probuf field names (case sensitive!).
    // If the column name does not match the field name, use an alias.
    const query = "select something as toggle, number, snumber, unumber, large, slarge, ularge, sfloat, lfloat, text, bin from table;"

    rows, err := db.Query(query)
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    var msgs []*pb.Msg

    for rows.Next() {
        msg := new(pb.Msg)

        if err := Scan(rows, msg); err != nil {
            panic(err)
        }

        msgs = append(msgs, msg)
    }
}

Copyright (c) 2021, Mohlmann Solutions SRL. All rights reserved. Use of this source code is governed by a License that can be found in the LICENSE file.

Documentation

Overview

Package pbind provides means of binding protocol buffer message types to sql.Rows output.

A common design challange with Go is scanning sql.Rows results into structs. Ussualy, one would scan into the indivudual fields or local variables. But what if the the selected columns are a variable in the application? In such cases developers have to resort to reflection, an ORM based on reflection or a code generator like SQLBoiler with tons of adaptor code.

Pbind is for protocol buffer developers which like to avoid the usage of ORMs or reflection. It uses protoreflect, which uses the embedded protobuf descriptors in the generated Go code. This should give a potential performance edge over Go reflection.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Scan

func Scan(rows *sql.Rows, msg proto.Message) error

Scan the values from a Rows iteration into a protobuf message. The caller must call rows.Next() before each call of Scan.

Column (or alias) names are matched with the proto Message Field Names. If a column name cannot be matched to a Field Name, Scan returns an error.

All (and only) scalar Field types are supported as defined at: https://developers.google.com/protocol-buffers/docs/proto3#scalar.

Example

Example to scan into the protobuf message as defined in test/pb/bind_test.proto.

// Scanning will match the probuf field names (case sensitive!).
// If the column name does not match the field name, use an alias.
const query = "select something as toggle, number, snumber, unumber, large, slarge, ularge, sfloat, lfloat, text, bin from table;"

rows, err := db.Query(query)
if err != nil {
	panic(err)
}
defer rows.Close()

var msgs []*pb.Msg

for rows.Next() {
	msg := new(pb.Msg)

	if err := Scan(rows, msg); err != nil {
		panic(err)
	}

	msgs = append(msgs, msg)
}
Output:

Types

This section is empty.

Directories

Path Synopsis
test
pb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL