From d50a0e853c424ca9d6cfb368f617ebdb55705139 Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Mon, 13 Aug 2018 12:04:22 -0500 Subject: [PATCH] change NewTranslateStore() to take an interface for backward compatibility --- holder.go | 2 +- http/translator.go | 32 ++++++++++++++++++++++++-------- server.go | 2 +- server/server.go | 2 +- translate.go | 2 +- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/holder.go b/holder.go index 89b4dc368..d8d3fea81 100644 --- a/holder.go +++ b/holder.go @@ -48,7 +48,7 @@ type Holder struct { // Key/ID translation translateFile *TranslateFile - NewPrimaryTranslateStore func(*Node) TranslateStore + NewPrimaryTranslateStore func(interface{}) TranslateStore // opened channel is closed once Open() completes. opened chan struct{} diff --git a/http/translator.go b/http/translator.go index 715493c6b..7ca85a33f 100644 --- a/http/translator.go +++ b/http/translator.go @@ -6,11 +6,13 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "net/url" "strconv" "github.com/pilosa/pilosa" + "github.com/pkg/errors" ) // Ensure implementation implements inteface. @@ -22,14 +24,28 @@ type translateStore struct { node *pilosa.Node } -// DEPRECATED: NewTranslateStore returns a new instance of translateStore. -func NewTranslateStore(rawurl string) *translateStore { // nolint: unparam - return &translateStore{node: nil} -} - -// NewNodeTranslateStore returns a new instance of TranslateStore based on node. -func NewNodeTranslateStore(node *pilosa.Node) pilosa.TranslateStore { - return &translateStore{node: node} +// NewTranslateStore returns a new instance of TranslateStore based on node. +// DEPRECATED: Providing a string url to this function is being deprecated. Instead, +// provide a *pilosa.Node. +func NewTranslateStore(node interface{}) pilosa.TranslateStore { + var n *pilosa.Node + switch v := node.(type) { + case string: + log.Printf("WARNING: providing a string url to NewTranslateStore() has been deprecated.") + if uri, err := pilosa.NewURIFromAddress(v); err != nil { + log.Println(errors.Wrap(err, "creating uri")) + } else { + n = &pilosa.Node{ + ID: v, + URI: *uri, + } + } + case *pilosa.Node: + n = v + default: + log.Printf("WARNING: a *pilosa.Node is the only type supported by NewTranslateStore().") + } + return &translateStore{node: n} } // TranslateColumnsToUint64 is not currently implemented. diff --git a/server.go b/server.go index b4a704b80..076c181d4 100644 --- a/server.go +++ b/server.go @@ -168,7 +168,7 @@ func OptServerPrimaryTranslateStore(store TranslateStore) ServerOption { } } -func OptServerPrimaryTranslateStoreFunc(tf func(*Node) TranslateStore) ServerOption { +func OptServerPrimaryTranslateStoreFunc(tf func(interface{}) TranslateStore) ServerOption { return func(s *Server) error { s.holder.NewPrimaryTranslateStore = tf return nil diff --git a/server/server.go b/server/server.go index d10fa3ac2..4fdd711fe 100644 --- a/server/server.go +++ b/server/server.go @@ -277,7 +277,7 @@ func (m *Command) SetupServer() error { pilosa.OptServerStatsClient(statsClient), pilosa.OptServerURI(uri), pilosa.OptServerInternalClient(http.NewInternalClientFromURI(uri, c)), - pilosa.OptServerPrimaryTranslateStoreFunc(http.NewNodeTranslateStore), + pilosa.OptServerPrimaryTranslateStoreFunc(http.NewTranslateStore), pilosa.OptServerClusterDisabled(m.Config.Cluster.Disabled, m.Config.Cluster.Hosts), pilosa.OptServerSerializer(proto.Serializer{}), coordinatorOpt, diff --git a/translate.go b/translate.go index a37eb5b74..32bcc1126 100644 --- a/translate.go +++ b/translate.go @@ -1083,7 +1083,7 @@ var nopTStore TranslateStore = nopTranslateStore{} // newNopTranslateStore returns a translate store which does nothing. It returns a global // object to avoid unnecessary allocations. -func newNopTranslateStore(*Node) TranslateStore { return nopTStore } +func newNopTranslateStore(interface{}) TranslateStore { return nopTStore } // nopTranslateStore represents a no-op implementation of the TranslateStore interface. type nopTranslateStore struct{}