doc.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package namespace is a clientv3 wrapper that translates all keys to begin
  15. // with a given prefix.
  16. //
  17. // First, create a client:
  18. //
  19. // cli, err := clientv3.New(clientv3.Config{Endpoints: []string{"localhost:2379"}})
  20. // if err != nil {
  21. // // handle error!
  22. // }
  23. //
  24. // Next, override the client interfaces:
  25. //
  26. // unprefixedKV := cli.KV
  27. // cli.KV = namespace.NewKV(cli.KV, "my-prefix/")
  28. // cli.Watcher = namespace.NewWatcher(cli.Watcher, "my-prefix/")
  29. // cli.Lease = namespace.NewLease(cli.Lease, "my-prefix/")
  30. //
  31. // Now calls using 'cli' will namespace / prefix all keys with "my-prefix/":
  32. //
  33. // cli.Put(context.TODO(), "abc", "123")
  34. // resp, _ := unprefixedKV.Get(context.TODO(), "my-prefix/abc")
  35. // fmt.Printf("%s\n", resp.Kvs[0].Value)
  36. // // Output: 123
  37. // unprefixedKV.Put(context.TODO(), "my-prefix/abc", "456")
  38. // resp, _ = cli.Get("abc")
  39. // fmt.Printf("%s\n", resp.Kvs[0].Value)
  40. // // Output: 456
  41. //
  42. package namespace