diff options
Diffstat (limited to 'internal/tree/mutations.go')
-rw-r--r-- | internal/tree/mutations.go | 89 |
1 files changed, 0 insertions, 89 deletions
diff --git a/internal/tree/mutations.go b/internal/tree/mutations.go deleted file mode 100644 index 173828d..0000000 --- a/internal/tree/mutations.go +++ /dev/null @@ -1,89 +0,0 @@ -package tree - -import ( - "context" - "fmt" - "strings" - - "go.neonxp.dev/djson/internal/model" - json "go.neonxp.dev/json/model" -) - -func (t *stdCore) Mutate(ctx context.Context, mut *model.Mutation) error { - t.mu.Lock() - defer t.mu.Unlock() - if err := t.execute(mut); err != nil { - return err - } - return t.storage.Commit(ctx, *mut) -} - -func (t *stdCore) execute(mut *model.Mutation) error { - switch mut.Type { - case model.Create: - if len(mut.Path) == 0 { - // create root node - inObject, ok := mut.Body.(*json.ObjectNode) - if !ok { - return fmt.Errorf("root node must be object") - } - t.Root = *inObject - return nil - } - key := mut.Path[len(mut.Path)-1] - path := mut.Path[:len(mut.Path)-1] - target, err := json.Query(&t.Root, path) - if err != nil { - return err - } - targetObject, ok := target.(*json.ObjectNode) - if !ok { - return fmt.Errorf("node %s is not object", strings.Join(path, "/")) - } - if err := targetObject.Value.Set(key, mut.Body); err != nil { - return err - } - case model.Merge: - inObject, ok := mut.Body.(*json.ObjectNode) - if !ok { - return fmt.Errorf("patch allowed only for objects") - } - if len(mut.Path) == 0 { - // patch root node - t.Root.Merge(inObject) - return nil - } - target, err := json.Query(&t.Root, mut.Path) - if err != nil { - return err - } - targetObject, ok := target.(*json.ObjectNode) - if !ok { - return fmt.Errorf("patch allowed only for objects") - } - targetObject.Merge(inObject) - case model.Remove: - if len(mut.Path) == 0 { - return fmt.Errorf("can't remove root node. Only replace or create avaliable") - } - key := mut.Path[len(mut.Path)-1] - if len(mut.Path) == 1 { - t.Root.Remove(key) - return nil - } - path := mut.Path[:len(mut.Path)-1] - target, err := json.Query(&t.Root, path) - if err != nil { - return err - } - targetObject, ok := target.(*json.ObjectNode) - if !ok { - return fmt.Errorf("remove allowed only from objects") - } - targetObject.Remove(key) - default: - return fmt.Errorf("invalid command type: %d", mut.Type) - } - t.eventDispatcher.Notify(mut.Path, mut) - return nil -} |