package
1.9.0
Repository: https://github.com/didi/gendry.git
Documentation: pkg.go.dev

# README

Scaner

Scan

import (
    "database/sql"
    "fmt"
    "github.com/didi/gendry/scanner"
)

type Person struct {
    Name string `ddb:"name"`
    Age int `ddb:"m_age"`
}


rows,_ := db.Query("select name,m_age from person")
var students []Person
err := scanner.Scan(rows, &students)
for _,student := range students {
	fmt.Println(student)
}

Make sure the second param of Scan should be a reference

ScanClose

ScanClose is the same as the Scan but it also close the rows so you dont't need to worry about closing the rows yourself.

ScanMap

ScanMap returns the result in the form of []map[string]interface{}, sometimes this could be more convenient.

rows,_ := db.Query("select name,m_age from person")
result,err := scanner.ScanMap(rows)
for _,record := range result {
	fmt.Println(record["name"], record["m_age"])
}

If you don't want to define a struct,ScanMap may be useful.But the returned the map is map[string]interface{}, and interface{} is pretty unclear like the void * in C or Object in JAVA, it'll suck you sooner or later.

ScanMapClose

ScanMapClose is the same as ScanMap but it also close the rows

Map

Map convert a struct into a map which could easily be used to insert

Test cases blow may make sense

package scaner

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestMap(t *testing.T) {
	type Person struct {
		Name string `ddb:"name"`
		Age  int    `ddb:"age"`
		foo  byte   `ddb:"foo"`
	}
	a := Person{"deen", 22, 1}
	b := &Person{"caibirdme", 23, 1}
	c := &b
	mapA, err := Map(a, DefaultTagName)
	ass := assert.New(t)
	ass.NoError(err)
	ass.Equal("deen", mapA["name"])
	ass.Equal(22, mapA["age"])
	_, ok := mapA["foo"]
	ass.False(ok)
	mapB, err := Map(c, "")
	ass.NoError(err)
	ass.Equal("caibirdme", mapB["Name"])
	ass.Equal(23, mapB["Age"])
}
  • Unexported fields will be ignored
  • Ptr type will be ignored
  • Resolve pointer automatically
  • The second param specify what tagName you used in defining your struct.If passed an empty string, FieldName will be returned as the key of the map

# Functions

Map converts a struct to a map type for each field of the struct must be built-in type.
Scan scans data from rows to target Don't forget to close the rows When the target is not a pointer of slice, ErrEmptyResult may be returned if the query result is empty.
ScanClose is the same as Scan and helps you Close the rows Not necessary exec the rows.Close after calling this.
ScanMap returns the result in the form of []map[string]interface{} json.Marshal encodes []byte as a base64 string, while in most cases it's expected to be encoded as string or int.
ScanMapClose is the same as ScanMap and close the rows.
ScanMapDecode returns the result in the form of []map[string]interface{} If possible, it will convert []uint8 to int or float64, or it will convert []uint8 to string.
ScanMapDecodeClose returns the result in the form of []map[string]interface{} If possible, it will convert []uint8 to int or float64, or it will convert []uint8 to string.
SetTagName can be set only once.

# Constants

DefaultTagName is the default struct tag name.

# Variables

ErrEmptyResult occurs when target of Scan isn't slice and the result of the query is empty.
ErrNilRows means the first param can't be a nil.
ErrNoneStructTarget as its name says.
ErrSliceToString means only []uint8 can be transmuted into string.
ErrTargetNotSettable means the second param of Bind is not settable.

# Structs

CloseErr is the error occurs when rows.Close().
ScanErr will be returned if an underlying type couldn't be AssignableTo type of target field.

# Interfaces

ByteUnmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.
Rows defines methods that scanner needs, which database/sql.Rows already implements.