Skip to content

Commit

Permalink
Revert "Allow dot notation for update set. #Set method is now taking …
Browse files Browse the repository at this point in the history
…a ValuePath as a parameter"

This reverts commit 9b9f72f.
  • Loading branch information
asdine committed Aug 25, 2020
1 parent 96922a7 commit 4655ba8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 101 deletions.
102 changes: 5 additions & 97 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,108 +86,16 @@ func (fb FieldBuffer) GetByField(field string) (Value, error) {
return Value{}, ErrFieldNotFound
}

// SetFieldValue replaces a field if it already exists or creates one if not.
func (fb *FieldBuffer) SetFieldValue(field string, reqValue Value) error {
_, err := fb.GetByField(field)
switch err {
case ErrFieldNotFound:
fb.Add(field, reqValue)
return nil
case nil:
_ = fb.Replace(field, reqValue)
return nil
}

return err
}

// SetValueFromValue deep replaces or creates a field
// through the value path to get the value to set or replace.
func (fb *FieldBuffer) SetValueFromValue(v Value, p ValuePath, newValue Value) (Value, error) {

switch v.Type {
case DocumentValue:
var buf FieldBuffer
err := buf.Copy(v.V.(Document))
if err != nil {
return v, err
}

if len(p) == 1 {
err = buf.SetFieldValue(p[0], newValue)
return NewDocumentValue(&buf), err
}

va, err := buf.GetByField(p[0])
if err != nil {
return v, err
}

va, err = buf.SetValueFromValue(va, p[1:], newValue)
if err != nil {
return v, err
}

err = buf.SetFieldValue(p[0], va)
return NewDocumentValue(&buf), err
case ArrayValue:
var vb ValueBuffer
err := vb.Copy(v.V.(Array))
if err != nil {
return v, err
}


index, err := strconv.Atoi(p[0])
if err != nil {
return v, err
}

va, err := vb.GetByIndex(index)
if err != nil {
return v, err
}

if len(p) == 1 {
err = vb.Replace(index, newValue)
return NewArrayValue(&vb), err
}

va, err = fb.SetValueFromValue(va, p[1:], newValue)
err = vb.Replace(index, va)
return NewArrayValue(&vb), err
}

return v, nil
}

// Set replaces a field if it already exists or creates one if not.
func (fb *FieldBuffer) Set(path ValuePath, reqValue Value) error {
// check if the ValuePath contains only one field to set or replace
if len(path) == 1 {
// Set or replace the unique field
return fb.SetFieldValue(path[0], reqValue)
}

for i, field := range fb.fields {
if path[0] == field.Field {
var buf FieldBuffer
err := buf.Copy(fb)
if err != nil {
return err
}

v, err := buf.SetValueFromValue(field.Value, path[1:], reqValue)
if err != nil {
return err
}
func (fb *FieldBuffer) Set(f string, v Value) {
for i := range fb.fields {
if fb.fields[i].Field == f {
fb.fields[i].Value = v
return nil
return
}
}

//return Err if the request is like foo.1.2.etc where foo doesn't exist
return ErrFieldNotFound
fb.Add(f, v)
}

// Iterate goes through all the fields of the document and calls the given function by passing each one of them.
Expand Down
18 changes: 14 additions & 4 deletions sql/planner/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,20 @@ func (n *setNode) toStream(st document.Stream) (document.Stream, error) {
return nil, err
}

path := document.NewValuePath(n.field)
err = fb.Set(path, ev)

return &fb, err
_, err = fb.GetByField(n.field)

switch err {
case nil:
// If no error, it means that the field already exists
// and it should be replaced.
_ = fb.Replace(n.field, ev)
case document.ErrFieldNotFound:
// If the field doesn't exist,
// it should be added to the document.
fb.Set(n.field, ev)
}

return &fb, nil
}), nil
}

Expand Down

0 comments on commit 4655ba8

Please sign in to comment.