Do partial updates with MutateIn require the use of dynamic?

I’m attempting to perform partial updates with CouchbaseNetClient, but receive an InvalidCastException if I don’t use dynamic as the generic type.

I’m using a simple class like this:

public class TestClass
{
public string foo { get; set; }
public string bar { get; set; }
}

I’m able to make a partial update with this:

var builder = bucket.MutateIn< dynamic>(key).Replace(“foo”, “baz”);
var result = builder.Execute();

However, this generates an exception:

var builder = bucket.MutateIn< TestClass>(key).Replace(“foo”, “baz”);
var result = builder.Execute();

The exception occurs on this line within OptimizeSingleMutation of CouchbaseBucket because spec.Value contains the string “baz”, which cannot be cast to TestClass:

return new SubDocReplace< T>(builder, builder.Key, (T)spec.Value, null, _transcoder, _operationLifespanTimeout);

Is this use of MutateIn supported? Is there another way to perform partial updates without using dynamic? One issue with dynamic is that you are not able to use the lambda based extension method to perform updates.

This code results in a compile time exception: “An expression tree may not contain a dynamic operation”:

var builder = bucket.MutateIn< dynamic>(key).Replace(x => x.bar, “baz”);
var result = builder.Execute();

Should I be doing something different to make this work?

Thanks,
Glen

@drumboog -

The POCO should work for your first example; dynamic Types are just an alternative. The second example with the lambda expression, however, will require the use of a POCO since the Type needs to be known at compile time.

Can you post the exception you are hitting for the first example?

-Jeff

It’s an InvalidCastException: Unable to cast object of type ‘System.String’ to type ‘ConsoleApplication1.TestClass’.

On the line below, T is correctly evaluated as TestClass and Value is a string.

I think it looks like a bug in the singular mutation system. The value here:

shouldn’t be of type T, which is the type of overall document. Seems like it should be either object or it should be an additional generic type, like SubDocReplace<TDocument, TValue>.

Brant

@drumboog -

I created a Jira ticket to look deeper into: https://issues.couchbase.com/browse/NCBC-1161

You can add yourself as a watcher if you would like to follow progress.

@btburnett3 - thanks for the info!

-Jeff