This is useful when we want to map a value from a config to a field that has a different name. The usual way this can be accomplished is by writing a FromDoc, but for many cases that's tedious and completely overkill.
For example, given the following class and config
class Player {
public int currentHealth;
}
# Player.yaml
startingHealth: 100
We want to be able to map the startingHealth value in the yaml to the currentHealth field in a Player instance.
This can currently be accomplished with:
class Player {
public int currentHealth;
public static object FromDoc(object existing, DocNode doc) {
var result = existing as Player ?? new Player();
result.currentHealth = doc["startingHealth"];
return result;
}
}
This has a number of downsides:
Mapping a differently-named yaml key to a single field a complex object means we have to write a FromDoc for the entire object. If there are 100 fields with one that we wish to have a non-default mapping, we need to duplicate the normal functionality for 99 keys just so we can override the parsing behavior of one
Because we're doing custom parsing in a FromDoc, we lose the normal ReificationOptions behavior and would need to re-implement that error checking as well.
Lots more code to write
An possible solution is to add a ConfigKeyAttribute class that lets the user specify an alternative yaml key to use.
class Player {
[ConfigKey("startingHealth")]
public int currentHealth;
}
This also needs to be extended to SetFieldOnObject and SetFieldOnStruct. In these cases it should be implemented as an optional string parameter that lets the user pass the yaml field name through to TypeReifier.
This is useful when we want to map a value from a config to a field that has a different name. The usual way this can be accomplished is by writing a FromDoc, but for many cases that's tedious and completely overkill.
For example, given the following class and config
We want to be able to map the
startingHealth
value in the yaml to thecurrentHealth
field in a Player instance.This can currently be accomplished with:
This has a number of downsides:
ReificationOptions
behavior and would need to re-implement that error checking as well.An possible solution is to add a
ConfigKeyAttribute
class that lets the user specify an alternative yaml key to use.This also needs to be extended to
SetFieldOnObject
andSetFieldOnStruct
. In these cases it should be implemented as an optional string parameter that lets the user pass the yaml field name through toTypeReifier
.