go-micro.dev/v5@v5.12.0/store/nats-js-kv/keys.go (about)

     1  package natsjskv
     2  
     3  import (
     4  	"encoding/base32"
     5  	"strings"
     6  )
     7  
     8  // NatsKey is a convenience function to create a key for the nats kv store.
     9  func (n *natsStore) NatsKey(table, microkey string) string {
    10  	return n.NewKey(table, microkey, "").NatsKey()
    11  }
    12  
    13  // MicroKey is a convenience function to create a key for the micro interface.
    14  func (n *natsStore) MicroKey(table, natskey string) string {
    15  	return n.NewKey(table, "", natskey).MicroKey()
    16  }
    17  
    18  // MicroKeyFilter is a convenience function to create a key for the micro interface.
    19  // It returns false if the key does not match the table, prefix or suffix.
    20  func (n *natsStore) MicroKeyFilter(table, natskey string, prefix, suffix string) (string, bool) {
    21  	k := n.NewKey(table, "", natskey)
    22  	return k.MicroKey(), k.Check(table, prefix, suffix)
    23  }
    24  
    25  // Key represents a key in the store.
    26  // They are used to convert nats keys (base32 encoded) to micro keys (plain text - no table prefix) and vice versa.
    27  type Key struct {
    28  	// Plain is the plain key as requested by the go-micro interface.
    29  	Plain string
    30  	// Full is the full key including the table prefix.
    31  	Full string
    32  	// Encoded is the base64 encoded key as used by the nats kv store.
    33  	Encoded string
    34  }
    35  
    36  // NewKey creates a new key. Either plain or encoded must be set.
    37  func (n *natsStore) NewKey(table string, plain, encoded string) *Key {
    38  	k := &Key{
    39  		Plain:   plain,
    40  		Encoded: encoded,
    41  	}
    42  
    43  	switch {
    44  	case k.Plain != "":
    45  		k.Full = getKey(k.Plain, table)
    46  		k.Encoded = encode(k.Full, n.encoding)
    47  	case k.Encoded != "":
    48  		k.Full = decode(k.Encoded, n.encoding)
    49  		k.Plain = trimKey(k.Full, table)
    50  	}
    51  
    52  	return k
    53  }
    54  
    55  // NatsKey returns a key the nats kv store can work with.
    56  func (k *Key) NatsKey() string {
    57  	return k.Encoded
    58  }
    59  
    60  // MicroKey returns a key the micro interface can work with.
    61  func (k *Key) MicroKey() string {
    62  	return k.Plain
    63  }
    64  
    65  // Check returns false if the key does not match the table, prefix or suffix.
    66  func (k *Key) Check(table, prefix, suffix string) bool {
    67  	if table != "" && k.Full != getKey(k.Plain, table) {
    68  		return false
    69  	}
    70  
    71  	if prefix != "" && !strings.HasPrefix(k.Plain, prefix) {
    72  		return false
    73  	}
    74  
    75  	if suffix != "" && !strings.HasSuffix(k.Plain, suffix) {
    76  		return false
    77  	}
    78  
    79  	return true
    80  }
    81  
    82  func encode(s string, alg string) string {
    83  	switch alg {
    84  	case "base32":
    85  		return base32.StdEncoding.EncodeToString([]byte(s))
    86  	default:
    87  		return s
    88  	}
    89  }
    90  
    91  func decode(s string, alg string) string {
    92  	switch alg {
    93  	case "base32":
    94  		b, err := base32.StdEncoding.DecodeString(s)
    95  		if err != nil {
    96  			return s
    97  		}
    98  
    99  		return string(b)
   100  	default:
   101  		return s
   102  	}
   103  }
   104  
   105  func getKey(key, table string) string {
   106  	if table != "" {
   107  		return table + "_" + key
   108  	}
   109  
   110  	return key
   111  }
   112  
   113  func trimKey(key, table string) string {
   114  	if table != "" {
   115  		return strings.TrimPrefix(key, table+"_")
   116  	}
   117  
   118  	return key
   119  }