Skip to content

Commit

Permalink
Move RecordTransform functionality to CopyFields to make purpose more…
Browse files Browse the repository at this point in the history
… explicit
  • Loading branch information
ipavlic committed Mar 4, 2019
1 parent 07ef9ce commit ec3818e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ List<Opportunity> opps = new List<Opportunity>{
Collection.of(opps).mapAll(new DoubleAmount()); // amounts have been doubled
```

One `SObjectToSObjectFunction` is provided out of the box, `RecordTransform`. It is instantiated through a factory method on `Transform`:
One `SObjectToSObjectFunction` is provided out of the box, `CopyFields`. It is instantiated through a factory method, `CopyFields.fromRecord`.

#### `RecordTransform`
#### `CopyFields`

`RecordTransform` copies all defined fields from `prototype` record to the record it is applied to. Values of fields defined for `prototype` are overwritten on
`CopyFields` copies all defined fields from `prototype` record to the record it is applied to. Values of fields defined for `prototype` are overwritten on
target records. Other fields on target record are not modified.

```apex
Collection.of(opps).mapAll(Transform.record(new Opportunity(Name = 'Test'))); // Name field has been overwritten with 'Test'
Collection.of(opps).mapAll(CopyFields.fromRecord(new Opportunity(Name = 'Test'))); // Name field has been overwritten with 'Test'
```

### `mapSome`
Expand Down
4 changes: 2 additions & 2 deletions src/classes/CollectionTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,11 @@ public class CollectionTest {
}

@IsTest
static void testMapWithTransform() {
static void testMapWithCopyFields() {
List<Opportunity> opportunities = Collection.of(new List<Opportunity>{
new Opportunity(Amount = 100),
new Opportunity(Amount = 150)
}).mapAll(Transform.record(new Opportunity(Amount = 123))).asList();
}).mapAll(CopyFields.fromRecord(new Opportunity(Amount = 123))).asList();
System.assertEquals(123, opportunities[0].Amount);
System.assertEquals(123, opportunities[1].Amount);
}
Expand Down
10 changes: 7 additions & 3 deletions src/classes/RecordTransform.cls → src/classes/CopyFields.cls
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
public class RecordTransform implements SObjectToSObjectFunction {
public class CopyFields implements SObjectToSObjectFunction {

private SObject prototype;
private Map<String, Object> populatedFieldsMap;

public SObject apply(SObject record) {
public static CopyFields fromRecord(SObject record) {
return new CopyFields(record);
}

public SObject apply(SObject record) {
for (String field : populatedFieldsMap.keySet()) {
record.put(field, prototype.get(field));
}
return record;
}

public RecordTransform(sObject prototype) {
private CopyFields(sObject prototype) {
this.prototype = prototype;
this.populatedFieldsMap = prototype.getPopulatedFieldsAsMap();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>44.0</apiVersion>
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
@IsTest
public class RecordTransformTest {
public class CopyFieldsTest {
@IsTest
public static void definedFieldsAreUsed() {
Opportunity opp = new Opportunity();
RecordTransform t = Transform.record(new Opportunity(Amount = 1000, Name = 'Test'));
t.apply(opp);
CopyFields c = CopyFields.fromRecord(new Opportunity(Amount = 1000, Name = 'Test'));
c.apply(opp);
System.assertEquals(1000, opp.Amount);
System.assertEquals('Test', opp.Name);
}

@IsTest
public static void fieldsAreOverwritten() {
Opportunity opp = new Opportunity(Amount = 1000);
RecordTransform t = Transform.record(new Opportunity(Amount = 2000));
t.apply(opp);
CopyFields c = CopyFields.fromRecord(new Opportunity(Amount = 2000));
c.apply(opp);
System.assertEquals(2000, opp.Amount);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>44.0</apiVersion>
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>
5 changes: 0 additions & 5 deletions src/classes/RecordTransformTest.cls-meta.xml

This file was deleted.

5 changes: 0 additions & 5 deletions src/classes/Transform.cls

This file was deleted.

0 comments on commit ec3818e

Please sign in to comment.