forked from KocproZ/jenkins-discord
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from CrystallDEV/fix/issue-115
Fix #115 by using a custom object for dynamic fields
- Loading branch information
Showing
5 changed files
with
140 additions
and
31 deletions.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
src/main/java/nz/co/jammehcow/jenkinsdiscord/DynamicFieldContainer.java
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,86 @@ | ||
package nz.co.jammehcow.jenkinsdiscord; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author Marek Hasselder | ||
*/ | ||
public class DynamicFieldContainer { | ||
|
||
private final List<DynamicField> fields = new ArrayList<>(); | ||
|
||
public List<DynamicField> getFields() { | ||
return fields; | ||
} | ||
|
||
public DynamicFieldContainer addField(String key, String value) { | ||
fields.add(new DynamicField(key, value)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
// Could be optimized using >8 Java Features | ||
StringBuilder builder = new StringBuilder(); | ||
for (int i = 0; i < fields.size(); i++) { | ||
// Serialize the field using the custom toString method | ||
builder.append(fields.get(i).toString()); | ||
if (i + 1 < fields.size()) { | ||
builder.append(", "); | ||
} | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
/** | ||
* Creates a new dynamic field container using the fieldsString. | ||
* | ||
* @param fieldsString the string containing the dynamic fields (key1:value1, key2:value2) | ||
* @return a dynamic field container containing the | ||
* @throws RuntimeException if the string is in a wrong format | ||
*/ | ||
public static DynamicFieldContainer of(String fieldsString) { | ||
DynamicFieldContainer fieldContainer = new DynamicFieldContainer(); | ||
|
||
try { | ||
for (String s : fieldsString.split(", ")) { | ||
String[] pair = s.split(":"); | ||
fieldContainer.addField(pair[0], pair[1]); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException("Dynamic fields string is in a wrong format. Using empty container as fallback"); | ||
} | ||
|
||
return fieldContainer; | ||
} | ||
|
||
public static class DynamicField { | ||
|
||
private final String key; | ||
private final String value; | ||
|
||
public DynamicField(String key, String value) { | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(value); | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s:%s", key, value); | ||
} | ||
} | ||
|
||
|
||
} |
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
3 changes: 3 additions & 0 deletions
3
...resources/nz/co/jammehcow/jenkinsdiscord/WebhookPublisher/help-dynamicFieldContainer.html
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 @@ | ||
<div> | ||
Field that contains key value pairs in an array in the following format `key1:value1, key2:value2, [...]` | ||
</div> |