Merge pull request #2 from yunionio/bugfix/lzx-tristate

vendor update: fix tristate.
This commit is contained in:
屈轩
2018-07-30 20:51:32 +08:00
committed by GitHub
8 changed files with 91 additions and 95 deletions
Generated
+3 -4
View File
@@ -242,7 +242,7 @@
[[projects]]
name = "github.com/yunionio/jsonutils"
packages = ["."]
revision = "1edf0ca607c9e49be4705bd3628b2165d7ffee3c"
revision = "6dd39f8579af6c6b61b971eb3a1ccf55e724b0ca"
[[projects]]
branch = "master"
@@ -309,10 +309,9 @@
revision = "e3d49929d3d3ae3aeea77b188a03e5107f5a8b7e"
[[projects]]
branch = "master"
name = "github.com/yunionio/sqlchemy"
packages = ["."]
revision = "9e57e4fa1e915d46e299155f065a954cd7a07e7f"
revision = "a14641a90b9e5c72868b7a8358d62ea1d83eab53"
[[projects]]
branch = "master"
@@ -371,6 +370,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "de3d4429983b4807a82bb7dc8f29a34fff78db2ef986739c4cbd73a6c0a2bf06"
inputs-digest = "1bc86ec334153df33c312e01120e9f5f954a209f76b6d9afd3fc7cb6af2b9de2"
solver-name = "gps-cdcl"
solver-version = 1
+5 -2
View File
@@ -30,10 +30,13 @@
version = "0.2.2"
[[constraint]]
#branch = "master"
revision = "1edf0ca607c9e49be4705bd3628b2165d7ffee3c"
revision = "6dd39f8579af6c6b61b971eb3a1ccf55e724b0ca"
name = "github.com/yunionio/jsonutils"
[[constraint]]
revision = "a14641a90b9e5c72868b7a8358d62ea1d83eab53"
name = "github.com/yunionio/sqlchemy"
[[constraint]]
branch = "master"
name = "github.com/yunionio/log"
+2
View File
@@ -1,5 +1,7 @@
package jsonutils
// import "github.com/yunionio/pkg/gotypes"
func (dict *JSONDict) Equals(json JSONObject) bool {
dict2, ok := json.(*JSONDict)
if !ok {
+26 -1
View File
@@ -1,12 +1,20 @@
package jsonutils
/**
jsonutils.Marshal
Convert any object to JSONObject
*/
import (
"fmt"
"reflect"
"time"
"github.com/yunionio/log"
"github.com/yunionio/pkg/gotypes"
"github.com/yunionio/log"
"github.com/yunionio/pkg/tristate"
"github.com/yunionio/pkg/util/reflectutils"
"github.com/yunionio/pkg/util/timeutils"
)
@@ -76,6 +84,16 @@ func marshalBoolean(val bool) *JSONBool {
}
}
func marshalTristate(val tristate.TriState) JSONObject {
if val.IsTrue() {
return JSONTrue
} else if val.IsFalse() {
return JSONFalse
} else {
return JSONNull
}
}
func marshalString(val string) JSONObject {
if len(val) == 0 {
return JSONNull
@@ -149,6 +167,13 @@ func marshalValue(objValue reflect.Value) JSONObject {
} else {
return JSONNull
}
case tristate.TriStateType:
tri, ok := objValue.Interface().(tristate.TriState)
if ok {
return marshalTristate(tri)
} else {
return JSONNull
}
}
switch objValue.Kind() {
case reflect.Slice, reflect.Array:
+40 -87
View File
@@ -9,6 +9,7 @@ import (
"github.com/yunionio/log"
"github.com/yunionio/pkg/gotypes"
"github.com/yunionio/pkg/tristate"
"github.com/yunionio/pkg/util/regutils"
"github.com/yunionio/pkg/utils"
)
@@ -256,14 +257,6 @@ func (c *SBooleanColumn) ConvertFromString(str string) string {
}
}
func (c *SBooleanColumn) ConvertToString(str string) string {
if str == "0" {
return "false"
} else {
return "true"
}
}
func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
bVal := val.(bool)
if bVal {
@@ -273,31 +266,55 @@ func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
}
}
func (c *SBooleanColumn) ConvertToValue(val interface{}) interface{} {
iVal := val.(int)
if iVal == 0 {
return false
} else {
return true
}
}
func (c *SBooleanColumn) IsZero(val interface{}) bool {
bVal := val.(bool)
return bVal == false
}
func (c *SBooleanColumn) IsEqual(v1, v2 interface{}) bool {
bVal1 := v1.(bool)
bVal2 := v2.(bool)
return bVal1 == bVal2
}
func NewBooleanColumn(name string, tagmap map[string]string) SBooleanColumn {
bc := SBooleanColumn{SBaseWidthColumn: NewBaseWidthColumn(name, "TINYINT", tagmap)}
return bc
}
type STristateColumn struct {
SBaseWidthColumn
}
func (c *STristateColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *STristateColumn) ConvertFromString(str string) string {
switch strings.ToLower(str) {
case "true", "yes", "on", "ok", "1":
return "1"
case "none", "null", "unknown":
return ""
default:
return "0"
}
}
func (c *STristateColumn) ConvertFromValue(val interface{}) interface{} {
bVal := val.(tristate.TriState)
if bVal == tristate.True {
return 1
} else {
return 0
}
}
func (c *STristateColumn) IsZero(val interface{}) bool {
bVal := val.(tristate.TriState)
return bVal == tristate.None
}
func NewTristateColumn(name string, tagmap map[string]string) STristateColumn {
bc := STristateColumn{SBaseWidthColumn: NewBaseWidthColumn(name, "TINYINT", tagmap)}
return bc
}
type SIntegerColumn struct {
SBaseWidthColumn
IsAutoIncrement bool
@@ -347,32 +364,6 @@ func (c *SIntegerColumn) IsZero(val interface{}) bool {
return true
}
func (c *SIntegerColumn) IsEqual(v1, v2 interface{}) bool {
switch v1.(type) {
case int:
return v1.(int) == v2.(int)
case int8:
return v1.(int8) == v2.(int8)
case int16:
return v1.(int16) == v2.(int16)
case int32:
return v1.(int32) == v2.(int32)
case int64:
return v1.(int64) == v2.(int64)
case uint:
return v1.(uint) == v2.(uint)
case uint8:
return v1.(uint8) == v2.(uint8)
case uint16:
return v1.(uint16) == v2.(uint16)
case uint32:
return v1.(uint32) == v2.(uint32)
case uint64:
return v1.(uint64) == v2.(uint64)
}
return false
}
func (c *SIntegerColumn) ColType() string {
str := (&c.SBaseWidthColumn).ColType()
if c.IsUnsigned {
@@ -434,16 +425,6 @@ func (c *SFloatColumn) IsZero(val interface{}) bool {
return true
}
func (c *SFloatColumn) IsEqual(v1, v2 interface{}) bool {
switch v1.(type) {
case float32:
return v1.(float32) == v2.(float32)
case float64:
return v1.(float64) == v2.(float64)
}
return false
}
func NewFloatColumn(name string, sqlType string, tagmap map[string]string) SFloatColumn {
return SFloatColumn{SBaseColumn: NewBaseColumn(name, sqlType, tagmap)}
}
@@ -476,16 +457,6 @@ func (c *SDecimalColumn) IsZero(val interface{}) bool {
return true
}
func (c *SDecimalColumn) IsEqual(v1, v2 interface{}) bool {
switch v1.(type) {
case float32:
return v1.(float32) == v2.(float32)
case float64:
return v1.(float64) == v2.(float64)
}
return false
}
func NewDecimalColumn(name string, tagmap map[string]string) SDecimalColumn {
tagmap, v, ok := utils.TagPop(tagmap, TAG_PRECISION)
if !ok {
@@ -534,12 +505,6 @@ func (c *STextColumn) IsZero(val interface{}) bool {
return len(bVal) == 0
}
func (c *STextColumn) IsEqual(v1, v2 interface{}) bool {
bVal1 := v1.(string)
bVal2 := v2.(string)
return bVal1 == bVal2
}
func NewTextColumn(name string, tagmap map[string]string) STextColumn {
var width int
var sqltype string
@@ -607,12 +572,6 @@ func (c *SDateTimeColumn) IsZero(val interface{}) bool {
return bVal.IsZero()
}
func (c *SDateTimeColumn) IsEqual(v1, v2 interface{}) bool {
bVal1 := v1.(time.Time)
bVal2 := v2.(time.Time)
return bVal1.Equal(bVal2)
}
func NewDateTimeColumn(name string, tagmap map[string]string) SDateTimeColumn {
createdAt := false
updatedAt := false
@@ -638,12 +597,6 @@ func (c *CompondColumn) DefinitionString() string {
return buf.String()
}
/* func (c *CompondColumn) IsEqual(v1, v2 interface{}) bool {
bVal1 := v1.(gotypes.ISerializable)
bVal2 := v2.(gotypes.ISerializable)
return bVal1.Equals(bVal2)
} */
func (c *CompondColumn) IsZero(val interface{}) bool {
if val == nil {
return true
+1 -1
View File
@@ -1,5 +1,5 @@
package sqlchemy
const (
var (
DEBUG_SQLCHEMY = false
)
+5
View File
@@ -5,6 +5,7 @@ import (
"github.com/yunionio/log"
"github.com/yunionio/pkg/gotypes"
"github.com/yunionio/pkg/tristate"
"github.com/yunionio/pkg/util/reflectutils"
"github.com/yunionio/pkg/utils"
)
@@ -55,6 +56,10 @@ func fieldToColumnSpec(field *reflect.StructField) IColumnSpec {
tagmap[TAG_WIDTH] = "1"
col := NewBooleanColumn(fieldname, tagmap)
return &col
case tristate.TriStateType:
tagmap[TAG_WIDTH] = "1"
col := NewTristateColumn(fieldname, tagmap)
return &col
case gotypes.Float32Type, gotypes.Float64Type:
if _, ok := tagmap[TAG_WIDTH]; ok {
col := NewDecimalColumn(fieldname, tagmap)
+9
View File
@@ -9,6 +9,7 @@ import (
"github.com/yunionio/jsonutils"
"github.com/yunionio/log"
"github.com/yunionio/pkg/gotypes"
"github.com/yunionio/pkg/tristate"
"github.com/yunionio/pkg/util/timeutils"
)
@@ -76,6 +77,14 @@ func setValueBySQLString(value reflect.Value, val string) error {
} else {
value.SetBool(true)
}
case tristate.TriStateType:
if val == "0" {
value.Set(tristate.TriStateFalseValue)
} else if val == "1" {
value.Set(tristate.TriStateTrueValue)
} else {
value.Set(tristate.TriStateNoneValue)
}
case gotypes.IntType, gotypes.Int8Type, gotypes.Int16Type, gotypes.Int32Type, gotypes.Int64Type:
valInt, err := strconv.ParseInt(val, 10, 64)
if err != nil {