diff --git a/Gopkg.lock b/Gopkg.lock index 403d5479f1..58bf74b691 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -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 diff --git a/Gopkg.toml b/Gopkg.toml index 46a156f8b1..87b2b20a4e 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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" diff --git a/vendor/github.com/yunionio/jsonutils/equals.go b/vendor/github.com/yunionio/jsonutils/equals.go index 4d7139cc0e..aa7cf70677 100644 --- a/vendor/github.com/yunionio/jsonutils/equals.go +++ b/vendor/github.com/yunionio/jsonutils/equals.go @@ -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 { diff --git a/vendor/github.com/yunionio/jsonutils/marshal.go b/vendor/github.com/yunionio/jsonutils/marshal.go index 800256b1c7..fd142cc90d 100644 --- a/vendor/github.com/yunionio/jsonutils/marshal.go +++ b/vendor/github.com/yunionio/jsonutils/marshal.go @@ -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: diff --git a/vendor/github.com/yunionio/sqlchemy/column.go b/vendor/github.com/yunionio/sqlchemy/column.go index 3666599ca2..e3146324a3 100644 --- a/vendor/github.com/yunionio/sqlchemy/column.go +++ b/vendor/github.com/yunionio/sqlchemy/column.go @@ -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 diff --git a/vendor/github.com/yunionio/sqlchemy/debug.go b/vendor/github.com/yunionio/sqlchemy/debug.go index 0c1227f9a9..3ff7657616 100644 --- a/vendor/github.com/yunionio/sqlchemy/debug.go +++ b/vendor/github.com/yunionio/sqlchemy/debug.go @@ -1,5 +1,5 @@ package sqlchemy -const ( +var ( DEBUG_SQLCHEMY = false ) diff --git a/vendor/github.com/yunionio/sqlchemy/parser.go b/vendor/github.com/yunionio/sqlchemy/parser.go index eb38b591c4..f68ad1fe3d 100644 --- a/vendor/github.com/yunionio/sqlchemy/parser.go +++ b/vendor/github.com/yunionio/sqlchemy/parser.go @@ -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) diff --git a/vendor/github.com/yunionio/sqlchemy/reflect.go b/vendor/github.com/yunionio/sqlchemy/reflect.go index 648b95c497..88cfa85b3b 100644 --- a/vendor/github.com/yunionio/sqlchemy/reflect.go +++ b/vendor/github.com/yunionio/sqlchemy/reflect.go @@ -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 {