Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:
	watchRevision, putReturnTime, clientPutCount, persistedPutCount map[keyValue]int64,
	clientDeleteCount, persistedDeleteCount map[string]int64,
	clientCompactCount, persistedCompactCount map[int64]int64,
	deleteReturnTime map[string]int64,
	compactReturnTime map[int64]int64,
) []porcupine.Operation {
	newOperations := make([]porcupine.Operation, 0, len(operations))

	for _, op := range operations {
		request := op.Input.(model.EtcdRequest)
		resp := op.Output.(model.MaybeEtcdResponse)
		if resp.Error == "" || request.Type != model.Txn {
			// Cannot patch those requests.
			newOperations = append(newOperations, op)
			continue
		}
		var txnRevision int64
		var persisted bool
		for _, etcdOp := range append(request.Txn.OperationsOnSuccess, request.Txn.OperationsOnFailure...) {
			switch etcdOp.Type {
			case model.PutOperation:
				kv := keyValue{Key: etcdOp.Put.Key, Value: etcdOp.Put.Value}
				if _, ok := persistedPutCount[kv]; ok {
					persisted = true
				}
				if count := clientPutCount[kv]; count != 1 {
					continue
				}
				if revision, ok := watchRevision[kv]; ok {
					txnRevision = revision
				}
				if returnTime, ok := putReturnTime[kv]; ok {
					op.Return = min(op.Return, returnTime)
				}
			case model.DeleteOperation:
				key := etcdOp.Delete.Key
				if _, ok := persistedDeleteCount[key]; ok {
					persisted = true
				}
				if count := clientDeleteCount[key]; count != 1 {
					continue
				}
				if returnTime, ok := deleteReturnTime[key]; ok {
					op.Return = min(op.Return, returnTime)
				}
			case model.CompactOperation:
				rev := etcdOp.Compact.Revision
				if _, ok := persistedCompactCount[rev]; ok {
					persisted = true
				}
				if count := clientCompactCount[rev]; count != 1 {

Review comment (suggestion):
There is a lot of arguments with that and easy to mix up. Have you considered separating Put and Delete by type?

For example instead of
```
putReturnTime, clientPutCount, persistedPutCount map[keyValue]int64,
deleteReturnTime, clientDeleteCount, persistedDeleteCount map[string]int64, 
```

Have
```
putReturnTime, clientPutCount, persistedPutCount map[PutOptions]int64,
deleteReturnTime, clientDeleteCount, persistedDeleteCount map[DeleteOptions]int64, 
```

And instead of 
```
putReturnTime, clientPutCount, persistedPutCount map[PutOptions]int64,
```
Have
```
type patchArgs struct{
  returnTime int64
  clientCount int64
  persistedCount int64
}

put map[PutOptions]patchArgs
```

Code after:
		for _, watch := range client.Watch {
			for _, resp := range watch.Responses {
				for _, event := range resp.Events {
					switch event.Type {
					case model.RangeOperation:
					case model.PutOperation:
						kv := model.PutOptions{Key: event.Key, Value: event.Value}
						putRevisions[kv] = event.Revision
					case model.DeleteOperation:
						kv := model.DeleteOptions{Key: event.Key} // ADD: Track delete revisions
						delRevisions[kv] = event.Revision
					default:
						panic(fmt.Sprintf("unknown event type %q", event.Type))
					}
				}
			}
		}
	}
	return putRevisions, delRevisions
}

func patchOperations(
	operations []porcupine.Operation,
	putArgs map[model.PutOptions]patchArgs,
	delArgs map[model.DeleteOptions]patchArgs,
) []porcupine.Operation {
	newOperations := make([]porcupine.Operation, 0, len(operations))

	for _, op := range operations {
		request := op.Input.(model.EtcdRequest)
		resp := op.Output.(model.MaybeEtcdResponse)
		if resp.Error == "" || request.Type != model.Txn {
			// Cannot patch those requests.
			newOperations = append(newOperations, op)
			continue
		}
		var txnRevision int64
		var persisted bool
		for _, etcdOp := range append(request.Txn.OperationsOnSuccess, request.Txn.OperationsOnFailure...) {
			switch etcdOp.Type {
			case model.PutOperation:
				kv := model.PutOptions{Key: etcdOp.Put.Key, Value: etcdOp.Put.Value}
				arg, ok := putArgs[kv]
				if !ok {
					continue
				}
				if arg.persistedCount > 0 {
					persisted = true
				}
				if arg.clientCount != 1 {
					continue