Skip to content

Commit

Permalink
lots of small lint cleanups; JsonText -> JSONText is not fixable whil…
Browse files Browse the repository at this point in the history
…e maintaining backwards compatability
  • Loading branch information
jmoiron committed May 3, 2015
1 parent b507f47 commit a88aa93
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
5 changes: 2 additions & 3 deletions named.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,8 @@ func compileNamedQuery(qs []byte, bindType int) (query string, names []string, e
return string(rebound), names, err
}

// Bind binds a struct or a map to a query with named parameters.
// DEPRECATED: use of `sqlx.Named` is suggested instead of this
// function, and it may be removed in future.
// BindNamed binds a struct or a map to a query with named parameters.
// DEPRECATED: use sqlx.Named` instead of this, it may be removed in future.
func BindNamed(bindType int, query string, arg interface{}) (string, []interface{}, error) {
return bindNamedMapper(bindType, query, arg, mapper())
}
Expand Down
8 changes: 4 additions & 4 deletions reflectx/reflect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package reflect implements extensions to the standard reflect lib suitable
// Package reflectx implements extensions to the standard reflect lib suitable
// for implementing marshaling and unmarshaling packages. The main Mapper type
// allows for Go-compatible named atribute access, including accessing embedded
// struct attributes and the ability to use functions and struct tags to
Expand Down Expand Up @@ -121,7 +121,7 @@ func (m *Mapper) FieldsByName(v reflect.Value, names []string) []reflect.Value {
return vals
}

// Traversals by name returns a slice of int slices which represent the struct
// TraversalsByName returns a slice of int slices which represent the struct
// traversals for each mapped name. Panics if t is not a struct or Indirectable
// to a struct. Returns empty int slice for each name not found.
func (m *Mapper) TraversalsByName(t reflect.Type, names []string) [][]int {
Expand Down Expand Up @@ -177,13 +177,13 @@ func Deref(t reflect.Type) reflect.Type {

// -- helpers & utilities --

type Kinder interface {
type kinder interface {
Kind() reflect.Kind
}

// mustBe checks a value against a kind, panicing with a reflect.ValueError
// if the kind isn't that which is required.
func mustBe(v Kinder, expected reflect.Kind) {
func mustBe(v kinder, expected reflect.Kind) {
k := v.Kind()
if k != expected {
panic(&reflect.ValueError{Method: methodName(), Kind: k})
Expand Down
5 changes: 2 additions & 3 deletions reflectx/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ func TestEmbedded(t *testing.T) {

func TestTagNameMapping(t *testing.T) {
type Strategy struct {
StrategyId string `protobuf:"bytes,1,opt,name=strategy_id" json:"strategy_id,omitempty"`
StrategyID string `protobuf:"bytes,1,opt,name=strategy_id" json:"strategy_id,omitempty"`
StrategyName string
}

m := NewMapperTagFunc("json", strings.ToUpper, func(value string) string {
if strings.Contains(value, ",") {
return strings.Split(value, ",")[0]
} else {
return value
}
return value
})
strategy := Strategy{"1", "Alpah"}
mapping := m.TypeMap(reflect.TypeOf(strategy))
Expand Down
18 changes: 9 additions & 9 deletions sqlx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,13 @@ func TestNamedQuery(t *testing.T) {
// queries and NamedStmt queries, which use different code paths internally.
old := *db.Mapper

type JsonPerson struct {
type JSONPerson struct {
FirstName sql.NullString `json:"FIRST"`
LastName sql.NullString `json:"last_name"`
Email sql.NullString
}

jp := JsonPerson{
jp := JSONPerson{
FirstName: sql.NullString{"ben", true},
LastName: sql.NullString{"smith", true},
Email: sql.NullString{"[email protected]", true},
Expand All @@ -551,7 +551,7 @@ func TestNamedQuery(t *testing.T) {

// Checks that a person pulled out of the db matches the one we put in
check := func(t *testing.T, rows *Rows) {
jp = JsonPerson{}
jp = JSONPerson{}
for rows.Next() {
err = rows.StructScan(&jp)
if err != nil {
Expand Down Expand Up @@ -619,22 +619,22 @@ func TestNilInserts(t *testing.T) {

RunWithSchema(schema, t, func(db *DB, t *testing.T) {
type TT struct {
Id int
ID int
Value *string
}
var v, v2 TT
r := db.Rebind

db.MustExec(r(`INSERT INTO tt (id) VALUES (1)`))
db.Get(&v, r(`SELECT * FROM tt`))
if v.Id != 1 {
t.Errorf("Expecting id of 1, got %v", v.Id)
if v.ID != 1 {
t.Errorf("Expecting id of 1, got %v", v.ID)
}
if v.Value != nil {
t.Errorf("Expecting NULL to map to nil, got %s", v.Value)
}

v.Id = 2
v.ID = 2
// NOTE: this incidentally uncovered a bug which was that named queries with
// pointer destinations would not work if the passed value here was not addressable,
// as reflectx.FieldByIndexes attempts to allocate nil pointer receivers for
Expand All @@ -643,8 +643,8 @@ func TestNilInserts(t *testing.T) {
db.NamedExec(`INSERT INTO tt (id, value) VALUES (:id, :value)`, v)

db.Get(&v2, r(`SELECT * FROM tt WHERE id=2`))
if v.Id != v2.Id {
t.Errorf("%v != %v", v.Id, v2.Id)
if v.ID != v2.ID {
t.Errorf("%v != %v", v.ID, v2.ID)
}
if v2.Value != nil {
t.Errorf("Expecting NULL to map to nil, got %s", v.Value)
Expand Down
8 changes: 7 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import (
"io/ioutil"
)

// GzippedText is a []byte which transparently gzips data being submitted to
// a database and ungzips data being Scanned from a database.
type GzippedText []byte

// Value implements the driver.Valuer interface, gzipping the raw value of
// this GzippedText.
func (g GzippedText) Value() (driver.Value, error) {
b := make([]byte, 0, len(g))
buf := bytes.NewBuffer(b)
Expand All @@ -22,6 +26,8 @@ func (g GzippedText) Value() (driver.Value, error) {

}

// Scan implements the sql.Scanner interface, ungzipping the value coming off
// the wire and storing the raw result in the GzippedText.
func (g *GzippedText) Scan(src interface{}) error {
var source []byte
switch src.(type) {
Expand All @@ -48,7 +54,7 @@ func (g *GzippedText) Scan(src interface{}) error {
// implements `Unmarshal`, which unmarshals the json within to an interface{}
type JsonText json.RawMessage

// Returns the *j as the JSON encoding of j.
// MarshalJSON returns the *j as the JSON encoding of j.
func (j *JsonText) MarshalJSON() ([]byte, error) {
return *j, nil
}
Expand Down

0 comments on commit a88aa93

Please sign in to comment.