Skip to content

Commit

Permalink
Update value, suport interface, slice, map (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Jul 9, 2024
1 parent 852c844 commit 64400e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 17 additions & 3 deletions builder_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@
// limitations under the License.
package sqlxb

import "time"
import (
"encoding/json"
"time"
)

type UpdateBuilder struct {
bbs []Bb
}

func (ub *UpdateBuilder) Set(k string, v interface{}) *UpdateBuilder {

defer func() *UpdateBuilder {
bytes, _ := json.Marshal(v)
ub.bbs = append(ub.bbs, Bb{
op: "SET",
key: k,
value: string(bytes),
})
return ub
}()

switch v.(type) {
case string:
if v.(string) == "" {
Expand All @@ -42,8 +55,9 @@ func (ub *UpdateBuilder) Set(k string, v interface{}) *UpdateBuilder {
case time.Time:
ts := v.(time.Time).Format("2006-01-02 15:04:05")
v = ts
case []interface{}:
panic("Builder.doGLE(ke, []arr), [] ?")
case interface{}:
bytes, _ := json.Marshal(v)
v = string(bytes)
default:
if v == nil {
return ub
Expand Down
10 changes: 5 additions & 5 deletions cond_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func (cb *CondBuilder) doIn(p string, k string, vs ...interface{}) *CondBuilder
continue
}
ss = append(ss, s)
case []interface{}:
panic("Builder.doIn(ke, ([]arr)), ([]arr) ?")
case interface{}:
panic("Builder.doIn(ke, (obj), ([]arr) ? ...")
default:
panic("Builder.doIn(ke, (*[]arr)), (*[]arr) ?")
panic("Builder.doIn(ke, (*obj)), (*[]arr) ? ...")
}
}

Expand Down Expand Up @@ -112,8 +112,8 @@ func (cb *CondBuilder) doGLE(p string, k string, v interface{}) *CondBuilder {
case time.Time:
ts := v.(time.Time).Format("2006-01-02 15:04:05")
return cb.addBb(p, k, ts)
case []interface{}:
panic("Builder.doGLE(ke, []arr), [] ?")
case interface{}:
panic("Builder.doGLE(ke, obj, [] ? ...")
default:
if v == nil {
return cb
Expand Down

0 comments on commit 64400e4

Please sign in to comment.