diff --git a/document/document.go b/document/document.go index fbcd2c836..e985d2ae0 100644 --- a/document/document.go +++ b/document/document.go @@ -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. diff --git a/sql/planner/tree.go b/sql/planner/tree.go index d1fb3ec7b..57dd0ac8a 100644 --- a/sql/planner/tree.go +++ b/sql/planner/tree.go @@ -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 }