omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.99k stars 115 forks source link

Controlling conflict resolution in merge_with #1191

Open slavaGanzin opened 3 months ago

slavaGanzin commented 3 months ago

I was tryin to merge two configs, one docker-compose and docker-compose.swarm override. Swarm do not understand object for depends_on, answer should be only a list.

#docker-compose
services:
  service1:
    # some other args
    depends_on: 
      service2:
        condition: healthy

#docer-compose.swarm just to override depends_on
services:
  service1:
     depends_on:
        - service2
    compose = OmegaConf.load(compose_file)
    override = OmegaConf.load(replace('.yaml', '.swarm.yaml', compose_file))
    swarm = compose.merge_with(override)

    #which will throw this:
    omegaconf.errors.ConfigTypeError: Cannot merge DictConfig with ListConfig
    full_key:
    object_type=dict

First of all it will be really useful to show what can't be merged. Second, Dict and Map couldn't be merged, but that's not what I need, and there is no way to change this behavior: I couldn't catch an error on specific key and fix it. OmegeConf do not return any information for me to fix it. Third, it would be really helpful to have an ability to pass resolution function, like ramda do https://ramdajs.com/docs/#mergeWith

swarm = compose.merge_with(override, lambda a,b: b)

So when conflict arises, I just use right value.

slavaGanzin commented 3 months ago

And there is no easy way to monkey patch this behavior either