Repository: etcd-io/etcd
File: tests/robustness/model/history.go (Go)

Code before:
	results := []EtcdOperationResult{}
	for _, resp := range resp.Responses {
		results = append(results, toEtcdOperationResult(resp))
	}
	h.appendSuccessful(request, start, end, txnResponse(results, resp.Succeeded, revision))
}

func (h *AppendableHistory) appendSuccessful(request EtcdRequest, start, end time.Duration, response MaybeEtcdResponse) {
	op := porcupine.Operation{
		ClientId: h.streamID,
		Input:    request,
		Call:     start.Nanoseconds(),
		Output:   response,
		Return:   end.Nanoseconds(),
	}
	h.append(op)
}

func toEtcdCondition(cmp clientv3.Cmp) (cond EtcdCondition) {
	switch {
	case cmp.Result == etcdserverpb.Compare_EQUAL && cmp.Target == etcdserverpb.Compare_MOD:
		cond.Key = string(cmp.KeyBytes())
		cond.ExpectedRevision = cmp.TargetUnion.(*etcdserverpb.Compare_ModRevision).ModRevision
	case cmp.Result == etcdserverpb.Compare_EQUAL && cmp.Target == etcdserverpb.Compare_CREATE:
		cond.Key = string(cmp.KeyBytes())
		cond.ExpectedRevision = cmp.TargetUnion.(*etcdserverpb.Compare_ModRevision).ModRevision
	case cmp.Result == etcdserverpb.Compare_EQUAL && cmp.Target == etcdserverpb.Compare_VERSION:
		cond.ExpectedVersion = cmp.TargetUnion.(*etcdserverpb.Compare_Version).Version
		cond.Key = string(cmp.KeyBytes())
	default:
		panic(fmt.Sprintf("Compare not supported, target: %q, result: %q", cmp.Target, cmp.Result))
	}
	return cond
}

func toEtcdOperation(option clientv3.Op) (op EtcdOperation) {
	switch {
	case option.IsGet():
		op.Type = RangeOperation
		op.Range = RangeOptions{
			Start: string(option.KeyBytes()),
			End:   string(option.RangeBytes()),
		}
	case option.IsPut():
		op.Type = PutOperation
		op.Put = PutOptions{
			Key:   string(option.KeyBytes()),
			Value: ValueOrHash{Value: string(option.ValueBytes())},
		}
	case option.IsDelete():
		op.Type = DeleteOperation

Review comment (suggestion):
Don't know why we added parsing `Compare_CREATE` but we should not mix `CreateRevision` with `ModRevision`.

Code after:
	results := []EtcdOperationResult{}
	for _, resp := range resp.Responses {
		results = append(results, toEtcdOperationResult(resp))
	}
	h.appendSuccessful(request, start, end, txnResponse(results, resp.Succeeded, revision))
}

func (h *AppendableHistory) appendSuccessful(request EtcdRequest, start, end time.Duration, response MaybeEtcdResponse) {
	op := porcupine.Operation{
		ClientId: h.streamID,
		Input:    request,
		Call:     start.Nanoseconds(),
		Output:   response,
		Return:   end.Nanoseconds(),
	}
	h.append(op)
}

func toEtcdCondition(cmp clientv3.Cmp) (cond EtcdCondition) {
	switch {
	case cmp.Result == etcdserverpb.Compare_EQUAL && cmp.Target == etcdserverpb.Compare_MOD:
		cond.Key = string(cmp.KeyBytes())
		cond.ExpectedRevision = cmp.TargetUnion.(*etcdserverpb.Compare_ModRevision).ModRevision
	case cmp.Result == etcdserverpb.Compare_EQUAL && cmp.Target == etcdserverpb.Compare_VERSION:
		cond.ExpectedVersion = cmp.TargetUnion.(*etcdserverpb.Compare_Version).Version
		cond.Key = string(cmp.KeyBytes())
	default:
		panic(fmt.Sprintf("Compare not supported, target: %q, result: %q", cmp.Target, cmp.Result))
	}
	return cond
}

func toEtcdOperation(option clientv3.Op) (op EtcdOperation) {
	switch {
	case option.IsGet():
		op.Type = RangeOperation
		op.Range = RangeOptions{
			Start: string(option.KeyBytes()),
			End:   string(option.RangeBytes()),
		}
	case option.IsPut():
		op.Type = PutOperation
		op.Put = PutOptions{
			Key:   string(option.KeyBytes()),
			Value: ValueOrHash{Value: string(option.ValueBytes())},
		}
	case option.IsDelete():
		op.Type = DeleteOperation
		op.Delete = DeleteOptions{
			Key: string(option.KeyBytes()),
		}