-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024-12-04 17:13:54.644919 new snippets
- Loading branch information
1 parent
f8f753e
commit 50ecff0
Showing
21 changed files
with
1,462 additions
and
675 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
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,52 @@ | ||
//date: 2024-12-04T16:51:06Z | ||
//url: https://api.github.com/gists/cf0f9afbcf359df65846a760da64d230 | ||
//owner: https://api.github.com/users/avishaybp81 | ||
|
||
package com.example.additionalproperties; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
public class AdditionalPropertiesExtractor { | ||
|
||
public static Map<String, String> extractAdditionalProperties(Object obj) { | ||
Map<String, String> allProperties = new HashMap<>(); | ||
processObject(obj, allProperties, ""); | ||
return allProperties; | ||
} | ||
|
||
private static void processObject(Object obj, Map<String, String> allProperties, String prefix) { | ||
if (obj == null) { | ||
return; | ||
} | ||
|
||
if (obj instanceof Map) { | ||
for (Map.Entry<String, Object> entry : ((Map<String, Object>) obj).entrySet()) { | ||
allProperties.put(prefix + "." + entry.getKey(), entry.getValue().toString()); | ||
} | ||
|
||
} else { | ||
for (Field field : obj.getClass().getDeclaredFields()) { | ||
if (field.getType().getPackage().equals(SecurityEvent.class.getPackage()) || field.getType().equals(Map.class)) { | ||
try { | ||
field.setAccessible(true); | ||
} catch (Exception ignored) { | ||
continue; | ||
} | ||
try { | ||
Object fieldValue = field.get(obj); | ||
System.out.println("--->: " + fieldValue.getClass().getSimpleName() + " : " + fieldValue); | ||
processObject(fieldValue, allProperties, obj.getClass().getSimpleName()); | ||
} catch (IllegalAccessException e) { | ||
// Handle exception, e.g., log the error | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
} | ||
} |
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,58 @@ | ||
//date: 2024-12-04T16:51:06Z | ||
//url: https://api.github.com/gists/cf0f9afbcf359df65846a760da64d230 | ||
//owner: https://api.github.com/users/avishaybp81 | ||
|
||
package com.example.additionalproperties; | ||
|
||
import com.example.additionalproperties.SecurityEvent; | ||
import com.example.additionalproperties.AdditionalPropertiesExtractor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
SecurityEvent event = new SecurityEvent(); | ||
event.setEventId("event123"); | ||
event.setEventType("security_event"); | ||
|
||
// Set top-level additional properties | ||
Map<String, String> topLevelProps = new HashMap<>(); | ||
topLevelProps.put("prop1", "value1"); | ||
topLevelProps.put("prop2", "value2"); | ||
event.setAdditionalProperties(topLevelProps); | ||
|
||
// Create nested event | ||
SecurityEvent.NestedSecurityEvent nestedEvent = new SecurityEvent.NestedSecurityEvent(); | ||
nestedEvent.setNestedId("nested123"); | ||
nestedEvent.setNestedType("nested_event"); | ||
|
||
// Set nested additional properties | ||
Map<String, String> nestedProps = new HashMap<>(); | ||
nestedProps.put("nestedProp1", "nestedValue1"); | ||
nestedProps.put("nestedProp2", "nestedValue2"); | ||
nestedEvent.setAdditionalProperties(nestedProps); | ||
|
||
// Create deeply nested event | ||
SecurityEvent.NestedSecurityEvent.DeeplyNestedSecurityEvent deeplyNestedEvent = new SecurityEvent.NestedSecurityEvent.DeeplyNestedSecurityEvent(); | ||
deeplyNestedEvent.setDeeplyNestedId("deeplyNested123"); | ||
deeplyNestedEvent.setDeeplyNestedType("deeplyNested_event"); | ||
|
||
// Set deeply nested additional properties | ||
Map<String, String> deeplyNestedProps = new HashMap<>(); | ||
deeplyNestedProps.put("deeplyNestedProp1", "deeplyNestedValue1"); | ||
deeplyNestedProps.put("deeplyNestedProp2", "deeplyNestedValue2"); | ||
deeplyNestedEvent.setAdditionalProperties(deeplyNestedProps); | ||
|
||
nestedEvent.setDeeplyNestedEvent(deeplyNestedEvent); | ||
event.setNestedEvent(nestedEvent); | ||
|
||
// Extract additional properties | ||
Map<String, String> extractedProps = AdditionalPropertiesExtractor.extractAdditionalProperties(event); | ||
|
||
// Print extracted properties | ||
|
||
System.out.println("Result below:"); | ||
extractedProps.forEach((key, value) -> System.out.println("\t" + 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//date: 2024-12-04T16:51:06Z | ||
//url: https://api.github.com/gists/cf0f9afbcf359df65846a760da64d230 | ||
//owner: https://api.github.com/users/avishaybp81 | ||
|
||
package com.example.additionalproperties; | ||
|
||
import java.util.Map; | ||
|
||
public class SecurityEvent { | ||
private String eventId; | ||
private String eventType; | ||
private Map<String, String> additionalProperties; | ||
private NestedSecurityEvent nestedEvent; | ||
|
||
// Getters and Setters | ||
public String getEventId() { | ||
return eventId; | ||
} | ||
|
||
public void setEventId(String eventId) { | ||
this.eventId = eventId; | ||
} | ||
|
||
public String getEventType() { | ||
return eventType; | ||
} | ||
|
||
public void setEventType(String eventType) { | ||
this.eventType = eventType; | ||
} | ||
|
||
public Map<String, String> getAdditionalProperties() { | ||
return additionalProperties; | ||
} | ||
|
||
public void setAdditionalProperties(Map<String, String> additionalProperties) { | ||
this.additionalProperties = additionalProperties; | ||
} | ||
|
||
public NestedSecurityEvent getNestedEvent() { | ||
return nestedEvent; | ||
} | ||
|
||
public void setNestedEvent(NestedSecurityEvent nestedEvent) { | ||
this.nestedEvent = nestedEvent; | ||
} | ||
|
||
// NestedSecurityEvent Class | ||
public static class NestedSecurityEvent { | ||
private String nestedId; | ||
private String nestedType; | ||
private Map<String, String> additionalProperties; | ||
private DeeplyNestedSecurityEvent deeplyNestedEvent; | ||
|
||
// Getters and Setters | ||
public String getNestedId() { | ||
return nestedId; | ||
} | ||
|
||
public void setNestedId(String nestedId) { | ||
this.nestedId = nestedId; | ||
} | ||
|
||
public String getNestedType() { | ||
return nestedType; | ||
} | ||
|
||
public void setNestedType(String nestedType) { | ||
this.nestedType = nestedType; | ||
} | ||
|
||
public Map<String, String> getAdditionalProperties() { | ||
return additionalProperties; | ||
} | ||
|
||
public void setAdditionalProperties(Map<String, String> additionalProperties) { | ||
this.additionalProperties = additionalProperties; | ||
} | ||
|
||
public DeeplyNestedSecurityEvent getDeeplyNestedEvent() { | ||
return deeplyNestedEvent; | ||
} | ||
|
||
public void setDeeplyNestedEvent(DeeplyNestedSecurityEvent deeplyNestedEvent) { | ||
this.deeplyNestedEvent = deeplyNestedEvent; | ||
} | ||
|
||
// DeeplyNestedSecurityEvent Class | ||
public static class DeeplyNestedSecurityEvent { | ||
private String deeplyNestedId; | ||
private String deeplyNestedType; | ||
private Map<String, String> additionalProperties; | ||
|
||
// Getters and Setters | ||
public String getDeeplyNestedId() { | ||
return deeplyNestedId; | ||
} | ||
|
||
public void setDeeplyNestedId(String deeplyNestedId) { | ||
this.deeplyNestedId = deeplyNestedId; | ||
} | ||
|
||
public String getDeeplyNestedType() { | ||
return deeplyNestedType; | ||
} | ||
|
||
public void setDeeplyNestedType(String deeplyNestedType) { | ||
this.deeplyNestedType = deeplyNestedType; | ||
} | ||
|
||
public Map<String, String> getAdditionalProperties() { | ||
return additionalProperties; | ||
} | ||
|
||
public void setAdditionalProperties(Map<String, String> additionalProperties) { | ||
this.additionalProperties = additionalProperties; | ||
} | ||
} | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.