本文整理汇总了Golang中github.com/cockroachdb/cockroach/pkg/roachpb.Value.SetDecimal方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.SetDecimal方法的具体用法?Golang Value.SetDecimal怎么用?Golang Value.SetDecimal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cockroachdb/cockroach/pkg/roachpb.Value
的用法示例。
在下文中一共展示了Value.SetDecimal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MarshalColumnValue
// MarshalColumnValue returns a Go primitive value equivalent of val, of the
// type expected by col. If val's type is incompatible with col, or if
// col's type is not yet implemented, an error is returned.
func MarshalColumnValue(col ColumnDescriptor, val parser.Datum) (roachpb.Value, error) {
var r roachpb.Value
if val == parser.DNull {
return r, nil
}
switch col.Type.Kind {
case ColumnType_BOOL:
if v, ok := val.(*parser.DBool); ok {
r.SetBool(bool(*v))
return r, nil
}
case ColumnType_INT:
if v, ok := val.(*parser.DInt); ok {
r.SetInt(int64(*v))
return r, nil
}
case ColumnType_FLOAT:
if v, ok := val.(*parser.DFloat); ok {
r.SetFloat(float64(*v))
return r, nil
}
case ColumnType_DECIMAL:
if v, ok := val.(*parser.DDecimal); ok {
err := r.SetDecimal(&v.Dec)
return r, err
}
case ColumnType_STRING:
if v, ok := val.(*parser.DString); ok {
r.SetString(string(*v))
return r, nil
}
case ColumnType_BYTES:
if v, ok := val.(*parser.DBytes); ok {
r.SetString(string(*v))
return r, nil
}
if v, ok := val.(*parser.DString); ok {
r.SetString(string(*v))
return r, nil
}
case ColumnType_DATE:
if v, ok := val.(*parser.DDate); ok {
r.SetInt(int64(*v))
return r, nil
}
case ColumnType_TIMESTAMP:
if v, ok := val.(*parser.DTimestamp); ok {
r.SetTime(v.Time)
return r, nil
}
case ColumnType_TIMESTAMPTZ:
if v, ok := val.(*parser.DTimestampTZ); ok {
r.SetTime(v.Time)
return r, nil
}
case ColumnType_INTERVAL:
if v, ok := val.(*parser.DInterval); ok {
err := r.SetDuration(v.Duration)
return r, err
}
default:
return r, errors.Errorf("unsupported column type: %s", col.Type.Kind)
}
return r, fmt.Errorf("value type %s doesn't match type %s of column %q",
val.ResolvedType(), col.Type.Kind, col.Name)
}
示例2: marshalValue
// marshalValue returns a roachpb.Value initialized from the source
// interface{}, returning an error if the types are not compatible.
func marshalValue(v interface{}) (roachpb.Value, error) {
var r roachpb.Value
// Handle a few common types via a type switch.
switch t := v.(type) {
case *roachpb.Value:
return *t, nil
case nil:
return r, nil
case bool:
r.SetBool(t)
return r, nil
case string:
r.SetBytes([]byte(t))
return r, nil
case []byte:
r.SetBytes(t)
return r, nil
case inf.Dec:
err := r.SetDecimal(&t)
return r, err
case roachpb.Key:
r.SetBytes([]byte(t))
return r, nil
case time.Time:
r.SetTime(t)
return r, nil
case duration.Duration:
err := r.SetDuration(t)
return r, err
case proto.Message:
err := r.SetProto(t)
return r, err
case roachpb.Value:
panic("unexpected type roachpb.Value (use *roachpb.Value)")
}
// Handle all of the Go primitive types besides struct and pointers. This
// switch also handles types based on a primitive type (e.g. "type MyInt
// int").
switch v := reflect.ValueOf(v); v.Kind() {
case reflect.Bool:
r.SetBool(v.Bool())
return r, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
r.SetInt(v.Int())
return r, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
r.SetInt(int64(v.Uint()))
return r, nil
case reflect.Float32, reflect.Float64:
r.SetFloat(v.Float())
return r, nil
case reflect.String:
r.SetBytes([]byte(v.String()))
return r, nil
}
return r, fmt.Errorf("unable to marshal %T: %v", v, v)
}