Michael Dz
Michael Dz

Reputation: 3854

Spring conditional yaml property value

That's how one line of my .yaml properties file looks like:

profiles.active: rabbit-${CLUSTER_ENV}, mongo-${CLUSTER_ENV} ...

I want to put the below logic only for rabbit- property:

if(CLUSTER_ENV == "local") {
   return "dev";
} else {
   return CLUSTER_ENV;
}

Other properties should be filled with local but only in this place the property value should be conditionally filled. Can I somehow add this logic in Spring yaml properties?

Upvotes: 0

Views: 6454

Answers (1)

WiPU
WiPU

Reputation: 443

It does not look pretty, but you can use something like:

 #this can be added on startup
mykey: key1

#a map with your condition
mymap:
  key1: val1
  key2: val2

#your value based on the condition
conditional: con-${mymap.${mykey}}

regards, WiPU

UPDATE based on comment:

 #this can be added on startup as variable
mykey: local

#a map with your condition
mymap:
  local: dev
  xyz: test

# your value based on the condition or the key as fallback if the key is not 
# present in mymap.
conditional: con-${mymap.${mykey}:${mykey}}

Upvotes: 6

Related Questions