BuildValue builds a value from any go type. sqltype.Value is also allowed.
(goval interface{})
| 69 | // BuildValue builds a value from any go type. sqltype.Value is |
| 70 | // also allowed. |
| 71 | func BuildValue(goval interface{}) (v Value, err error) { |
| 72 | // Look for the most common types first. |
| 73 | switch goval := goval.(type) { |
| 74 | case nil: |
| 75 | // no op |
| 76 | case []byte: |
| 77 | v = MakeTrusted(VarBinary, goval) |
| 78 | case int64: |
| 79 | v = MakeTrusted(Int64, strconv.AppendInt(nil, int64(goval), 10)) |
| 80 | case uint64: |
| 81 | v = MakeTrusted(Uint64, strconv.AppendUint(nil, uint64(goval), 10)) |
| 82 | case float64: |
| 83 | v = MakeTrusted(Float64, strconv.AppendFloat(nil, goval, 'f', -1, 64)) |
| 84 | case int: |
| 85 | v = MakeTrusted(Int64, strconv.AppendInt(nil, int64(goval), 10)) |
| 86 | case int8: |
| 87 | v = MakeTrusted(Int8, strconv.AppendInt(nil, int64(goval), 10)) |
| 88 | case int16: |
| 89 | v = MakeTrusted(Int16, strconv.AppendInt(nil, int64(goval), 10)) |
| 90 | case int32: |
| 91 | v = MakeTrusted(Int32, strconv.AppendInt(nil, int64(goval), 10)) |
| 92 | case uint: |
| 93 | v = MakeTrusted(Uint64, strconv.AppendUint(nil, uint64(goval), 10)) |
| 94 | case uint8: |
| 95 | v = MakeTrusted(Uint8, strconv.AppendUint(nil, uint64(goval), 10)) |
| 96 | case uint16: |
| 97 | v = MakeTrusted(Uint16, strconv.AppendUint(nil, uint64(goval), 10)) |
| 98 | case uint32: |
| 99 | v = MakeTrusted(Uint32, strconv.AppendUint(nil, uint64(goval), 10)) |
| 100 | case float32: |
| 101 | v = MakeTrusted(Float32, strconv.AppendFloat(nil, float64(goval), 'f', -1, 64)) |
| 102 | case string: |
| 103 | v = MakeTrusted(VarBinary, []byte(goval)) |
| 104 | case time.Time: |
| 105 | v = MakeTrusted(Datetime, []byte(goval.Format("2006-01-02 15:04:05"))) |
| 106 | case Value: |
| 107 | v = goval |
| 108 | case *querypb.BindVariable: |
| 109 | return ValueFromBytes(goval.Type, goval.Value) |
| 110 | default: |
| 111 | return v, fmt.Errorf("unexpected type %T: %v", goval, goval) |
| 112 | } |
| 113 | return v, nil |
| 114 | } |
| 115 | |
| 116 | // BuildConverted is like BuildValue except that it tries to |
| 117 | // convert a string or []byte to an integral if the target type |