Skip to content

Conversation

@sylvainmouquet
Copy link

#2730
#4347

Currently, the environment variables are string only.
This patch try to cast a variable to his natural type.
It's possible to get a string every time with using this format : ${FOO:-"1"}

Unit tests

    assert defaults_interpolator("${BAR:-1}") == 1
    assert defaults_interpolator("${BAR:--1}") == -1
    assert defaults_interpolator("${BAR:-1.2}") == 1.2
    assert defaults_interpolator("${BAR:-True}") == True
    assert defaults_interpolator("${BAR:-False}") == False
    assert defaults_interpolator("${BAR:-\"True\"}") == "True"
    assert defaults_interpolator("${BAR:-\"False\"}") == "False"
    assert defaults_interpolator("${BAR:-\"1\"}") == "1"
    assert defaults_interpolator("${BAR:-\"1.2\"}") == "1.2"
    assert defaults_interpolator("${BAR:-\"foo\"}") == "foo"

Sample file sample-compose.yml

version: "3.3"

networks:
  default:
    external:
      name: ${NETWORK_NAME:-"1"}
      
    
services:
  monservice:
    image: alpine
    command: nc -lp1789
    ports:
      - "1789:1789"
    deploy:
      replicas: ${NC_REPLICAS:-1}

before:

$ docker-compose -f ../../../Desktop/sample-compose.yml config
ERROR: The Compose file '.\../../../Desktop/sample-compose.yml' is invalid because:
services.monservice.deploy.replicas contains an invalid type, it should be an integer

after:

$ docker-compose -f ../../../Desktop/sample-compose.yml config
WARNING: Some services (monservice) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
networks:
  default:
	external:
	  name: '1'
services:
  monservice:
	command: nc -lp1789
	deploy:
	  replicas: 1
	image: alpine
	ports:
	- published: 1789
	  target: 1789
version: '3.3'




An environment variable like ${FOO:-1} is replaced to 1 (int) and not '1' (string)

Signed-off-by: Sylvain Mouquet <sylvain.mouquet@gmail.com>
@sylvainmouquet sylvainmouquet changed the title Add casting to environment variables Add cast to environment variables Aug 31, 2017
Signed-off-by: Sylvain Mouquet <sylvain.mouquet@gmail.com>
@sylvainmouquet sylvainmouquet changed the title Add cast to environment variables Add cast to environment variables Sep 8, 2017
@shin-
Copy link

shin- commented Oct 23, 2017

Closing in favor of #5291

Thank you!

@shin- shin- closed this Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants