Skip to content

Commit

Permalink
Fixes #41 by adding support for CMDT parent fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jamessimone committed Oct 16, 2024
1 parent 20c18d3 commit e5c9134
Show file tree
Hide file tree
Showing 40 changed files with 142 additions and 50 deletions.
2 changes: 1 addition & 1 deletion example-app/ExampleFactory.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion example-app/ExampleRepoFactory.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion example-app/handlers/AccountHandler.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion example-app/handlers/AccountHandlerTests.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion example-app/handlers/ExampleSObjectTest.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion example-app/triggers/TriggerHandler.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion example-app/triggers/TriggerHandler_Tests.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/dml/DML.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/dml/DMLMock.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/dml/DMLTests.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/dml/IDML.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/factory/Factory.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/factory/RepoFactory.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/factory/RepoFactoryMock.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/AdditionalSoslObject.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/AggregateRecord.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/AggregateRepository.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/AggregateRepositoryTests.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/Aggregation.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/AggregationTests.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/DateFunction.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/FieldLevelHistory.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/FieldLevelHistoryRepo.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/IAggregateRepository.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/IHistoryRepository.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
7 changes: 7 additions & 0 deletions force-app/repository/IRepository.cls
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public interface IRepository extends IDML {
Map<String, RepositorySortOrder> fieldToSortOrder,
Integer limitBy
);
IRepository addChildFields(
Schema.SObjectField childFieldToken,
List<QueryField> childFields,
List<Query> optionalWhereFilters,
Map<String, RepositorySortOrder> fieldToSortOrder,
Integer limitBy
);
}
2 changes: 1 addition & 1 deletion force-app/repository/IRepository.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/Query.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
39 changes: 39 additions & 0 deletions force-app/repository/QueryField.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public without sharing virtual class QueryField {
private final String fieldValue;

public QueryField(List<Schema.SObjectField> fieldTokens) {
this(getFieldNames(fieldTokens));
}

public QueryField(List<String> fieldNames) {
this.fieldValue = String.join(fieldNames, ',');
}

public QueryField(Schema.SObjectField token) {
this.fieldValue = token.toString();
}

public QueryField(List<Schema.SObjectField> parentFieldChain, List<Schema.SObjectField> parentFields) {
String base = '';
while (parentFieldChain.isEmpty() == false) {
base = parentFieldChain.remove(0).getDescribe().getRelationshipName() + '.';
}
List<String> fields = new List<String>();
for (Schema.SObjectField field : parentFields) {
fields.add(base + field.toString());
}
this.fieldValue = String.join(fields, ',');
}

public override String toString() {
return this.fieldValue;
}

private static List<String> getFieldNames(List<Schema.SObjectField> fieldTokens) {
List<String> fieldNames = new List<String>();
for (Schema.SObjectField token : fieldTokens) {
fieldNames.add(token.toString());
}
return fieldNames;
}
}
5 changes: 5 additions & 0 deletions force-app/repository/QueryField.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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/QueryTests.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
46 changes: 37 additions & 9 deletions force-app/repository/Repository.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
public virtual without sharing class Repository implements IRepository {
private final Map<Schema.SObjectField, String> childToRelationshipNames;
private final IDML dml;
@TestVisible
private final List<Schema.SObjectField> queryFields;
private final Set<String> selectFields = new Set<String>();

Expand Down Expand Up @@ -79,13 +80,7 @@ public virtual without sharing class Repository implements IRepository {
}

public Repository addParentFields(List<Schema.SObjectField> parentTypes, List<Schema.SObjectField> parentFields) {
String parentBase = '';
for (SObjectField parentId : parentTypes) {
parentBase += parentId.getDescribe().getRelationshipName() + '.';
}
for (SObjectField parentField : parentFields) {
this.selectFields.add(parentBase + parentField.getDescribe().getName());
}
this.selectFields.addAll(this.getParentFields(parentTypes, parentFields));
return this;
}

Expand All @@ -105,6 +100,22 @@ public virtual without sharing class Repository implements IRepository {
List<Query> optionalWhereFilters,
Map<String, RepositorySortOrder> fieldToSortOrder,
Integer limitAmount
) {
return this.addChildFields(
childFieldToken,
new List<QueryField>{ new QueryField(childFields) },
optionalWhereFilters,
fieldToSortOrder,
limitAmount
);
}

public Repository addChildFields(
Schema.SObjectField childFieldToken,
List<QueryField> childFields,
List<Query> optionalWhereFilters,
Map<String, RepositorySortOrder> fieldToSortOrder,
Integer limitBy
) {
if (this.childToRelationshipNames.containsKey(childFieldToken) == false || this.shouldAddChildFields == false) {
return this;
Expand All @@ -118,8 +129,8 @@ public virtual without sharing class Repository implements IRepository {
')';

Set<String> childFieldNames = new Set<String>{ 'Id' };
for (SObjectField childField : childFields) {
childFieldNames.add(childField.getDescribe().getName());
for (QueryField childField : childFields) {
childFieldNames.add(childField.toString());
}

this.selectFields.add(
Expand Down Expand Up @@ -158,6 +169,22 @@ public virtual without sharing class Repository implements IRepository {
this.limitAmount = null;
}

private List<String> getParentFields(
List<Schema.SObjectField> parentTypes,
List<Schema.SObjectField> parentFieldTokens
) {
List<String> parentFields = new List<String>();
String parentBase = '';
for (Schema.SObjectField parentToken : parentTypes) {
String parentName = parentToken.getDescribe().getRelationshipName() ?? parentToken.toString().replace('__c', '__r');
parentBase += parentName + '.';
}
for (Schema.SObjectField parentField : parentFieldTokens) {
parentFields.add(parentBase + parentField.getDescribe().getName());
}
return parentFields;
}

private Map<Schema.SObjectField, String> getChildRelationshipNames(Schema.SObjectType repoType) {
Map<Schema.SObjectField, String> localChildToRelationshipNames = new Map<Schema.SObjectField, String>();
for (Schema.ChildRelationship childRelationship : repoType.getDescribe().getChildRelationships()) {
Expand Down Expand Up @@ -276,6 +303,7 @@ public virtual without sharing class Repository implements IRepository {
return String.join(objectsPreJoin, ',');
}

// DML
public Database.SaveResult doInsert(SObject record) {
return this.dml.doInsert(record);
}
Expand Down
2 changes: 1 addition & 1 deletion force-app/repository/Repository.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/repository/RepositorySortOrder.cls-meta.xml
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>60.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading

0 comments on commit e5c9134

Please sign in to comment.