3

I'm trying to retain a few business configs on the domain through yaml file.

Structure of yml config file:

bank:
    accountType1:
        debit:
            - Product1
            - Product2
        hybrid:
            - ProductX
            - ProductY
    accountType2:
        debit:
            - Product1
            - Product2
            - Product3
        hybrid:
            - ProductX
            - ProductY
            - ProductZ

I have the below enums for on the domain.

enum AccountType{ ACCOUNT_TYPE1, ACCOUNT_TYPE2}
enum Feature{ DEBIT, HYBRID}
enum Products { Product1, Product2, Product3, ProductX, ProductY, ProductZ }

I would like to seek help to get these business configs as a MultikeyMap, or a nested map, but using enums as keys. For eg:

Map<AccountType, Map<Feature, List<Products>>

Basically I would like the config to perform the following lookup:

if AccountType = accountType1 & Feature = debit, then return [Product1,Product2]

I couldn't find a way to do this elegantly via config files, as it always initialises to null

@ConfigurationProperties(prefix="bank")
public class ProductResolver {
Map<AccountType, Map<Feature, List<Products>> configMap 
  // NestedMap, Apache MultiKeymap or something similar either should be ok for me
}

Any experts, please could you guide me?

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
technoJ
  • 175
  • 1
  • 1
  • 16
  • [this helps?](https://stackoverflow.com/questions/71154180/how-to-inject-spring-application-properties-grouped-by-prefix/71155036#71155036) – Eugene Mar 02 '22 at 21:13
  • @Eugene I am aware of Map but couldn’t make it work with enums and multikey maps; which is what am looking for. Any clue if this is doable? – technoJ Mar 03 '22 at 06:50

1 Answers1

1

If you can slightly change you mappings, like this:

bank:
 map:
  accountType1:
    debit:
     - Product1
     - Product2
    hybrid:
     - ProductX
     - ProductY

Then below config actually worked for me just fine:

@ConfigurationProperties(prefix = "bank")
public class NewProps {

    private Map<AccountType, Map<String, List<Product>>> map = new HashMap<>();

    public Map<AccountType, Map<String, List<Product>>> getMap() {
        return map;
    }

    public void setMap(Map<AccountType, Map<String, List<Product>>> map) {
        this.map = map;
    }
}
Eugene
  • 117,005
  • 15
  • 201
  • 306