-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
367 additions
and
23 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public with sharing class ObjectCollection { | ||
private List<Object> objects; | ||
|
||
public ObjectCollection(List<Object> objects) { | ||
this.objects = objects; | ||
} | ||
|
||
public List<Object> asList() { | ||
return new List<Object>(objects); | ||
} | ||
|
||
public Set<Object> asSet() { | ||
return new Set<Object>(objects); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
public with sharing class MapToObject implements SObjectToObjectFunction { | ||
Type type; | ||
Map<String, String> fieldMappings; | ||
SObjectFieldReader fieldReader; | ||
Map<String, Object> fieldValues = new Map<String, Object>(); | ||
|
||
public MapToObject(Type type) { | ||
this.type = type; | ||
this.fieldValues = new Map<String, Object>(); | ||
this.fieldMappings = new Map<String, String>(); | ||
this.fieldReader = new SObjectFieldReader(); | ||
} | ||
|
||
private Object setField(Map<String, Object> objectData, String path, Object value) { | ||
Integer dotPos = path.indexOf('.'); | ||
if (dotPos == -1) { | ||
return objectData.put(path, value); | ||
} else { | ||
String childObject = path.substring(0, dotPos); | ||
String remainingPath = path.substring(dotPos + 1, path.length()); | ||
|
||
if (!objectData.containsKey(childObject)) { | ||
objectData.put(childObject, new Map<String, Object>()); | ||
} | ||
Map<String, Object> childObjectData = (Map<String, Object>) objectData.get(childObject); | ||
return setField(childObjectData, remainingPath, value); | ||
} | ||
} | ||
|
||
public MapToObject setField(String targetFieldPath, Object value) { | ||
setField(fieldValues, targetFieldPath, value); | ||
return this; | ||
} | ||
|
||
public MapToObject setFields(Map<String, Object> fieldValues) { | ||
for (String targetFieldPath : fieldValues.keySet()) { | ||
setField(targetFieldPath, fieldValues.get(targetFieldPath)); | ||
} | ||
return this; | ||
} | ||
|
||
public MapToObject mapField(String targetFieldPath, String sourceFieldRelation) { | ||
fieldMappings.put(targetFieldPath, sourceFieldRelation); | ||
return this; | ||
} | ||
|
||
public MapToObject mapField(String targetFieldPath, Schema.SObjectField sourceField) { | ||
fieldMappings.put(targetFieldPath, sourceField.getDescribe().getName()); | ||
return this; | ||
} | ||
|
||
public MapToObject mapFields(Map<String, Schema.SObjectField> fieldMappings) { | ||
for (String targetFieldPath : fieldMappings.keySet()) { | ||
Schema.SObjectField sourceField = fieldMappings.get(targetFieldPath); | ||
mapField(targetFieldPath, sourceField); | ||
} | ||
return this; | ||
} | ||
|
||
public MapToObject mapFields(Map<String, String> fieldMappings) { | ||
for (String targetFieldPath : fieldMappings.keySet()) { | ||
String sourceFieldRelation = fieldMappings.get(targetFieldPath); | ||
mapField(targetFieldPath, sourceFieldRelation); | ||
} | ||
return this; | ||
} | ||
|
||
public Object call(SObject record) { | ||
for (String targetFieldPath : fieldMappings.keySet()) { | ||
String sourceFieldRelation = fieldMappings.get(targetFieldPath); | ||
setField(targetFieldPath, fieldReader.read(record, sourceFieldRelation)); | ||
} | ||
return JSON.deserialize(JSON.serialize(fieldValues), type); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
sfdx-source/apex-fp/main/classes/function/MapToObject.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
sfdx-source/apex-fp/main/classes/function/SObjectToObjectFunction.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface SObjectToObjectFunction { | ||
Object call(SObject record); | ||
} |
5 changes: 5 additions & 0 deletions
5
sfdx-source/apex-fp/main/classes/function/SObjectToObjectFunction.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
sfdx-source/apex-fp/test/classes/function/MapToObjectTest.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
@IsTest(isParallel=true) | ||
private class MapToObjectTest { | ||
|
||
private class MappingTarget { | ||
public String name; | ||
public MappingTarget child; | ||
} | ||
|
||
@IsTest | ||
private static void setFieldSetsFieldValue() { | ||
MapToObject m = new MapToObject(MappingTarget.class).setField('name', 'foo'); | ||
MappingTarget mapped = (MappingTarget) m.call(new Account()); | ||
System.assertEquals('foo', mapped.name); | ||
} | ||
|
||
@IsTest | ||
private static void setFieldSetsNestedFieldValue() { | ||
MapToObject m = new MapToObject(MappingTarget.class).setField('child.name', 'foo'); | ||
MappingTarget mapped = (MappingTarget) m.call(new Account()); | ||
System.assertEquals('foo', mapped.child.name); | ||
} | ||
|
||
@IsTest | ||
private static void fieldValuesCanBeProvidedAsAMap() { | ||
MapToObject m = new MapToObject(MappingTarget.class).setFields( | ||
new Map<String, Object>{ | ||
'name' => 'foo', | ||
'child.name' => 'bar' | ||
}); | ||
MappingTarget mapped = (MappingTarget) m.call(new Account(Name = 'foo')); | ||
System.assertEquals('foo', mapped.name); | ||
System.assertEquals('bar', mapped.child.name); | ||
} | ||
|
||
@IsTest | ||
private static void fieldRelationsCanBeMappedToFieldPaths() { | ||
MapToObject m = new MapToObject(MappingTarget.class).mapField('name', 'Name'); | ||
MappingTarget mapped = (MappingTarget) m.call(new Account(Name = 'foo')); | ||
System.assertEquals('foo', mapped.name); | ||
} | ||
|
||
|
||
@IsTest | ||
private static void fieldRelationsCanBeMappedToNestedFieldPaths() { | ||
MapToObject m = new MapToObject(MappingTarget.class).mapField('child.name', 'Name'); | ||
MappingTarget mapped = (MappingTarget) m.call(new Account(Name = 'foo')); | ||
System.assertEquals('foo', mapped.child.name); | ||
} | ||
|
||
@IsTest | ||
private static void nestedFieldRelationsCanBeMappedToNestedFieldPaths() { | ||
MapToObject m = new MapToObject(MappingTarget.class).mapField('child.name', 'Account.Name'); | ||
MappingTarget mapped = (MappingTarget) m.call(new Opportunity(Account = new Account(Name = 'foo'))); | ||
System.assertEquals('foo', mapped.child.name); | ||
} | ||
|
||
@IsTest | ||
private static void fieldRelationsMappingsCanBeProvidedAsAMap() { | ||
MapToObject m = new MapToObject(MappingTarget.class).mapFields( | ||
new Map<String, String>{ | ||
'name' => 'Name', | ||
'child.name' => 'Name' | ||
}); | ||
MappingTarget mapped = (MappingTarget) m.call(new Account(Name = 'foo')); | ||
System.assertEquals('foo', mapped.name); | ||
System.assertEquals('foo', mapped.child.name); | ||
} | ||
|
||
@IsTest | ||
private static void fieldMappingsCanBeProvidedAsAMap() { | ||
MapToObject m = new MapToObject(MappingTarget.class).mapFields( | ||
new Map<String, Schema.SObjectField>{ | ||
'name' => Account.Name, | ||
'child.name' => Account.Name | ||
}); | ||
MappingTarget mapped = (MappingTarget) m.call(new Account(Name = 'foo')); | ||
System.assertEquals('foo', mapped.name); | ||
System.assertEquals('foo', mapped.child.name); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
sfdx-source/apex-fp/test/classes/function/MapToObjectTest.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.