@@ -162,6 +162,7 @@ func (c *Consul) IsReady(ctx context.Context) bool {
162162}
163163
164164func (c * Consul ) Get (ctx context.Context , key string ) ([]byte , error ) {
165+ key = sanitizeKey (key )
165166 rsp , _ , err := c .client .KV ().Get (key , nil )
166167 if err != nil {
167168 return nil , err
@@ -173,6 +174,7 @@ func (c *Consul) Get(ctx context.Context, key string) ([]byte, error) {
173174}
174175
175176func (c * Consul ) Exists (ctx context.Context , key string ) (bool , error ) {
177+ key = sanitizeKey (key )
176178 _ , err := c .Get (ctx , key )
177179 if err != nil {
178180 if errors .Is (err , consts .ErrNotFound ) {
@@ -184,6 +186,7 @@ func (c *Consul) Exists(ctx context.Context, key string) (bool, error) {
184186}
185187
186188func (c * Consul ) Set (ctx context.Context , key string , value []byte ) error {
189+ key = sanitizeKey (key )
187190 kvPair := & api.KVPair {
188191 Key : key ,
189192 Value : value ,
@@ -193,11 +196,13 @@ func (c *Consul) Set(ctx context.Context, key string, value []byte) error {
193196}
194197
195198func (c * Consul ) Delete (ctx context.Context , key string ) error {
199+ key = sanitizeKey (key )
196200 _ , err := c .client .KV ().Delete (key , nil )
197201 return err
198202}
199203
200204func (c * Consul ) List (ctx context.Context , prefix string ) ([]engine.Entry , error ) {
205+ prefix = sanitizeKey (prefix )
201206 rsp , _ , err := c .client .KV ().List (prefix , nil )
202207 if err != nil {
203208 return nil , err
@@ -209,7 +214,7 @@ func (c *Consul) List(ctx context.Context, prefix string) ([]engine.Entry, error
209214 if string (kv .Key ) == prefix {
210215 continue
211216 }
212- key := strings .TrimLeft (string (kv .Key [prefixLen + 1 ]), "/" )
217+ key := strings .TrimLeft (string (kv .Key [prefixLen + 1 : ]), "/" )
213218 if strings .ContainsRune (key , '/' ) {
214219 continue
215220 }
@@ -236,7 +241,6 @@ func (c *Consul) electLoop() {
236241 TTL : fmt .Sprintf ("%v" , sessionTTL ),
237242 LockDelay : lockDelay ,
238243 }, nil )
239-
240244 if err != nil {
241245 logger .Get ().With (
242246 zap .Error (err ),
@@ -312,3 +316,10 @@ func (c *Consul) Close() error {
312316 c .client = nil
313317 return nil
314318}
319+
320+ func sanitizeKey (key string ) string {
321+ if len (key ) > 0 && key [0 ] == '/' {
322+ key = strings .TrimPrefix (key , "/" )
323+ }
324+ return key
325+ }
0 commit comments