-
-
Notifications
You must be signed in to change notification settings - Fork 0
1. ConfiGoodie Basics
ConfiGoodies are POJOs with fields that are going to be filled with the data coming from external configuration files.
@Goodie
Annotation tells the reader to validate, fix and fill the field with the loaded config. Bounds a field in the config file with the field it annotates. It is possible to declare a custom name via key = ""
argument.
public class MyConfig extends JsonConfiGoodie {
@Goodie
String name; // <-- this will be filled with "$.name" path from the config
@Goodie(key = "age") // <-- Note that its configuration key is "age"
int myAge; // <-- this will be filled with "$.age" path from the config
}
After creating your own config POJO (a.k.a ConfiGoodie), configurations can be read by using ConfiGoodie::read
method. The code below will read data from given file, fix read config if anything is mismatching or is missing, then return as a ConfiGoodie POJO:
File file = new File("path/to/my_config.json");
MyConfig config = new MyConfig().read(file);
When a value in the config data is missing or its type mismatches, the fixer fills that field with a default value. If the default value is provided, it will always use the provided one to fix.
public class CatConfig extends JsonConfiGoodie {
@Goodie
String name = "Tom"; // <-- This is how you provide a default value.
@Goodie
List<String> friends = defaultValue(() -> {
List<String> friends = new LinkedList();
friends.add("Talking Tom Cat");
friends.add("Talking Angela");
return friends;
}) // Default values can be provided with a Supplier too!
}
If a default value is not provided, the fixer will use those values for corresponding fields:
Field Type | Default Value |
---|---|
String | "" |
boolean | false |
char | '\0' |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0F |
double | 0.0D |
long | 0L |
List<?> | new LinkedList() |
Map<String, ?> | new HashMap() |
any other type | null |
Virtual Fields (very similar to Virtual Columns) are fields that are computed by using other fields. ConfiGoodies allow you to Virtualize fields after initialization of it is done via @GoodieVirtualizer
method annotation. Here's how to do:
public class Credentials extends JsonConfiGoodie {
@Goodie
String firstName, lastName;
String fullName; // Note that, these fields do not require @Goodie annotation
String initials;
@GoodieVirtualizer
public void virtualizeFields() {
fullName = firstName + lastName;
initials = String.format("%s.%s.",
firstName.charAt(0), lastName.charAt(0));
}
}
They are allowed to have constructors. However, they MUST expose a nullary constructor too.
public class UserConfig extends JsonConfiGoodie {
@Goodie
String username, password;
// Nullary Constructor
public UserConfig() {}
public UserConfig(String username, String password) {
this.username = username;
this.password = password;
}
}
Yes, they can have methods and fields totally independently from the Goodie fields!
public class ItemsConfig extends JsonConfiGoodie {
@Goodie
List<String> itemNames;
public String getRandomItem() {
Random random = new Random();
return itemNames.get(random.nextInt(itemNames.size()));
}
}
In honor of the deprecated repository iGoodie/ConfiGoodies. It was the draft version of RuntimeGoodies. F ConfiGoodies