Create will add the key/value pair if it does not exist.
(ctx context.Context, key string, value []byte, opts ...KVCreateOpt)
| 1057 | |
| 1058 | // Create will add the key/value pair if it does not exist. |
| 1059 | func (kv *kvs) Create(ctx context.Context, key string, value []byte, opts ...KVCreateOpt) (revision uint64, err error) { |
| 1060 | var o createOpts |
| 1061 | for _, opt := range opts { |
| 1062 | if opt != nil { |
| 1063 | if err := opt.configureCreate(&o); err != nil { |
| 1064 | return 0, err |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | v, err := kv.updateRevision(ctx, key, value, 0, o.ttl) |
| 1070 | if err == nil { |
| 1071 | return v, nil |
| 1072 | } |
| 1073 | |
| 1074 | if e, err := kv.get(ctx, key, kvLatestRevision); errors.Is(err, ErrKeyDeleted) { |
| 1075 | return kv.updateRevision(ctx, key, value, e.Revision(), o.ttl) |
| 1076 | } |
| 1077 | |
| 1078 | // Check if the expected last subject sequence is not zero which implies |
| 1079 | // the key already exists. |
| 1080 | if errors.Is(err, ErrKeyExists) { |
| 1081 | jserr := ErrKeyExists.(*jsError) |
| 1082 | return 0, fmt.Errorf("%w: %s", err, jserr.message) |
| 1083 | } |
| 1084 | |
| 1085 | return 0, err |
| 1086 | } |
| 1087 | |
| 1088 | // Update will update the value if the latest revision matches. |
| 1089 | func (kv *kvs) Update(ctx context.Context, key string, value []byte, revision uint64) (uint64, error) { |
nothing calls this directly
no test coverage detected