1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 20:10:55 +00:00

orm: fix golint

This commit is contained in:
miraclesu 2016-04-09 10:22:40 +08:00
parent e95bef1331
commit 99f1e6c8b5
6 changed files with 59 additions and 53 deletions

View File

@ -90,7 +90,7 @@ checkColumn:
} else { } else {
col = fmt.Sprintf(s, fi.digits, fi.decimals) col = fmt.Sprintf(s, fi.digits, fi.decimals)
} }
case TypeJsonField: case TypeJSONField:
if al.Driver != DRPostgres { if al.Driver != DRPostgres {
fieldType = TypeCharField fieldType = TypeCharField
goto checkColumn goto checkColumn
@ -290,7 +290,7 @@ func getColumnDefault(fi *fieldInfo) string {
case TypeBooleanField: case TypeBooleanField:
t = " DEFAULT %s " t = " DEFAULT %s "
d = "FALSE" d = "FALSE"
case TypeJsonField, TypeJsonbField: case TypeJSONField, TypeJsonbField:
d = "{}" d = "{}"
} }

View File

@ -141,7 +141,7 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val
} else { } else {
value = field.Bool() value = field.Bool()
} }
case TypeCharField, TypeTextField, TypeJsonField, TypeJsonbField: case TypeCharField, TypeTextField, TypeJSONField, TypeJsonbField:
if ns, ok := field.Interface().(sql.NullString); ok { if ns, ok := field.Interface().(sql.NullString); ok {
value = nil value = nil
if ns.Valid { if ns.Valid {
@ -247,7 +247,7 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val
field.Set(reflect.ValueOf(tnow.In(DefaultTimeLoc))) field.Set(reflect.ValueOf(tnow.In(DefaultTimeLoc)))
} }
} }
case TypeJsonField, TypeJsonbField: case TypeJSONField, TypeJsonbField:
if s, ok := value.(string); (ok && len(s) == 0) || value == nil { if s, ok := value.(string); (ok && len(s) == 0) || value == nil {
if fi.colDefault && fi.initial.Exist() { if fi.colDefault && fi.initial.Exist() {
value = fi.initial.String() value = fi.initial.String()
@ -1101,7 +1101,7 @@ setValue:
} }
value = b value = b
} }
case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJsonField || fieldType == TypeJsonbField: case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField:
if str == nil { if str == nil {
value = ToStr(val) value = ToStr(val)
} else { } else {
@ -1247,7 +1247,7 @@ setValue:
field.SetBool(value.(bool)) field.SetBool(value.(bool))
} }
} }
case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJsonField || fieldType == TypeJsonbField: case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField:
if isNative { if isNative {
if ns, ok := field.Interface().(sql.NullString); ok { if ns, ok := field.Interface().(sql.NullString); ok {
if value == nil { if value == nil {

View File

@ -38,7 +38,7 @@ const (
TypePositiveBigIntegerField TypePositiveBigIntegerField
TypeFloatField TypeFloatField
TypeDecimalField TypeDecimalField
TypeJsonField TypeJSONField
TypeJsonbField TypeJsonbField
RelForeignKey RelForeignKey
RelOneToOne RelOneToOne
@ -148,7 +148,7 @@ func (e *CharField) RawValue() interface{} {
// verify CharField implement Fielder // verify CharField implement Fielder
var _ Fielder = new(CharField) var _ Fielder = new(CharField)
// A time, represented in go by a time.Time instance. // TimeField A time, represented in go by a time.Time instance.
// only time values like 10:00:00 // only time values like 10:00:00
// Has a few extra, optional attr tag: // Has a few extra, optional attr tag:
// //
@ -163,22 +163,27 @@ var _ Fielder = new(CharField)
// eg: `orm:"auto_now"` or `orm:"auto_now_add"` // eg: `orm:"auto_now"` or `orm:"auto_now_add"`
type TimeField time.Time type TimeField time.Time
// Value return the time.Time
func (e TimeField) Value() time.Time { func (e TimeField) Value() time.Time {
return time.Time(e) return time.Time(e)
} }
// Set set the TimeField's value
func (e *TimeField) Set(d time.Time) { func (e *TimeField) Set(d time.Time) {
*e = TimeField(d) *e = TimeField(d)
} }
// String convert time to string
func (e *TimeField) String() string { func (e *TimeField) String() string {
return e.Value().String() return e.Value().String()
} }
// FieldType return enum type Date
func (e *TimeField) FieldType() int { func (e *TimeField) FieldType() int {
return TypeDateField return TypeDateField
} }
// SetRaw convert the interface to time.Time. Allow string and time.Time
func (e *TimeField) SetRaw(value interface{}) error { func (e *TimeField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case time.Time: case time.Time:
@ -195,6 +200,7 @@ func (e *TimeField) SetRaw(value interface{}) error {
return nil return nil
} }
// RawValue return time value
func (e *TimeField) RawValue() interface{} { func (e *TimeField) RawValue() interface{} {
return e.Value() return e.Value()
} }
@ -684,47 +690,47 @@ func (e *TextField) RawValue() interface{} {
// verify TextField implement Fielder // verify TextField implement Fielder
var _ Fielder = new(TextField) var _ Fielder = new(TextField)
// JsonField postgres json field. // JSONField postgres json field.
type JsonField string type JSONField string
// Value return JsonField value // Value return JSONField value
func (j JsonField) Value() string { func (j JSONField) Value() string {
return string(j) return string(j)
} }
// Set the JsonField value // Set the JSONField value
func (j *JsonField) Set(d string) { func (j *JSONField) Set(d string) {
*j = JsonField(d) *j = JSONField(d)
} }
// String convert JsonField to string // String convert JSONField to string
func (j *JsonField) String() string { func (j *JSONField) String() string {
return j.Value() return j.Value()
} }
// FieldType return enum type // FieldType return enum type
func (j *JsonField) FieldType() int { func (j *JSONField) FieldType() int {
return TypeJsonField return TypeJSONField
} }
// SetRaw convert interface string to string // SetRaw convert interface string to string
func (j *JsonField) SetRaw(value interface{}) error { func (j *JSONField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case string: case string:
j.Set(d) j.Set(d)
default: default:
return fmt.Errorf("<JsonField.SetRaw> unknown value `%s`", value) return fmt.Errorf("<JSONField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return JsonField value // RawValue return JSONField value
func (j *JsonField) RawValue() interface{} { func (j *JSONField) RawValue() interface{} {
return j.Value() return j.Value()
} }
// verify JsonField implement Fielder // verify JSONField implement Fielder
var _ Fielder = new(JsonField) var _ Fielder = new(JSONField)
// JsonbField postgres json field. // JsonbField postgres json field.
type JsonbField string type JsonbField string

View File

@ -244,7 +244,7 @@ checkType:
case "text": case "text":
fieldType = TypeTextField fieldType = TypeTextField
case "json": case "json":
fieldType = TypeJsonField fieldType = TypeJSONField
case "jsonb": case "jsonb":
fieldType = TypeJsonbField fieldType = TypeJsonbField
} }
@ -349,7 +349,7 @@ checkType:
switch fieldType { switch fieldType {
case TypeBooleanField: case TypeBooleanField:
case TypeCharField, TypeJsonField, TypeJsonbField: case TypeCharField, TypeJSONField, TypeJsonbField:
if size != "" { if size != "" {
v, e := StrTo(size).Int32() v, e := StrTo(size).Int32()
if e != nil { if e != nil {

View File

@ -78,41 +78,41 @@ func (e *SliceStringField) RawValue() interface{} {
var _ Fielder = new(SliceStringField) var _ Fielder = new(SliceStringField)
// A json field. // A json field.
type JSONField struct { type JSONFieldTest struct {
Name string Name string
Data string Data string
} }
func (e *JSONField) String() string { func (e *JSONFieldTest) String() string {
data, _ := json.Marshal(e) data, _ := json.Marshal(e)
return string(data) return string(data)
} }
func (e *JSONField) FieldType() int { func (e *JSONFieldTest) FieldType() int {
return TypeTextField return TypeTextField
} }
func (e *JSONField) SetRaw(value interface{}) error { func (e *JSONFieldTest) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case string: case string:
return json.Unmarshal([]byte(d), e) return json.Unmarshal([]byte(d), e)
default: default:
return fmt.Errorf("<JsonField.SetRaw> unknown value `%v`", value) return fmt.Errorf("<JSONField.SetRaw> unknown value `%v`", value)
} }
} }
func (e *JSONField) RawValue() interface{} { func (e *JSONFieldTest) RawValue() interface{} {
return e.String() return e.String()
} }
var _ Fielder = new(JSONField) var _ Fielder = new(JSONFieldTest)
type Data struct { type Data struct {
ID int `orm:"column(id)"` ID int `orm:"column(id)"`
Boolean bool Boolean bool
Char string `orm:"size(50)"` Char string `orm:"size(50)"`
Text string `orm:"type(text)"` Text string `orm:"type(text)"`
Json string `orm:"type(json);default({\"name\":\"json\"})"` JSON string `orm:"type(json);default({\"name\":\"json\"})"`
Jsonb string `orm:"type(jsonb)"` Jsonb string `orm:"type(jsonb)"`
Time time.Time `orm:"type(time)"` Time time.Time `orm:"type(time)"`
Date time.Time `orm:"type(date)"` Date time.Time `orm:"type(date)"`
@ -139,7 +139,7 @@ type DataNull struct {
Boolean bool `orm:"null"` Boolean bool `orm:"null"`
Char string `orm:"null;size(50)"` Char string `orm:"null;size(50)"`
Text string `orm:"null;type(text)"` Text string `orm:"null;type(text)"`
Json string `orm:"type(json);null"` JSON string `orm:"type(json);null"`
Jsonb string `orm:"type(jsonb);null"` Jsonb string `orm:"type(jsonb);null"`
Time time.Time `orm:"null;type(time)"` Time time.Time `orm:"null;type(time)"`
Date time.Time `orm:"null;type(date)"` Date time.Time `orm:"null;type(date)"`
@ -243,7 +243,7 @@ type User struct {
ShouldSkip string `orm:"-"` ShouldSkip string `orm:"-"`
Nums int Nums int
Langs SliceStringField `orm:"size(100)"` Langs SliceStringField `orm:"size(100)"`
Extra JSONField `orm:"type(text)"` Extra JSONFieldTest `orm:"type(text)"`
unexport bool `orm:"-"` unexport bool `orm:"-"`
unexportBool bool unexportBool bool
} }
@ -394,12 +394,12 @@ func NewInLineOneToOne() *InLineOneToOne {
} }
type IntegerPk struct { type IntegerPk struct {
Id int64 `orm:"pk"` ID int64 `orm:"pk"`
Value string Value string
} }
type UintPk struct { type UintPk struct {
Id uint32 `orm:"pk"` ID uint32 `orm:"pk"`
Name string Name string
} }

View File

@ -241,7 +241,7 @@ var DataValues = map[string]interface{}{
"Boolean": true, "Boolean": true,
"Char": "char", "Char": "char",
"Text": "text", "Text": "text",
"Json": `{"name":"json"}`, "JSON": `{"name":"json"}`,
"Jsonb": `{"name": "jsonb"}`, "Jsonb": `{"name": "jsonb"}`,
"Time": time.Now(), "Time": time.Now(),
"Date": time.Now(), "Date": time.Now(),
@ -268,7 +268,7 @@ func TestDataTypes(t *testing.T) {
ind := reflect.Indirect(reflect.ValueOf(&d)) ind := reflect.Indirect(reflect.ValueOf(&d))
for name, value := range DataValues { for name, value := range DataValues {
if name == "Json" { if name == "JSON" {
continue continue
} }
e := ind.FieldByName(name) e := ind.FieldByName(name)
@ -316,7 +316,7 @@ func TestNullDataTypes(t *testing.T) {
throwFail(t, AssertIs(id, 1)) throwFail(t, AssertIs(id, 1))
data := `{"ok":1,"data":{"arr":[1,2],"msg":"gopher"}}` data := `{"ok":1,"data":{"arr":[1,2],"msg":"gopher"}}`
d = DataNull{ID: 1, Json: data} d = DataNull{ID: 1, JSON: data}
num, err := dORM.Update(&d) num, err := dORM.Update(&d)
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(num, 1)) throwFail(t, AssertIs(num, 1))
@ -325,7 +325,7 @@ func TestNullDataTypes(t *testing.T) {
err = dORM.Read(&d) err = dORM.Read(&d)
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(d.Json, data)) throwFail(t, AssertIs(d.JSON, data))
throwFail(t, AssertIs(d.NullBool.Valid, false)) throwFail(t, AssertIs(d.NullBool.Valid, false))
throwFail(t, AssertIs(d.NullString.Valid, false)) throwFail(t, AssertIs(d.NullString.Valid, false))
@ -2027,9 +2027,9 @@ func TestInLineOneToOne(t *testing.T) {
func TestIntegerPk(t *testing.T) { func TestIntegerPk(t *testing.T) {
its := []IntegerPk{ its := []IntegerPk{
{Id: math.MinInt64, Value: "-"}, {ID: math.MinInt64, Value: "-"},
{Id: 0, Value: "0"}, {ID: 0, Value: "0"},
{Id: math.MaxInt64, Value: "+"}, {ID: math.MaxInt64, Value: "+"},
} }
num, err := dORM.InsertMulti(len(its), its) num, err := dORM.InsertMulti(len(its), its)
@ -2037,7 +2037,7 @@ func TestIntegerPk(t *testing.T) {
throwFail(t, AssertIs(num, len(its))) throwFail(t, AssertIs(num, len(its)))
for _, intPk := range its { for _, intPk := range its {
out := IntegerPk{Id: intPk.Id} out := IntegerPk{ID: intPk.ID}
err = dORM.Read(&out) err = dORM.Read(&out)
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(out.Value, intPk.Value)) throwFail(t, AssertIs(out.Value, intPk.Value))
@ -2085,21 +2085,21 @@ func TestInsertAuto(t *testing.T) {
func TestUintPk(t *testing.T) { func TestUintPk(t *testing.T) {
name := "go" name := "go"
u := &UintPk{ u := &UintPk{
Id: 8, ID: 8,
Name: name, Name: name,
} }
created, pk, err := dORM.ReadOrCreate(u, "Id") created, pk, err := dORM.ReadOrCreate(u, "ID")
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(created, true)) throwFail(t, AssertIs(created, true))
throwFail(t, AssertIs(u.Name, name)) throwFail(t, AssertIs(u.Name, name))
nu := &UintPk{Id: 8} nu := &UintPk{ID: 8}
created, pk, err = dORM.ReadOrCreate(nu, "Id") created, pk, err = dORM.ReadOrCreate(nu, "ID")
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(created, false)) throwFail(t, AssertIs(created, false))
throwFail(t, AssertIs(nu.Id, u.Id)) throwFail(t, AssertIs(nu.ID, u.ID))
throwFail(t, AssertIs(pk, u.Id)) throwFail(t, AssertIs(pk, u.ID))
throwFail(t, AssertIs(nu.Name, name)) throwFail(t, AssertIs(nu.Name, name))
dORM.Delete(u) dORM.Delete(u)