Skip to content

Commit

Permalink
Add Transfrom to support functional mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlic committed Nov 20, 2018
1 parent d86cc12 commit e6eb65d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 3 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ Collection picked = Collection.of(opportunities).pick(new Set<String>{'Name', 'A

Maps all elements of `Collection` view into another `Collection` view with the provided `SObjectToSObjectFunction` mapping function.

| Modifier and type | Method | Description |
|-------------------|--------|-------------|
| `Collection` | `mapAll(SObjectToSObjectFunction fn)` | Returns a new `Collection` view formed by mapping all current view elements with `fn` |


```apex
private class DoubleAmount implements SObjectToSObjectFunction {
public SObject apply(SObject record) {
Expand All @@ -236,9 +241,16 @@ List<Opportunity> opps = new List<Opportunity>{
Collection.of(opps).mapAll(new DoubleAmount()); // amounts have been doubled
```

| Modifier and type | Method | Description |
|-------------------|--------|-------------|
| `Collection` | `mapAll(SObjectToSObjectFunction fn)` | Returns a new `Collection` view formed by mapping all current view elements with `fn` |
One `SObjectToSObjectFunction` is provided out of the box, `RecordTransform`. It is instantiated through a factory method on `Transform`:

#### `RecordTransform`

`RecordTransform` 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'
```

### `mapSome`
<a name="map-some"></a>
Expand Down
10 changes: 10 additions & 0 deletions src/classes/CollectionTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,14 @@ public class CollectionTest {
System.assertEquals(200, opportunities[0].Amount);
System.assertEquals(150, opportunities[1].Amount);
}

@IsTest
static void testMapWithTransform() {
List<Opportunity> opportunities = Collection.of(new List<Opportunity>{
new Opportunity(Amount = 100),
new Opportunity(Amount = 150)
}).mapAll(Transform.record(new Opportunity(Amount = 123))).asList();
System.assertEquals(123, opportunities[0].Amount);
System.assertEquals(123, opportunities[1].Amount);
}
}
17 changes: 17 additions & 0 deletions src/classes/RecordTransform.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class RecordTransform implements SObjectToSObjectFunction {
private SObject prototype;
private Map<String, Object> populatedFieldsMap;

public SObject apply(SObject record) {

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

public RecordTransform(sObject prototype) {
this.prototype = prototype;
this.populatedFieldsMap = prototype.getPopulatedFieldsAsMap();
}
}
5 changes: 5 additions & 0 deletions src/classes/RecordTransform.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>44.0</apiVersion>
<status>Active</status>
</ApexClass>
19 changes: 19 additions & 0 deletions src/classes/RecordTransformTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@IsTest
public class RecordTransformTest {
@IsTest
public static void definedFieldsAreUsed() {
Opportunity opp = new Opportunity();
RecordTransform t = Transform.record(new Opportunity(Amount = 1000, Name = 'Test'));
t.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);
System.assertEquals(2000, opp.Amount);
}
}
5 changes: 5 additions & 0 deletions src/classes/RecordTransformTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>44.0</apiVersion>
<status>Active</status>
</ApexClass>
5 changes: 5 additions & 0 deletions src/classes/Transform.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public with sharing class Transform {
public static RecordTransform record(SObject record) {
return new RecordTransform(record);
}
}
5 changes: 5 additions & 0 deletions src/classes/Transform.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>44.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit e6eb65d

Please sign in to comment.