go.etcd.io/etcd@v3.3.27+incompatible/clientv3/namespace/doc.go (about)

     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  
    15  // Package namespace is a clientv3 wrapper that translates all keys to begin
    16  // with a given prefix.
    17  //
    18  // First, create a client:
    19  //
    20  //	cli, err := clientv3.New(clientv3.Config{Endpoints: []string{"localhost:2379"}})
    21  //	if err != nil {
    22  //		// handle error!
    23  //	}
    24  //
    25  // Next, override the client interfaces:
    26  //
    27  //	unprefixedKV := cli.KV
    28  //	cli.KV = namespace.NewKV(cli.KV, "my-prefix/")
    29  //	cli.Watcher = namespace.NewWatcher(cli.Watcher, "my-prefix/")
    30  //	cli.Lease = namespace.NewLease(cli.Lease, "my-prefix/")
    31  //
    32  // Now calls using 'cli' will namespace / prefix all keys with "my-prefix/":
    33  //
    34  //	cli.Put(context.TODO(), "abc", "123")
    35  //	resp, _ := unprefixedKV.Get(context.TODO(), "my-prefix/abc")
    36  //	fmt.Printf("%s\n", resp.Kvs[0].Value)
    37  //	// Output: 123
    38  //	unprefixedKV.Put(context.TODO(), "my-prefix/abc", "456")
    39  //	resp, _ = cli.Get("abc")
    40  //	fmt.Printf("%s\n", resp.Kvs[0].Value)
    41  //	// Output: 456
    42  //
    43  package namespace