From 03b90dc5ad2aed13420b83ebe13184b6ff89d7f2 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 18 Jan 2022 20:07:32 +0800 Subject: [PATCH] suport check auto client version --- ccore/nebula/alias.go | 11 ++++++----- ccore/nebula/client.go | 4 ++-- ccore/nebula/gateway/dao/dao.go | 4 ++-- ccore/nebula/gateway/examples/main.go | 3 +-- ccore/nebula/gateway/pool/pool.go | 4 ++-- ccore/nebula/options.go | 13 ++++++++++++- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/ccore/nebula/alias.go b/ccore/nebula/alias.go index d71d24a..97379c7 100644 --- a/ccore/nebula/alias.go +++ b/ccore/nebula/alias.go @@ -11,12 +11,13 @@ type ( ) var ( - VersionAuto = Version("auto") - Version2_5 = types.Version2_5 - Version2_6 = types.Version2_6 - Version3_0 = types.Version3_0 + versionAuto = Version("auto") - Versions = []Version{ + Version2_5 = types.Version2_5 + Version2_6 = types.Version2_6 + Version3_0 = types.Version3_0 + + supportedVersions = []Version{ Version3_0, Version2_6, Version2_5, diff --git a/ccore/nebula/client.go b/ccore/nebula/client.go index 059cbe4..0653254 100644 --- a/ccore/nebula/client.go +++ b/ccore/nebula/client.go @@ -81,7 +81,7 @@ func (c *defaultClient) Version() Version { } func (c *defaultClient) initDriver(checkFn func(types.Driver) error) error { - if c.o.version != VersionAuto { + if c.o.version != versionAuto { driver, err := types.GetDriver(c.o.version) if err != nil { return err @@ -93,7 +93,7 @@ func (c *defaultClient) initDriver(checkFn func(types.Driver) error) error { return nil } - for _, v := range Versions { + for _, v := range c.o.autoVersions { driver, err := types.GetDriver(v) if err != nil { return err diff --git a/ccore/nebula/gateway/dao/dao.go b/ccore/nebula/gateway/dao/dao.go index ecd133e..b461dfb 100644 --- a/ccore/nebula/gateway/dao/dao.go +++ b/ccore/nebula/gateway/dao/dao.go @@ -276,8 +276,8 @@ func getMapInfo(valWarp *wrapper.ValueWrapper, _verticesParsedList *list, _edges } // Connect return if the nebula connect succeed -func Connect(address string, port int, username string, password string, version nebula.Version) (nsid string, err error) { - nsid, err = pool.NewClient(address, port, username, password, version) +func Connect(address string, port int, username string, password string, opts ...nebula.Option) (nsid string, err error) { + nsid, err = pool.NewClient(address, port, username, password, opts...) if err != nil { return "", err } diff --git a/ccore/nebula/gateway/examples/main.go b/ccore/nebula/gateway/examples/main.go index aa54b7f..7c5c8ed 100644 --- a/ccore/nebula/gateway/examples/main.go +++ b/ccore/nebula/gateway/examples/main.go @@ -3,7 +3,6 @@ package main import ( "log" - "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula" "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/gateway/dao" ) @@ -15,7 +14,7 @@ func main() { password = "123" ) - nsid, err := dao.Connect(address, port, username, password, nebula.VersionAuto) + nsid, err := dao.Connect(address, port, username, password) if err != nil { log.Println("error: ", err) } diff --git a/ccore/nebula/gateway/pool/pool.go b/ccore/nebula/gateway/pool/pool.go index 01ba0b0..6443944 100644 --- a/ccore/nebula/gateway/pool/pool.go +++ b/ccore/nebula/gateway/pool/pool.go @@ -202,12 +202,12 @@ func ListParams(args string, tmpParameter types.ParameterMap, sessionMap types.P return nil } -func NewClient(address string, port int, username string, password string, version nebula.Version) (ncid string, err error) { +func NewClient(address string, port int, username string, password string, opts ...nebula.Option) (ncid string, err error) { clientMux.Lock() defer clientMux.Unlock() host := strings.Join([]string{address, strconv.Itoa(port)}, ":") - c, err := nebula.NewGraphClient([]string{host}, username, password, nebula.WithVersion(version)) + c, err := nebula.NewGraphClient([]string{host}, username, password, opts...) if err != nil { return "", err } diff --git a/ccore/nebula/options.go b/ccore/nebula/options.go index a909b41..b1687a8 100644 --- a/ccore/nebula/options.go +++ b/ccore/nebula/options.go @@ -15,6 +15,7 @@ const ( type ( Options struct { version Version + autoVersions []Version log Logger graph socketOptions meta socketOptions @@ -37,6 +38,15 @@ func WithVersion(version Version) Option { } } +func WithAutoVersions(autoVersions ...Version) Option { + return func(o *Options) { + o.version = versionAuto + if len(autoVersions) > 0 { + o.autoVersions = autoVersions + } + } +} + func WithLogger(log Logger) Option { return func(o *Options) { o.log = log @@ -177,7 +187,8 @@ func (o *socketOptions) complete() { func defaultOptions() Options { return Options{ - version: VersionAuto, + version: versionAuto, + autoVersions: supportedVersions, log: noOpLogger{}, graph: defaultSocketOptions(), meta: defaultSocketOptions(),