-
Notifications
You must be signed in to change notification settings - Fork 14
Feat/add tuple #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Feat/add tuple #122
Changes from all commits
1be71cc
c00518b
96372af
03a26b7
83a3a29
b62047d
8207b77
940717f
93e2031
805c0bb
dfed13f
b4a5956
fecbea5
d19e20e
4cce47c
ee3f6df
9ff506b
0efb55e
b8486d1
56bec3a
e0115b6
b8a13cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| `APIVERSION: 58` | ||
|
|
||
| `STATUS: ACTIVE` | ||
|
|
||
| Tuple represents a key-value pair. Kinda. Don't come at me math nerds. | ||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| You may be asking why this exists, given the existence of Map. Unfortuately Map isn't available everywhere. | ||
| Notably, it's not available in Apex-defined Data Types used by Flow/Invocable actions. | ||
| Additionally, `Object` is not a valid data type in Apex-defined Data Types, so we can't use a list of objects | ||
| that have a string key, and Object value. This is the next best thing. A kind of polyfill for Map<String,Object> | ||
| for use in Apex-defined Data Types. | ||
| See also: https://help.salesforce.com/s/articleView?id=sf.flow_considerations_apex_data_type.htm&type=5 | ||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Available primitive datatypes available for use in tuple: | ||
| Boolean, Integer, Long, Decimal, Double, Date, DateTime, and String. Single values and lists are supported for each data type. | ||
|
|
||
| ## Constructors | ||
|
|
||
| ### `public Tuple()` | ||
|
|
||
| `SUPPRESSWARNINGS` | ||
|
|
||
| In order to create a Tuple instance from within a flow, you need a no-arg constructor. | ||
|
|
||
| ### `public Tuple(String key, Object value)` | ||
|
|
||
| standard constructor accepting a key and value | ||
|
|
||
| #### Parameters | ||
|
|
||
| | Param | Description | | ||
| | ------- | ----------------------------------- | | ||
| | `key` | String the name of the Tuple's Key. | | ||
| | `value` | Object the Tuple's Value. | | ||
|
|
||
| --- | ||
|
|
||
| ## Fields | ||
|
|
||
| ### `public booleanValue` → `Boolean` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public booleans` → `Boolean` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public dateTimeValue` → `Datetime` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public dateValue` → `Date` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public dates` → `Date` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public datetimes` → `Datetime` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public decimalValue` → `Decimal` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public decimals` → `Decimal` | ||
|
|
||
dschach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public doubleValue` → `Double` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public doubles` → `Double` | ||
|
|
||
dschach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public integerValue` → `Integer` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public integers` → `Integer` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public key` → `String` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public longValue` → `Long` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public longs` → `Long` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public sobjectValue` → `SObject` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public sobjects` → `SObject` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public stringValue` → `String` | ||
|
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| ### `public strings` → `String` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AURAENABLED` | ||
| `INVOCABLEVARIABLE` | ||
|
|
||
| --- | ||
|
|
||
| ## Methods | ||
|
|
||
| ### `private void determineTypeAndSetCastedValue(Object value)` | ||
|
|
||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `SUPPRESSWARNINGS` | ||
|
|
||
| Method is responsible for determining the type of the value and setting the appropriate field. | ||
|
|
||
| #### Parameters | ||
|
|
||
| | Param | Description | | ||
| | ------- | ------------------------- | | ||
| | `value` | Object the Tuple's Value. | | ||
|
|
||
| --- | ||
|
|
||
| ## Classes | ||
|
|
||
| ### TupleException | ||
|
|
||
| A custom Exception type thrown when the incoming Object is not a recognized Apex-defined-type safe | ||
| type. | ||
codefriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| **Inheritance** | ||
|
|
||
| TupleException | ||
|
|
||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /** | ||
| * @description Tuple represents a key-value pair. Kinda. Don't come at me math nerds. | ||
| * | ||
| * You may be asking why this exists, given the existence of Map. Unfortuately Map isn't available everywhere. | ||
| * Notably, it's not available in Apex-defined Data Types used by Flow/Invocable actions. | ||
| * | ||
| * Additionally, `Object` is not a valid data type in Apex-defined Data Types, so we can't use a list of objects | ||
| * that have a string key, and Object value. This is the next best thing. A kind of polyfill for Map<String,Object> | ||
| * for use in Apex-defined Data Types. | ||
| * | ||
| * See also: https://help.salesforce.com/s/articleView?id=sf.flow_considerations_apex_data_type.htm&type=5 | ||
| * Available primitive datatypes available for use in tuple: | ||
| * Boolean, Integer, Long, Decimal, Double, Date, DateTime, and String. Single values and lists are supported for each data type. | ||
| */ | ||
| @SuppressWarnings('PMD.StdCyclomaticComplexity') | ||
| public with sharing class Tuple { | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public String key; | ||
|
|
||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Boolean[] booleans; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Boolean booleanValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Date dateValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Date[] dates; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Datetime[] datetimes; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Datetime dateTimeValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Double doubleValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Double[] doubles; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Decimal decimalValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Decimal[] decimals; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Integer integerValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Integer[] integers; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Long longValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public Long[] longs; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public String stringValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public String[] strings; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public SObject sobjectValue; | ||
| @AuraEnabled | ||
| @InvocableVariable | ||
| public SObject[] sobjects; | ||
|
|
||
| /** | ||
| * @description In order to create a Tuple instance from within a flow, you need a no-arg constructor. | ||
| */ | ||
| @SuppressWarnings('PMD.EmptyStatementBlock') | ||
| public Tuple() { | ||
| } | ||
|
|
||
| /** | ||
| * @description standard constructor accepting a key and value | ||
| * @param key String the name of the Tuple's Key. | ||
| * @param value Object the Tuple's Value. | ||
| */ | ||
| public Tuple(String key, Object value) { | ||
| if (key == null) { | ||
| throw new TupleException('Key cannot be null'); | ||
| } | ||
| if (value == null) { | ||
| throw new TupleException('Value cannot be null'); | ||
| } | ||
| this.key = key; | ||
| determineTypeAndSetCastedValue(value); | ||
| } | ||
|
|
||
| /** | ||
| * @description Method is responsible for determining the type of the value and setting the appropriate field. | ||
| * @param value Object the Tuple's Value. | ||
| */ | ||
| @SuppressWarnings('PMD.CyclomaticComplexity, PMD.StdCyclomaticComplexity') | ||
| private void determineTypeAndSetCastedValue(Object value) { | ||
| if (value instanceof Boolean) { | ||
| this.booleanValue = (Boolean) value; | ||
| } else if (value instanceof List<Boolean>) { | ||
| this.booleans = (List<Boolean>) value; | ||
| } else if (value instanceof Datetime) { | ||
| this.dateTimeValue = (Datetime) value; | ||
| } else if (value instanceof List<Datetime>) { | ||
| this.datetimes = (List<Datetime>) value; | ||
| } else if (value instanceof Date) { | ||
| this.dateValue = (Date) value; | ||
| } else if (value instanceof List<Date>) { | ||
| this.dates = (List<Date>) value; | ||
| } else if (value instanceof Long) { | ||
| this.longValue = (Long) value; | ||
| } else if (value instanceof List<Long>) { | ||
| this.longs = (List<Long>) value; | ||
| } else if (value instanceof Integer) { | ||
| this.integerValue = (Integer) value; | ||
| } else if (value instanceof List<Integer>) { | ||
| this.integers = (List<Integer>) value; | ||
| } else if (value instanceof Decimal) { | ||
| this.decimalValue = (Decimal) value; | ||
| } else if (value instanceof List<Decimal>) { | ||
| this.decimals = (List<Decimal>) value; | ||
| } else if (value instanceof Double) { | ||
| this.doubleValue = (Double) value; | ||
| } else if (value instanceof List<Double>) { | ||
| this.doubles = (List<Double>) value; | ||
| } else if (value instanceof String) { | ||
| this.stringValue = (String) value; | ||
| } else if (value instanceof List<String>) { | ||
| this.strings = (List<String>) value; | ||
| } else if (value instanceof List<SObject>) { | ||
| this.sobjects = (List<SObject>) value; | ||
| } else if (value instanceof SObject) { | ||
| this.sobjectValue = (SObject) value; | ||
| } else { | ||
| throw new TupleException('Unsupported data type: ' + value); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @description A custom Exception type thrown when the incoming Object is not a recognized Apex-defined-type safe | ||
| * type. | ||
| */ | ||
| public class TupleException extends Exception { | ||
| } | ||
| } | ||
|
Comment on lines
+1
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The However, there are some potential improvements that could be made:
Here's an example of how you might refactor the private void determineTypeAndSetCastedValue(Object value) {
if (value instanceof Boolean) {
setBooleanValue(value);
} else if (value instanceof List<Boolean>) {
setBooleansValue(value);
} else if (value instanceof Datetime) {
setDatetimeValue(value);
} // ... continue for each type
}
private void setBooleanValue(Object value) {
this.booleanValue = (Boolean) value;
}
private void setBooleansValue(Object value) {
this.booleans = (List<Boolean>) value;
}
private void setDatetimeValue(Object value) {
this.dateTimeValue = (Datetime) value;
}
// ... continue for each typeThis approach breaks down the large method into smaller, more manageable methods, each responsible for setting a specific type of value. It also makes it easier to add or remove support for types in the future. |
||
| 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>58.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
Uh oh!
There was an error while loading. Please reload this page.