summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--cmd/api/flags.go12
-rw-r--r--cmd/api/main.go64
-rw-r--r--cmd/cli/main.go59
-rw-r--r--etc/config.json5
-rw-r--r--go.mod45
-rw-r--r--go.sum250
-rw-r--r--internal/command/commandtype_string.go (renamed from internal/model/commandtype_string.go)2
-rw-r--r--internal/command/mutations.go21
-rw-r--r--internal/command/objectid.go47
-rw-r--r--internal/config/config.go30
-rw-r--r--internal/core/core.go203
-rw-r--r--internal/events/contract.go6
-rw-r--r--internal/events/events.go8
-rw-r--r--internal/events/node.go12
-rw-r--r--internal/handler/auth.go15
-rw-r--r--internal/handler/contract.go12
-rw-r--r--internal/handler/crud.go88
-rw-r--r--internal/handler/events.go16
-rw-r--r--internal/handler/handler.go11
-rw-r--r--internal/handler/hearthbeat.go15
-rw-r--r--internal/logger/logger.go27
-rw-r--r--internal/model/mutations.go36
-rw-r--r--internal/node/acl.go12
-rw-r--r--internal/node/array.go23
-rw-r--r--internal/node/bool.go9
-rw-r--r--internal/node/node.go71
-rw-r--r--internal/node/number.go9
-rw-r--r--internal/node/object.go22
-rw-r--r--internal/node/string.go9
-rw-r--r--internal/storage/contract.go14
-rw-r--r--internal/storage/file.go79
-rw-r--r--internal/tree/contract.go23
-rw-r--r--internal/tree/core.go50
-rw-r--r--internal/tree/mutations.go89
35 files changed, 822 insertions, 575 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..927a3fb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+data
+**/__debug_bin
+go.work* \ No newline at end of file
diff --git a/cmd/api/flags.go b/cmd/api/flags.go
deleted file mode 100644
index 4d967fd..0000000
--- a/cmd/api/flags.go
+++ /dev/null
@@ -1,12 +0,0 @@
-package main
-
-import "flag"
-
-var (
- // cluster = flag.String("cluster", "", "Master node address (ex: 192.168.1.10:5657)")
- // clusterID = flag.String("cluster_id", "", "Cluster id")
- // clusterAddr = flag.String("cluster_addr", ":5657", "Self address for cluster")
- apiAddr = flag.String("api_addr", ":5656", "API address")
- dbPath = flag.String("db_path", "/var/djson.db", "DB file")
- logLevel = flag.String("log_level", "info", "Log level")
-)
diff --git a/cmd/api/main.go b/cmd/api/main.go
index 3d2f587..f6d0166 100644
--- a/cmd/api/main.go
+++ b/cmd/api/main.go
@@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
+ "log"
"net/http"
"os"
"os/signal"
@@ -10,39 +11,54 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog"
- "github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
+ badger "github.com/dgraph-io/badger/v3"
+
+ "go.neonxp.dev/djson/internal/config"
+ "go.neonxp.dev/djson/internal/core"
"go.neonxp.dev/djson/internal/events"
"go.neonxp.dev/djson/internal/handler"
- "go.neonxp.dev/djson/internal/storage"
- "go.neonxp.dev/djson/internal/tree"
+ "go.neonxp.dev/djson/internal/logger"
)
+var configFile = flag.String("config", "/etc/djson/config.json", "Path to config file")
+
func main() {
flag.Parse()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
- logger := httplog.NewLogger("djson", httplog.Options{
+ cfg, err := config.Parse(*configFile)
+ if err != nil {
+ panic(err)
+ }
+
+ baseLogger := httplog.NewLogger("djson", httplog.Options{
JSON: true,
- LogLevel: *logLevel,
+ LogLevel: cfg.Log.Level,
})
- // Tree storage
- storage, err := storage.New(*dbPath, logger)
+ appLogger := logger.Logger{
+ Logger: baseLogger,
+ }
+
+ dbOpts := badger.DefaultOptions(cfg.DB)
+ dbOpts.Logger = appLogger
+
+ db, err := badger.Open(dbOpts)
if err != nil {
- panic(err)
+ log.Fatal(err)
}
+ defer db.Close()
+
eventsDispatcher := events.New()
- // Tree engine
- core := tree.New(
- storage,
- eventsDispatcher,
- )
- if err := core.Init(); err != nil {
- panic(err)
+
+ // Tree core
+ core := core.New(db)
+ if err := core.Init(ctx); err != nil {
+ log.Fatal(err)
}
// Tree HTTP wrapper
@@ -51,7 +67,7 @@ func main() {
// HTTP router
r := chi.NewRouter()
- r.Use(httplog.RequestLogger(logger))
+ r.Use(httplog.RequestLogger(appLogger.Logger))
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
@@ -61,20 +77,14 @@ func main() {
r.Route("/events", treeHandler.HandleEvents)
server := &http.Server{
- Addr: *apiAddr,
+ Addr: cfg.Listen,
Handler: r,
}
- if err := run(ctx, logger, server, storage); err != nil {
- panic(err)
- }
-}
-
-func run(ctx context.Context, logger zerolog.Logger, server *http.Server, storage storage.Storage) error {
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
- logger.Info().Str("server listen", *apiAddr).Send()
+ appLogger.Info().Str("server listen", cfg.Listen).Send()
if err := server.ListenAndServe(); err != http.ErrServerClosed {
return err
}
@@ -83,9 +93,11 @@ func run(ctx context.Context, logger zerolog.Logger, server *http.Server, storag
eg.Go(func() error {
<-ctx.Done()
- storage.Close()
+ db.Close()
return server.Close()
})
- return eg.Wait()
+ if err := eg.Wait(); err != nil {
+ panic(err)
+ }
}
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
deleted file mode 100644
index 5cfd5e0..0000000
--- a/cmd/cli/main.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package main
-
-import (
- "fmt"
- "log"
- "os"
- "time"
-
- "github.com/lestrrat-go/jwx/v2/jwa"
- "github.com/lestrrat-go/jwx/v2/jwt"
- "github.com/urfave/cli/v2"
- "go.neonxp.dev/djson/internal/config"
-)
-
-func main() {
- app := &cli.App{
- Name: "djson cli tool",
- Commands: []*cli.Command{
- {
- Name: "token",
- Action: func(ctx *cli.Context) error {
- cfg, err := config.Parse(ctx.String("config"))
- if err != nil {
- return err
- }
-
- t := jwt.New()
- t.Set(jwt.SubjectKey, `djson`)
- t.Set(jwt.IssuedAtKey, time.Now())
- t.Set("allowed", []string{
- "a/b/c",
- "d/e/f",
- })
- signed, err := jwt.Sign(t, jwt.WithKey(
- jwa.KeyAlgorithmFrom(cfg.JWT.Algorithm),
- cfg.JWT.PrivateKey,
- ))
- if err != nil {
- return err
- }
- fmt.Println(string(signed))
- return nil
- },
- },
- },
- Action: cli.ShowAppHelp,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "config",
- Usage: "Path to config file",
- Value: "/etc/djson/config.json",
- },
- },
- }
-
- if err := app.Run(os.Args); err != nil {
- log.Fatal(err)
- }
-}
diff --git a/etc/config.json b/etc/config.json
index 246d903..f0cf5e8 100644
--- a/etc/config.json
+++ b/etc/config.json
@@ -1,7 +1,8 @@
{
"listen": ":5656",
- "db": {
- "path": "tree.db"
+ "db": "data",
+ "log": {
+ "level": "debug"
},
"jwt": {
"algorithm": "HS512",
diff --git a/go.mod b/go.mod
index b6c1b97..55a64d8 100644
--- a/go.mod
+++ b/go.mod
@@ -3,32 +3,37 @@ module go.neonxp.dev/djson
go 1.19
require (
- github.com/go-chi/chi/v5 v5.0.7
+ github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/httplog v0.2.5
- go.neonxp.dev/json v0.0.4
+ github.com/vmihailenco/msgpack/v5 v5.3.5
+ go.neonxp.dev/json v0.1.1
+ go.neonxp.dev/objectid v0.0.1
golang.org/x/sync v0.1.0
)
require (
- github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
- github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
- github.com/goccy/go-json v0.9.11 // indirect
- github.com/lestrrat-go/blackmagic v1.0.1 // indirect
- github.com/lestrrat-go/httpcc v1.0.1 // indirect
- github.com/lestrrat-go/httprc v1.0.4 // indirect
- github.com/lestrrat-go/iter v1.0.2 // indirect
- github.com/lestrrat-go/jwx/v2 v2.0.6 // indirect
- github.com/lestrrat-go/option v1.0.0 // indirect
- github.com/russross/blackfriday/v2 v2.1.0 // indirect
- github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
- golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
+ github.com/cespare/xxhash v1.1.0 // indirect
+ github.com/cespare/xxhash/v2 v2.2.0 // indirect
+ github.com/dgraph-io/ristretto v0.1.1 // indirect
+ github.com/dustin/go-humanize v1.0.0 // indirect
+ github.com/gogo/protobuf v1.3.2 // indirect
+ github.com/golang/glog v1.0.0 // indirect
+ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
+ github.com/golang/protobuf v1.5.2 // indirect
+ github.com/golang/snappy v0.0.4 // indirect
+ github.com/google/flatbuffers v22.11.23+incompatible // indirect
+ github.com/klauspost/compress v1.15.13 // indirect
+ github.com/pkg/errors v0.9.1 // indirect
+ github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
+ go.opencensus.io v0.24.0 // indirect
+ golang.org/x/net v0.4.0 // indirect
+ google.golang.org/protobuf v1.28.1 // indirect
)
require (
- github.com/go-chi/jwtauth/v5 v5.1.0
- github.com/mattn/go-colorable v0.1.12 // indirect
- github.com/mattn/go-isatty v0.0.14 // indirect
- github.com/rs/zerolog v1.27.0
- github.com/urfave/cli/v2 v2.23.7
- golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
+ github.com/dgraph-io/badger/v3 v3.2103.5
+ github.com/mattn/go-colorable v0.1.13 // indirect
+ github.com/mattn/go-isatty v0.0.16 // indirect
+ github.com/rs/zerolog v1.28.0
+ golang.org/x/sys v0.3.0 // indirect
)
diff --git a/go.sum b/go.sum
index 937c746..11b480b 100644
--- a/go.sum
+++ b/go.sum
@@ -1,69 +1,235 @@
+cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
+github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
+github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
+github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
+github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
+github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
+github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
+github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
+github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
+github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
+github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
+github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
+github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
-github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
-github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
-github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4=
-github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc=
-github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
+github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg=
+github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw=
+github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
+github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
+github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
+github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
+github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
+github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
+github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
+github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
+github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
+github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
+github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
+github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
+github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/httplog v0.2.5 h1:S02eG9NTrB/9kk3Q3RA3F6CR2b+v8WzB8IxK+zq3dBo=
github.com/go-chi/httplog v0.2.5/go.mod h1:/pIXuFSrOdc5heKIJRA5Q2mW7cZCI2RySqFZNFoZjKg=
-github.com/go-chi/jwtauth/v5 v5.1.0 h1:wJyf2YZ/ohPvNJBwPOzZaQbyzwgMZZceE1m8FOzXLeA=
-github.com/go-chi/jwtauth/v5 v5.1.0/go.mod h1:MA93hc1au3tAQwCKry+fI4LqJ5MIVN4XSsglOo+lSc8=
-github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
-github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
-github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=
-github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
-github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
-github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
-github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJGdI8=
-github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
-github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
-github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
-github.com/lestrrat-go/jwx/v2 v2.0.6 h1:RlyYNLV892Ed7+FTfj1ROoF6x7WxL965PGTHso/60G0=
-github.com/lestrrat-go/jwx/v2 v2.0.6/go.mod h1:aVrGuwEr3cp2Prw6TtQvr8sQxe+84gruID5C9TxT64Q=
-github.com/lestrrat-go/option v1.0.0 h1:WqAWL8kh8VcSoD6xjSH34/1m8yxluXQbDeKNfvFeEO4=
-github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
-github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
+github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
+github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
+github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
+github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
+github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
+github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
+github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
+github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
+github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
+github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
+github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
+github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
+github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
+github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
+github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
+github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
+github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
+github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
+github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
+github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
+github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
+github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
+github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
+github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
+github.com/google/flatbuffers v22.11.23+incompatible h1:334TygA7iuxt0hoamawsM36xoui01YiouEZnr0qeFMI=
+github.com/google/flatbuffers v22.11.23+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
+github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
+github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
+github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
+github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
+github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
+github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
+github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
+github.com/klauspost/compress v1.15.13 h1:NFn1Wr8cfnenSJSA46lLq4wHCcBzKTSjnBIexDMMOV0=
+github.com/klauspost/compress v1.15.13/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
+github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
-github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
+github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
+github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
+github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
+github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
+github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
+github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
+github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
-github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
+github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
-github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
-github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
+github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
+github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
+github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
+github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
+github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
+github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
+github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
+github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
+github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
+github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
+github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
+github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
-github.com/urfave/cli/v2 v2.23.7 h1:YHDQ46s3VghFHFf1DdF+Sh7H4RqhcM+t0TmZRJx4oJY=
-github.com/urfave/cli/v2 v2.23.7/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
-github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
-github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
-go.neonxp.dev/json v0.0.4 h1:PHLJANFNXv8vn79YWpWEW7l0dSxZ7XmuRQsHs9JC20w=
-go.neonxp.dev/json v0.0.4/go.mod h1:d+el/XRG46QCbCsly5PgUqyiwuU9GzBxFTGCuGHyd0w=
-golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f h1:OeJjE6G4dgCY4PIXvIRQbE8+RX+uXZyGhUy/ksMGJoc=
-golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
-golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
+github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
+github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
+github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
+github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
+github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
+github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
+github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
+github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
+go.neonxp.dev/json v0.1.1 h1:a5ZRyBQpPpGBX/z66bIYAtcXUGJ0ONlZu177xfsrhgk=
+go.neonxp.dev/json v0.1.1/go.mod h1:d+el/XRG46QCbCsly5PgUqyiwuU9GzBxFTGCuGHyd0w=
+go.neonxp.dev/objectid v0.0.1 h1:m8JBq5peIjcVx8BKZ+6mrx5EtjCUK641z0sLCaLCYhk=
+go.neonxp.dev/objectid v0.0.1/go.mod h1:tQFSOxvgeq6ooOnmjLx/O3K5SoamyX6XPAPSZykqURs=
+go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
+go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
+go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
+golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
+golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
+golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
+golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
+golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
+golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
+golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
+golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
+golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
+golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
-golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
+golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
+golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
+golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
+golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
+google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
+google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
+google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
+google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
+google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
+google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
+google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
+google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
+google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
+google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
+google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
+google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
+google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
+google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
+google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
+google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
+google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
+google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
+google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
+google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
+google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
+google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
+google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
+google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
+honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
diff --git a/internal/model/commandtype_string.go b/internal/command/commandtype_string.go
index 1ad4635..ef5080a 100644
--- a/internal/model/commandtype_string.go
+++ b/internal/command/commandtype_string.go
@@ -1,6 +1,6 @@
// Code generated by "stringer -type=CommandType"; DO NOT EDIT.
-package model
+package command
import "strconv"
diff --git a/internal/command/mutations.go b/internal/command/mutations.go
new file mode 100644
index 0000000..3fe1e3f
--- /dev/null
+++ b/internal/command/mutations.go
@@ -0,0 +1,21 @@
+package command
+
+import (
+ "go.neonxp.dev/objectid"
+)
+
+type Mutation struct {
+ ID objectid.ID
+ Type CommandType
+ Path []string
+ Data string
+}
+
+//go:generate stringer -type=CommandType
+type CommandType int
+
+const (
+ Create CommandType = iota
+ Merge
+ Remove
+)
diff --git a/internal/command/objectid.go b/internal/command/objectid.go
new file mode 100644
index 0000000..6eda868
--- /dev/null
+++ b/internal/command/objectid.go
@@ -0,0 +1,47 @@
+package command
+
+import (
+ "encoding/binary"
+ "encoding/hex"
+ "math/rand"
+ "time"
+)
+
+var iterator uint64
+
+func init() {
+ rand.Seed(time.Now().UnixMicro())
+ iterator = rand.Uint64()
+}
+
+func NewObjectID() ObjectID {
+ iterator++
+ p1 := uint64(time.Now().UnixMicro())
+ p2 := iterator
+ p3 := rand.Uint64()
+ r := ObjectID{}
+ r = binary.BigEndian.AppendUint64(r, p1)
+ r = binary.BigEndian.AppendUint64(r, p2)
+ r = binary.BigEndian.AppendUint64(r, p3)
+ return r
+}
+
+type ObjectID []byte
+
+func (o ObjectID) MarshalJSON() ([]byte, error) {
+ res := make([]byte, 0, hex.EncodedLen(len(o)))
+ hex.Encode(res, o)
+ return res, nil
+}
+
+func (o ObjectID) String() string {
+ return hex.EncodeToString(o)
+}
+
+func (o ObjectID) Time() time.Time {
+ if len(o) < 8 {
+ return time.Time{}
+ }
+ t := o[:8]
+ return time.UnixMicro(int64(binary.BigEndian.Uint64(t)))
+}
diff --git a/internal/config/config.go b/internal/config/config.go
index 0c48a87..9f581de 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -3,18 +3,20 @@ package config
import (
"os"
+ "go.neonxp.dev/djson/internal/node"
+
"go.neonxp.dev/json"
- "go.neonxp.dev/json/model"
)
type Config struct {
Listen string
- DB dbConfig
+ Log logConfig
+ DB string
JWT jwtConfig
}
-type dbConfig struct {
- Path string
+type logConfig struct {
+ Level string
}
type jwtConfig struct {
@@ -28,23 +30,23 @@ func Parse(file string) (*Config, error) {
return nil, err
}
- cfgNode, err := json.Unmarshal(fb)
+ j := json.New(node.Factory)
+ root, err := j.Unmarshal(string(fb))
if err != nil {
return nil, err
}
- listen := model.MustQuery(cfgNode, []string{"listen"}).(*model.StringNode).Value
- dbPath := model.MustQuery(cfgNode, []string{"db", "path"}).(*model.StringNode).Value
- algorithm := model.MustQuery(cfgNode, []string{"jwt", "algorithm"}).(*model.StringNode).Value
- privateKey := model.MustQuery(cfgNode, []string{"jwt", "privateKey"}).(*model.StringNode).Value
cfg := &Config{
- Listen: listen,
- DB: dbConfig{
- Path: dbPath,
+ Listen: json.MustQuery(root, []string{"listen"}).(*node.Node).GetString(),
+ Log: logConfig{
+ Level: json.MustQuery(root, []string{"log", "level"}).(*node.Node).GetString(),
},
+ DB: json.MustQuery(root, []string{"db"}).(*node.Node).GetString(),
JWT: jwtConfig{
- Algorithm: algorithm,
- PrivateKey: []byte(privateKey),
+ Algorithm: json.MustQuery(root, []string{"jwt", "algorithm"}).(*node.Node).GetString(),
+ PrivateKey: []byte(
+ json.MustQuery(root, []string{"jwt", "privateKey"}).(*node.Node).GetString(),
+ ),
},
}
diff --git a/internal/core/core.go b/internal/core/core.go
new file mode 100644
index 0000000..34718bd
--- /dev/null
+++ b/internal/core/core.go
@@ -0,0 +1,203 @@
+package core
+
+import (
+ "context"
+ "fmt"
+ "strconv"
+ "sync"
+
+ "github.com/dgraph-io/badger/v3"
+ "github.com/vmihailenco/msgpack/v5"
+ "go.neonxp.dev/djson/internal/command"
+ "go.neonxp.dev/djson/internal/node"
+ "go.neonxp.dev/json"
+)
+
+type Core struct {
+ storage *badger.DB
+ root *node.Node
+ mu sync.RWMutex
+ json *json.JSON
+}
+
+func New(storage *badger.DB) *Core {
+ return &Core{
+ storage: storage,
+ root: nil,
+ json: json.New(node.Factory),
+ }
+}
+
+func (c *Core) Init(ctx context.Context) error {
+ return c.storage.View(func(txn *badger.Txn) error {
+ opts := badger.DefaultIteratorOptions
+ opts.PrefetchSize = 10
+ it := txn.NewIterator(opts)
+ defer it.Close()
+ for it.Rewind(); it.Valid(); it.Next() {
+ item := it.Item()
+ err := item.Value(func(v []byte) error {
+ mut := &command.Mutation{}
+ if err := msgpack.Unmarshal(v, mut); err != nil {
+ return err
+ }
+ return c.apply(ctx, mut)
+ })
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+ })
+}
+
+func (c *Core) Apply(ctx context.Context, mutation *command.Mutation) error {
+ return c.storage.Update(func(txn *badger.Txn) error {
+ if err := c.apply(ctx, mutation); err != nil {
+ return err
+ }
+ mb, err := msgpack.Marshal(mutation)
+ if err != nil {
+ return err
+ }
+
+ e := &badger.Entry{
+ Key: mutation.ID,
+ Value: mb,
+ }
+
+ return txn.SetEntry(e)
+ })
+}
+
+func (c *Core) apply(ctx context.Context, mutation *command.Mutation) error {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+
+ switch mutation.Type {
+ case command.Create:
+ n, err := c.json.Unmarshal(mutation.Data)
+ if err != nil {
+ return err
+ }
+ if err := c.create(mutation.Path, n.(*node.Node)); err != nil {
+ return err
+ }
+ case command.Merge:
+ n, err := c.json.Unmarshal(mutation.Data)
+ if err != nil {
+ return err
+ }
+ if err := c.merge(mutation.Path, n.(*node.Node)); err != nil {
+ return err
+ }
+ case command.Remove:
+ if err := c.remove(mutation.Path); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (c *Core) Query(ctx context.Context, query []string) (json.Node, error) {
+ return json.Query(c.root, query)
+}
+
+func (c *Core) create(path []string, n *node.Node) error {
+ if len(path) == 0 {
+ c.root = n
+ return nil
+ }
+
+ path, last := path[:len(path)-1], path[len(path)-1]
+ parent, err := json.Query(c.root, path)
+ if err != nil {
+ return fmt.Errorf("parent node not found")
+ }
+
+ parentNode, ok := parent.(*node.Node)
+ if !ok {
+ return fmt.Errorf("invalid node")
+ }
+
+ switch parentNode.Type {
+ case json.ArrayType:
+ if last == "[]" {
+ parentNode.Append(n)
+ return nil
+ }
+ idx, err := strconv.Atoi(last)
+ if err != nil {
+ return fmt.Errorf("cant use %s as array index", last)
+ }
+ if idx < 0 || idx >= parentNode.Len() {
+ return fmt.Errorf("index %d out of bounds [0, %d]", idx, parentNode.Len()-1)
+ }
+ parentNode.SetByIndex(idx, n)
+ case json.ObjectType:
+ parentNode.SetKeyValue(last, n)
+ default:
+ return fmt.Errorf("cant add node to node of type %s", parentNode.Type)
+ }
+ return nil
+}
+
+func (c *Core) merge(path []string, n *node.Node) error {
+ parent, err := json.Query(c.root, path)
+ if err != nil {
+ return fmt.Errorf("parent node not found")
+ }
+
+ parentNode, ok := parent.(*node.Node)
+ if !ok {
+ return fmt.Errorf("invalid node")
+ }
+
+ if n.Type != parentNode.Type {
+ return fmt.Errorf("merging nodes must be same type")
+ }
+
+ switch n.Type {
+ case json.ObjectType:
+ parentNode.Merge(n)
+ case json.ArrayType:
+ for i := 0; i < n.Len(); i++ {
+ parentNode.Append(n.Index(i))
+ }
+ default:
+ return fmt.Errorf("can merge only objects or arrays")
+ }
+ return nil
+}
+
+func (c *Core) remove(path []string) error {
+ if len(path) == 0 {
+ c.root = nil
+ return nil
+ }
+ path, last := path[:len(path)-1], path[len(path)-1]
+ parent, err := json.Query(c.root, path)
+ if err != nil {
+ return fmt.Errorf("parent node not found")
+ }
+ parentNode, ok := parent.(*node.Node)
+ if !ok {
+ return fmt.Errorf("invalid node")
+ }
+ switch parentNode.Type {
+ case json.ObjectType:
+ parentNode.RemoveByKey(last)
+ case json.ArrayType:
+ idx, err := strconv.Atoi(last)
+ if err != nil {
+ return fmt.Errorf("cant use %s as array index", last)
+ }
+ if idx < 0 || idx >= parentNode.Len() {
+ return fmt.Errorf("index %d out of bounds [0, %d]", idx, parentNode.Len()-1)
+ }
+ parentNode.RemoveByIndex(idx)
+ default:
+ return fmt.Errorf("can remove children only from object or array")
+ }
+ return nil
+}
diff --git a/internal/events/contract.go b/internal/events/contract.go
index dc5003f..5bea6ac 100644
--- a/internal/events/contract.go
+++ b/internal/events/contract.go
@@ -1,11 +1,11 @@
package events
-import "go.neonxp.dev/djson/internal/model"
+import "go.neonxp.dev/djson/internal/command"
type Dispatcher interface {
- Subscribe(path []string, id string, ch chan model.Mutation)
+ Subscribe(path []string, id string, ch chan command.Mutation)
Unsubscribe(path []string, id string)
- Notify(path []string, event *model.Mutation)
+ Notify(path []string, event *command.Mutation)
}
diff --git a/internal/events/events.go b/internal/events/events.go
index 49731b8..3ee87e4 100644
--- a/internal/events/events.go
+++ b/internal/events/events.go
@@ -3,7 +3,7 @@ package events
import (
"sync"
- "go.neonxp.dev/djson/internal/model"
+ "go.neonxp.dev/djson/internal/command"
)
type stdDispatcher struct {
@@ -14,14 +14,14 @@ func New() Dispatcher {
return &stdDispatcher{
tree: subscriberNode{
children: make(map[string]*subscriberNode),
- channels: make(map[string]chan model.Mutation),
+ channels: make(map[string]chan command.Mutation),
parent: nil,
mu: sync.RWMutex{},
},
}
}
-func (ed *stdDispatcher) Subscribe(path []string, id string, ch chan model.Mutation) {
+func (ed *stdDispatcher) Subscribe(path []string, id string, ch chan command.Mutation) {
ed.tree.subscribe(path, id, ch)
}
@@ -29,6 +29,6 @@ func (ed *stdDispatcher) Unsubscribe(path []string, id string) {
ed.tree.unsubscribe(path, id)
}
-func (ed *stdDispatcher) Notify(path []string, event *model.Mutation) {
+func (ed *stdDispatcher) Notify(path []string, event *command.Mutation) {
ed.tree.notify(path, event)
}
diff --git a/internal/events/node.go b/internal/events/node.go
index a1d9c3e..62d45dd 100644
--- a/internal/events/node.go
+++ b/internal/events/node.go
@@ -3,17 +3,17 @@ package events
import (
"sync"
- "go.neonxp.dev/djson/internal/model"
+ "go.neonxp.dev/djson/internal/command"
)
type subscriberNode struct {
parent *subscriberNode
children map[string]*subscriberNode
- channels map[string]chan model.Mutation
+ channels map[string]chan command.Mutation
mu sync.RWMutex
}
-func (sn *subscriberNode) subscribe(path []string, id string, ch chan model.Mutation) {
+func (sn *subscriberNode) subscribe(path []string, id string, ch chan command.Mutation) {
sn.mu.Lock()
defer sn.mu.Unlock()
if len(path) == 0 {
@@ -26,7 +26,7 @@ func (sn *subscriberNode) subscribe(path []string, id string, ch chan model.Muta
child = &subscriberNode{
parent: sn,
children: make(map[string]*subscriberNode),
- channels: make(map[string]chan model.Mutation),
+ channels: make(map[string]chan command.Mutation),
}
sn.children[head] = child
}
@@ -51,11 +51,11 @@ func (sn *subscriberNode) unsubscribe(path []string, id string) {
}
}
-func (sn *subscriberNode) notify(path []string, event *model.Mutation) {
+func (sn *subscriberNode) notify(path []string, event *command.Mutation) {
sn.mu.RLock()
defer sn.mu.RUnlock()
for _, ch := range sn.channels {
- go func(ch chan model.Mutation) {
+ go func(ch chan command.Mutation) {
ch <- *event
}(ch)
}
diff --git a/internal/handler/auth.go b/internal/handler/auth.go
deleted file mode 100644
index c66eeb7..0000000
--- a/internal/handler/auth.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package handler
-
-import (
- "fmt"
-
- "github.com/go-chi/jwtauth/v5"
-)
-
-var tokenAuth *jwtauth.JWTAuth
-
-func init() {
- tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
- _, tokenString, _ := tokenAuth.Encode(map[string]interface{}{"user_id": 123})
- fmt.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
-}
diff --git a/internal/handler/contract.go b/internal/handler/contract.go
index 8887be8..38280cf 100644
--- a/internal/handler/contract.go
+++ b/internal/handler/contract.go
@@ -1,13 +1,13 @@
package handler
import (
- "net/http"
+ "context"
- "github.com/go-chi/chi/v5"
+ "go.neonxp.dev/djson/internal/command"
+ "go.neonxp.dev/json"
)
-type Handler interface {
- HandleCRUD(r chi.Router)
- HandleEvents(r chi.Router)
- Hearthbeat(w http.ResponseWriter, r *http.Request)
+type Core interface {
+ Apply(ctx context.Context, mutation *command.Mutation) error
+ Query(ctx context.Context, query []string) (json.Node, error)
}
diff --git a/internal/handler/crud.go b/internal/handler/crud.go
index 4ab0c7f..b12f68a 100644
--- a/internal/handler/crud.go
+++ b/internal/handler/crud.go
@@ -1,37 +1,35 @@
package handler
import (
+ "fmt"
"io"
"net/http"
- "time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
- "go.neonxp.dev/djson/internal/model"
- "go.neonxp.dev/json"
+ "go.neonxp.dev/djson/internal/command"
+ "go.neonxp.dev/objectid"
)
-func (h *handler) HandleCRUD(r chi.Router) {
- r.Use(middleware.CleanPath)
- r.Use(middleware.StripSlashes)
+func (h *handler) HandleCRUD(router chi.Router) {
+ router.Use(middleware.CleanPath)
+ router.Use(middleware.StripSlashes)
- r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
- res, err := h.core.Get(parsePath(rctx.RoutePath))
- if err != nil {
- writeError(http.StatusNotFound, err, w)
- return
- }
- result, err := res.MarshalJSON()
+ path := parsePath(rctx.RoutePath)
+
+ node, err := h.core.Query(r.Context(), path)
if err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
w.WriteHeader(http.StatusOK)
- _, _ = w.Write(result)
+ _, _ = w.Write([]byte(node.String()))
})
- r.Post("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Post("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
jsonBody, err := io.ReadAll(r.Body)
@@ -40,25 +38,23 @@ func (h *handler) HandleCRUD(r chi.Router) {
return
}
r.Body.Close()
- node, err := json.Unmarshal(jsonBody)
- if err != nil {
- writeError(http.StatusBadRequest, err, w)
- return
- }
- mutation := &model.Mutation{
- Date: time.Now(),
- Type: model.Create,
+
+ mutation := command.Mutation{
+ ID: objectid.New(),
+ Type: command.Create,
Path: path,
- Body: node,
+ Data: string(jsonBody),
}
- if err := h.core.Mutate(r.Context(), mutation); err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ if err := h.core.Apply(r.Context(), &mutation); err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
+
w.WriteHeader(http.StatusCreated)
})
- r.Patch("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Patch("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
jsonBody, err := io.ReadAll(r.Body)
@@ -67,37 +63,37 @@ func (h *handler) HandleCRUD(r chi.Router) {
return
}
r.Body.Close()
- node, err := json.Unmarshal(jsonBody)
- if err != nil {
- writeError(http.StatusBadRequest, err, w)
- return
- }
- mutation := &model.Mutation{
- Date: time.Now(),
- Type: model.Merge,
+
+ mutation := command.Mutation{
+ ID: objectid.New(),
+ Type: command.Merge,
Path: path,
- Body: node,
+ Data: string(jsonBody),
}
- if err := h.core.Mutate(r.Context(), mutation); err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ if err := h.core.Apply(r.Context(), &mutation); err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
+
w.WriteHeader(http.StatusOK)
})
- r.Delete("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Delete("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
- mutation := &model.Mutation{
- Date: time.Now(),
- Type: model.Remove,
+
+ mutation := command.Mutation{
+ ID: objectid.New(),
+ Type: command.Remove,
Path: path,
- Body: nil,
}
- if err := h.core.Mutate(r.Context(), mutation); err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ if err := h.core.Apply(r.Context(), &mutation); err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
+
w.WriteHeader(http.StatusNoContent)
})
}
diff --git a/internal/handler/events.go b/internal/handler/events.go
index 299971d..43de3c4 100644
--- a/internal/handler/events.go
+++ b/internal/handler/events.go
@@ -1,20 +1,22 @@
package handler
import (
+ "encoding/json"
"fmt"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
- dmodel "go.neonxp.dev/djson/internal/model"
+
+ "go.neonxp.dev/djson/internal/command"
)
func (h *handler) HandleEvents(r chi.Router) {
r.Route("/sse", func(r chi.Router) {
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
- h.receiveEvents(w, r, func(ev *dmodel.Mutation) {
- message, _ := ev.Body.MarshalJSON()
+ h.receiveEvents(w, r, func(ev *command.Mutation) {
+ message, _ := json.Marshal(ev.Data)
fmt.Fprintf(
w,
`event: %s\ndata: {"path":"%s","data":%s}\n\n`,
@@ -27,8 +29,8 @@ func (h *handler) HandleEvents(r chi.Router) {
})
r.Route("/json", func(r chi.Router) {
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
- h.receiveEvents(w, r, func(ev *dmodel.Mutation) {
- message, _ := ev.Body.MarshalJSON()
+ h.receiveEvents(w, r, func(ev *command.Mutation) {
+ message, _ := json.Marshal(ev.Data)
fmt.Fprintf(
w,
`{"event":"%s","path":"%s","data":"%s"}\n`,
@@ -44,7 +46,7 @@ func (h *handler) HandleEvents(r chi.Router) {
func (h *handler) receiveEvents(
w http.ResponseWriter,
r *http.Request,
- render func(ev *dmodel.Mutation),
+ render func(ev *command.Mutation),
) {
flusher := w.(http.Flusher)
w.Header().Set("Content-Type", "application/x-ndjson")
@@ -53,7 +55,7 @@ func (h *handler) receiveEvents(
w.Header().Set("Access-Control-Allow-Origin", "*")
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
- ch := make(chan dmodel.Mutation)
+ ch := make(chan command.Mutation)
reqID := middleware.GetReqID(r.Context())
h.events.Subscribe(path, reqID, ch)
defer h.events.Unsubscribe(path, reqID)
diff --git a/internal/handler/handler.go b/internal/handler/handler.go
index 11c1936..bd8a2c1 100644
--- a/internal/handler/handler.go
+++ b/internal/handler/handler.go
@@ -4,14 +4,10 @@ import (
"net/http"
"strings"
- "go.neonxp.dev/json"
- jsonModel "go.neonxp.dev/json/model"
-
"go.neonxp.dev/djson/internal/events"
- "go.neonxp.dev/djson/internal/tree"
)
-func New(core tree.Core, eventsDispatcher events.Dispatcher) Handler {
+func New(core Core, eventsDispatcher events.Dispatcher) *handler {
return &handler{
core: core,
events: eventsDispatcher,
@@ -19,13 +15,12 @@ func New(core tree.Core, eventsDispatcher events.Dispatcher) Handler {
}
type handler struct {
- core tree.Core
+ core Core
events events.Dispatcher
}
func writeError(code int, err error, w http.ResponseWriter) {
- jsonErr, _ := json.Marshal(jsonModel.NewNode(err.Error()))
- _, _ = w.Write(jsonErr)
+ _, _ = w.Write([]byte(err.Error()))
}
func parsePath(nodePath string) []string {
diff --git a/internal/handler/hearthbeat.go b/internal/handler/hearthbeat.go
index 014880e..22f6c3c 100644
--- a/internal/handler/hearthbeat.go
+++ b/internal/handler/hearthbeat.go
@@ -2,21 +2,10 @@ package handler
import (
"net/http"
-
- "go.neonxp.dev/djson/internal/tree"
)
func (h *handler) Hearthbeat(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
- switch h.core.State() {
- case tree.Ready:
- w.WriteHeader(http.StatusOK)
- _, _ = w.Write([]byte("."))
- case tree.Failed:
- w.WriteHeader(http.StatusInternalServerError)
- _, _ = w.Write([]byte("start failed"))
- case tree.Running:
- w.WriteHeader(http.StatusServiceUnavailable)
- _, _ = w.Write([]byte("starting..."))
- }
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte("."))
}
diff --git a/internal/logger/logger.go b/internal/logger/logger.go
new file mode 100644
index 0000000..bd90308
--- /dev/null
+++ b/internal/logger/logger.go
@@ -0,0 +1,27 @@
+package logger
+
+import (
+ "fmt"
+
+ "github.com/rs/zerolog"
+)
+
+type Logger struct {
+ zerolog.Logger
+}
+
+func (l Logger) Errorf(msg string, args ...interface{}) {
+ l.Error().Msg(fmt.Sprintf(msg, args...))
+}
+
+func (l Logger) Warningf(msg string, args ...interface{}) {
+ l.Warn().Msg(fmt.Sprintf(msg, args...))
+}
+
+func (l Logger) Infof(msg string, args ...interface{}) {
+ l.Info().Msg(fmt.Sprintf(msg, args...))
+}
+
+func (l Logger) Debugf(msg string, args ...interface{}) {
+ l.Debug().Msg(fmt.Sprintf(msg, args...))
+}
diff --git a/internal/model/mutations.go b/internal/model/mutations.go
deleted file mode 100644
index 6463823..0000000
--- a/internal/model/mutations.go
+++ /dev/null
@@ -1,36 +0,0 @@
-package model
-
-import (
- "fmt"
- "strings"
- "time"
-
- "go.neonxp.dev/json/model"
-)
-
-type Mutation struct {
- Date time.Time
- Type CommandType
- Path []string
- Body model.Node
-}
-
-func (m *Mutation) String() string {
- body, _ := m.Body.MarshalJSON()
- return fmt.Sprintf(
- "Date=%s Type=%s Path='%s' Body=%s",
- m.Date.Format(time.RFC3339),
- m.Type,
- strings.Join(m.Path, "/"),
- string(body),
- )
-}
-
-//go:generate stringer -type=CommandType
-type CommandType int
-
-const (
- Create CommandType = iota
- Merge
- Remove
-)
diff --git a/internal/node/acl.go b/internal/node/acl.go
new file mode 100644
index 0000000..ca2e3dc
--- /dev/null
+++ b/internal/node/acl.go
@@ -0,0 +1,12 @@
+package node
+
+type ACL struct {
+ Read []Actor
+ Write []Actor
+ Delete []Actor
+}
+
+type Actor struct {
+ UserID string
+ Group string
+}
diff --git a/internal/node/array.go b/internal/node/array.go
new file mode 100644
index 0000000..780f99f
--- /dev/null
+++ b/internal/node/array.go
@@ -0,0 +1,23 @@
+package node
+
+import "go.neonxp.dev/json"
+
+func (n *Node) Append(v json.Node) {
+ n.arrayValue = append(n.arrayValue, v.(*Node))
+}
+
+func (n *Node) Index(i int) json.Node {
+ return n.arrayValue[i]
+}
+
+func (n *Node) SetByIndex(i int, v *Node) {
+ n.arrayValue[i] = v
+}
+
+func (n *Node) RemoveByIndex(i int) {
+ n.arrayValue = append(n.arrayValue[:i], n.arrayValue[i:]...)
+}
+
+func (n *Node) Len() int {
+ return len(n.arrayValue)
+}
diff --git a/internal/node/bool.go b/internal/node/bool.go
new file mode 100644
index 0000000..91edaf9
--- /dev/null
+++ b/internal/node/bool.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetBool(v bool) {
+ n.boolValue = v
+}
+
+func (n *Node) GetBool() bool {
+ return n.boolValue
+}
diff --git a/internal/node/node.go b/internal/node/node.go
new file mode 100644
index 0000000..b8bec52
--- /dev/null
+++ b/internal/node/node.go
@@ -0,0 +1,71 @@
+package node
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+
+ "go.neonxp.dev/json"
+ "go.neonxp.dev/objectid"
+)
+
+func Factory(typ json.NodeType) (json.Node, error) {
+ n := &Node{
+ ID: objectid.New(),
+ Type: typ,
+ }
+ switch typ {
+ case json.ObjectType:
+ n.objectValue = make(map[string]*Node)
+ case json.ArrayType:
+ n.arrayValue = make([]*Node, 0)
+ }
+ return n, nil
+}
+
+type Node struct {
+ ID objectid.ID
+ Type json.NodeType
+ Parent objectid.ID
+ stringValue string
+ numberValue float64
+ boolValue bool
+ objectValue map[string]*Node
+ arrayValue []*Node
+ ACL ACL
+}
+
+func (n *Node) String() string {
+ switch n.Type {
+ case json.NullType:
+ return "null"
+ case json.StringType:
+ return `"` + n.stringValue + `"`
+ case json.NumberType:
+ return strconv.FormatFloat(n.numberValue, 'g', 15, 64)
+ case json.BooleanType:
+ if n.boolValue {
+ return "true"
+ }
+ return "false"
+ case json.ObjectType:
+ res := make([]string, 0, len(n.objectValue))
+ for k, n := range n.objectValue {
+ res = append(res, fmt.Sprintf(`"%s":%s`, k, n.String()))
+ }
+ return fmt.Sprintf(`{%s}`, strings.Join(res, ","))
+ case json.ArrayType:
+ res := make([]string, 0, len(n.arrayValue))
+ for _, v := range n.arrayValue {
+ res = append(res, v.String())
+ }
+ return fmt.Sprintf(`[%s]`, strings.Join(res, ","))
+ }
+ return ""
+}
+
+func (n *Node) SetParent(v json.Node) {
+ if v != nil {
+ n.Parent = v.(*Node).ID
+ }
+}
diff --git a/internal/node/number.go b/internal/node/number.go
new file mode 100644
index 0000000..055ae00
--- /dev/null
+++ b/internal/node/number.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetNumber(v float64) {
+ n.numberValue = v
+}
+
+func (n *Node) GetNumber() float64 {
+ return n.numberValue
+}
diff --git a/internal/node/object.go b/internal/node/object.go
new file mode 100644
index 0000000..35a3141
--- /dev/null
+++ b/internal/node/object.go
@@ -0,0 +1,22 @@
+package node
+
+import "go.neonxp.dev/json"
+
+func (n *Node) SetKeyValue(k string, v json.Node) {
+ n.objectValue[k] = v.(*Node)
+}
+
+func (n *Node) GetByKey(k string) (json.Node, bool) {
+ node, ok := n.objectValue[k]
+ return node, ok
+}
+
+func (n *Node) Merge(n2 *Node) {
+ for k, v := range n2.objectValue {
+ n.objectValue[k] = v
+ }
+}
+
+func (n *Node) RemoveByKey(k string) {
+ delete(n.objectValue, k)
+}
diff --git a/internal/node/string.go b/internal/node/string.go
new file mode 100644
index 0000000..90de4a7
--- /dev/null
+++ b/internal/node/string.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetString(v string) {
+ n.stringValue = v
+}
+
+func (n *Node) GetString() string {
+ return n.stringValue
+}
diff --git a/internal/storage/contract.go b/internal/storage/contract.go
deleted file mode 100644
index 5847bcc..0000000
--- a/internal/storage/contract.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package storage
-
-import (
- "context"
- "io"
-
- "go.neonxp.dev/djson/internal/model"
-)
-
-type Storage interface {
- io.Closer
- Commit(ctx context.Context, mut model.Mutation) error
- Load() chan model.Mutation
-}
diff --git a/internal/storage/file.go b/internal/storage/file.go
deleted file mode 100644
index 885d5ef..0000000
--- a/internal/storage/file.go
+++ /dev/null
@@ -1,79 +0,0 @@
-package storage
-
-import (
- "context"
- "encoding/gob"
- "fmt"
- "io"
- "os"
-
- "github.com/rs/zerolog"
- "go.neonxp.dev/djson/internal/model"
- jsonModel "go.neonxp.dev/json/model"
-)
-
-func init() {
- gob.Register(new(jsonModel.ArrayNode))
- gob.Register(new(jsonModel.ObjectNode))
- gob.Register(new(jsonModel.StringNode))
- gob.Register(new(jsonModel.BooleanNode))
- gob.Register(new(jsonModel.NullNode))
- gob.Register(new(jsonModel.NumberNode))
-}
-
-type fsStorage struct {
- logger zerolog.Logger
- enc *gob.Encoder
- dec *gob.Decoder
- fh *os.File
- fileName string
- mutationsLog []model.Mutation
-}
-
-func New(fileName string, logger zerolog.Logger) (Storage, error) {
- logger.Info().Str("path", fileName).Msg("loading db")
- fh, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
- if err != nil {
- return nil, err
- }
-
- return &fsStorage{
- logger: logger,
- fileName: fileName,
- fh: fh,
- enc: gob.NewEncoder(fh),
- dec: gob.NewDecoder(fh),
- mutationsLog: []model.Mutation{},
- }, nil
-}
-
-func (fs *fsStorage) Commit(ctx context.Context, mut model.Mutation) error {
- if fs.enc == nil {
- return fmt.Errorf("file storage not initiated")
- }
- return fs.enc.Encode(mut)
-}
-
-func (fs *fsStorage) Load() chan model.Mutation {
- ch := make(chan model.Mutation)
- go func() {
- for {
- m := model.Mutation{}
- if err := fs.dec.Decode(&m); err != nil {
- if err != io.EOF {
- fs.logger.Err(err)
- }
- close(ch)
- return
- }
- fs.logger.Debug().RawJSON("json", []byte(m.String())).Msg("loaded mutation")
- fs.mutationsLog = append(fs.mutationsLog, m)
- ch <- m
- }
- }()
- return ch
-}
-
-func (fs *fsStorage) Close() error {
- return fs.fh.Close()
-}
diff --git a/internal/tree/contract.go b/internal/tree/contract.go
deleted file mode 100644
index 29748cb..0000000
--- a/internal/tree/contract.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package tree
-
-import (
- "context"
-
- dmodel "go.neonxp.dev/djson/internal/model"
- "go.neonxp.dev/json/model"
-)
-
-type Core interface {
- Init() error
- Get(nodes []string) (model.Node, error)
- Mutate(ctx context.Context, mut *dmodel.Mutation) error
- State() CoreState
-}
-
-type CoreState int
-
-const (
- Running CoreState = iota
- Ready
- Failed
-)
diff --git a/internal/tree/core.go b/internal/tree/core.go
deleted file mode 100644
index d2cace3..0000000
--- a/internal/tree/core.go
+++ /dev/null
@@ -1,50 +0,0 @@
-package tree
-
-import (
- "sync"
-
- "go.neonxp.dev/djson/internal/events"
- "go.neonxp.dev/djson/internal/storage"
- "go.neonxp.dev/json/model"
-)
-
-type stdCore struct {
- Root model.ObjectNode
- state CoreState
- mu sync.RWMutex
- storage storage.Storage
- eventDispatcher events.Dispatcher
-}
-
-func New(storage storage.Storage, eventsDispatcher events.Dispatcher) Core {
- return &stdCore{
- Root: model.ObjectNode{},
- state: Running,
- mu: sync.RWMutex{},
- storage: storage,
- eventDispatcher: eventsDispatcher,
- }
-}
-
-func (t *stdCore) Init() error {
- // Load initial mutations
- for m := range t.storage.Load() {
- if err := t.execute(&m); err != nil {
- t.state = Failed
- return err
- }
- }
- t.state = Ready
- return nil
-}
-
-func (t *stdCore) Get(nodes []string) (model.Node, error) {
- if len(nodes) == 0 {
- return &t.Root, nil
- }
- return model.Query(&t.Root, nodes)
-}
-
-func (t *stdCore) State() CoreState {
- return t.state
-}
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
-}