Skip to content

1. ConfiGoodie Basics

Anılcan Metinyurt edited this page May 30, 2023 · 1 revision

📚 ConfiGoodies

ConfiGoodies are POJOs with fields that are going to be filled with the data coming from external configuration files.

📖 @Goodie Annotation

@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
	
}

📖 Read Config into POJO

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);

📖 Default Values

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

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));
	}
	
}

📖 FAQ: ConfiGoodies

❔ Are ConfiGoodies allowed to have Constructors?

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;
	} 
	
}

❔ Are ConfiGoodies allowed to have Methods?

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()));
	}
	
}

❔ Why is it named ConfiGoodies and not GoodieConfigs?

In honor of the deprecated repository iGoodie/ConfiGoodies. It was the draft version of RuntimeGoodies. F ConfiGoodies