AuthMe / ConfigMe

A simple configuration management library for any Java project!
MIT License
37 stars 16 forks source link

Get the file name for using in MigrationService #411

Closed drori200 closed 8 months ago

drori200 commented 8 months ago

With this library I split my old config into multiple files with the names of each section. The old config was one YML file that stored all the configs for every item and each section is an item. Example structure:

TPItem1:
 properties....

TPItem2:
 properties....
more sections below....

Each of the section has the same properties but different values set by the end user. For my migration service the best solution is for me to get the config file name for using for each property path (TPItem1.property, TPItem2.property....) Is there anything possible with this?

ljacqu commented 8 months ago

Hi, interesting problem! The easiest way would probably be to instantiate a separate MigrationService for each resource, so that you can pass into it what the resource name is. Failing that, you get configurationData as argument, which is the definition of the resource you're currently migrating for. Maybe based on a property there you could determine what resource you currently have?

Given your example, all properties for TPItem2 are prefixed with TPItem2, so it could be as simple as:

private static String determineResource(ConfigurationData configurationData) {
  Property<?> firstProperty = configurationData.getProperties().get(0);
  int indexFirstDot = firstProperty.getPath().indexOf('.');
  return firstProperty.getPath().substring(0, indexFirstDot);
}

Requires slightly more safeguards in the code if you also have paths that don't have a dot in them, but hopefully that brings the idea across.

Are these ideas helpful to you?

drori200 commented 8 months ago

Thanks for your response! To give some context my configs are for items (8 of the same item but each has different values when the player plays the game) so what i ended up doing is just passing the item's ID (8 items - IDs are ranging from 1 - 8) to the MigrationService constructor. That way i don't have to get the file name and make it a whole lot broader.