Skip to content

Commit

Permalink
fix: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed May 27, 2023
1 parent 8db8100 commit 1f7afb8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
36 changes: 35 additions & 1 deletion generate/client_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestFiltersClientEval(t *testing.T) {
//
// {"$set":{
// {{ if eq }}
// "field_int":10
// "field_int":10,
// {{end}}
// "field_float":1.1
// }}
Expand All @@ -146,6 +146,40 @@ func parseTestUpdateClientEval_first(d *Doc, args Args) {
d.FieldFloat = 1.1
}

// Update:
//
// {"$set":{
// "field_float":1.1
// {{ if eq }},
// "field_int":10
// {{end}}
// }}
func parseTestUpdateClientEval_last(d *Doc, args Args) {
d.FieldFloat = 1.1

if args.ArgInt == 10 {
d.FieldInt = 10
}
}

// Update:
//
// {"$set":{
// "field_float":1.1
// {{ if eq }},
// "field_int":10
// {{end}}
// }}
func parseTestUpdateClientEval_middle(d *Doc, args Args) {
d.FieldFloat = 1.1

if args.ArgInt == 10 {
d.FieldInt = 10
}

d.FieldBool = true
}

func TestUpdateClientEval(t *testing.T) {
execTests(t, "parseTestUpdateClientEval_", true)
}
41 changes: 28 additions & 13 deletions marshal/tigris/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,35 @@ func marshalUpdateExpr(upd expr.Expr, buf *bytes.Buffer) {

func marshalUpdateOp(op expr.Op, upd []expr.Expr, buf *bytes.Buffer) {
i := 0
oneNonOptional := false

for _, vv := range upd {
if vv.Type == op {
marshalUpdateExpr(vv, buf)

if i > 0 {
buf.WriteString(`,`)
}

marshalUpdateExpr(vv, buf)

i++
oneNonOptional = true
} else if vv.Type == expr.UpdIfOp {
marshalListTmplCond(vv.ListClient[0], buf)
var opBuf bytes.Buffer

marshalUpdateOp(op, vv.List, buf)
marshalUpdateOp(op, vv.List, &opBuf)

if i > 0 {
buf.WriteString(`,`)
}
if opBuf.Len() > 0 {
marshalListTmplCond(vv.ListClient[0], buf)

buf.WriteString(`{{end}}`)
if i > 0 {
buf.WriteString(`,`)
}

buf.Write(opBuf.Bytes())

buf.WriteString(`{{end}}`)
i++
}
}
}
}
Expand All @@ -79,13 +88,19 @@ func marshalUpdateLow(upd []expr.Expr, buf *bytes.Buffer) {
buf.WriteString(`{`)

for _, v := range expr.UpdOps {
buf.WriteString(`"`)
buf.WriteString(string(v))
buf.WriteString(`":{`)
var opBuf bytes.Buffer

marshalUpdateOp(v, upd, buf)
marshalUpdateOp(v, upd, &opBuf)

buf.WriteString(`}`)
if opBuf.Len() > 0 {
buf.WriteString(`"`)
buf.WriteString(string(v))
buf.WriteString(`":{`)

buf.Write(opBuf.Bytes())

buf.WriteString(`}`)
}
}

buf.WriteString(`}`)
Expand Down

0 comments on commit 1f7afb8

Please sign in to comment.