deepstream.io-client-java icon indicating copy to clipboard operation
deepstream.io-client-java copied to clipboard

Delete record path

Open pgoergler opened this issue 7 years ago • 4 comments

Hi,

How can i delete a field in a record ?

{
  fieldToClean: {
    fieldA: '..',
    fieldB: '..',
    fieldC: '..'
  }
}

How can i with java client delete fieldB (not setting null)

pgoergler avatar May 31 '17 14:05 pgoergler

Hey @pgoergler, the deepstream protocol deletes properties in records when it receives undefined. This is great in JavaScript but not so much in Java, where there is no concept of undefined.

The workaround using the java client would be something like the following:

Record record = client.record.getRecord('...');
JsonObject json = record.get();
System.out.println(json);
// {
//   fieldToClean: {
//     fieldA: '..',
//     fieldB: '..',
//     fieldC: '..'
//   }
// }
JsonObject nestedJson = json.get('fieldToClean');
nestedJson.remove('fieldB');
record.set(json);

There needs to be some casting, and you may need to set the fieldToClean property of the json object, but this would remove the property fieldB.

AlexBHarley avatar Jun 01 '17 15:06 AlexBHarley

Ok i understand, is it planed to simplify this process ? Maybe by adding a JsonUndefined object which let us write something like

Record record = client.record.getRecord('...');
record.set('fieldToClean.fieldB', new JsonUndefined());

pgoergler avatar Jun 01 '17 15:06 pgoergler

Happy for some discussion around this, that would work but feels weird as an api.

Something like

record.delete('path.nestedPath')

feels more natural, but goes against the api people are familiar with.

Would be interested to see what members of the community think about this.

AlexBHarley avatar Jun 01 '17 15:06 AlexBHarley

And what about :

record.unset('path.nestedPath');

pgoergler avatar Jun 01 '17 16:06 pgoergler