Skip to content

[FR] Try to parse strings as integers when requiring an integer #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pindab0ter opened this issue Feb 27, 2025 · 4 comments
Open

[FR] Try to parse strings as integers when requiring an integer #791

pindab0ter opened this issue Feb 27, 2025 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@pindab0ter
Copy link

pindab0ter commented Feb 27, 2025

Is your feature request related to a problem? Please describe.
In an attempt to set up different values for different environments (e.g. staging and production) I'm passing the values as environment variables and using them like so:

services:
  projectname:
    container_name: app
    image: ${IMAGE_ID}
    deploy:
      replicas: ${ECS_REPLICAS}

However, this results in the following error:

Traceback (most recent call last):
  File "/home/runner/work/projectname/venv/bin/ecs-compose-x", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/runner/work/projectname/venv/lib/python3.12/site-packages/ecs_composex/cli.py", line 194, in main
    settings = ComposeXSettings(**vars(args))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/projectname/venv/lib/python3.12/site-packages/ecs_composex/common/settings.py", line 177, in __init__
    self.set_content(kwargs, content)
  File "/home/runner/work/projectname/venv/lib/python3.12/site-packages/ecs_composex/common/settings.py", line 525, in set_content
    content_def = ComposeDefinition(files, content)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/projectname/venv/lib/python3.12/site-packages/compose_x_render/compose_x_render.py", line 324, in __init__
    jsonschema.validate(
  File "/home/runner/work/projectname/venv/lib/python3.12/site-packages/jsonschema/validators.py", line 1332, in validate
    raise error
jsonschema.exceptions.ValidationError: '1' is not of type 'integer'

Failed validating 'type' in schema['properties']['services']['patternProperties']['^[a-zA-Z0-9._-]+$']['properties']['deploy']['properties']['replicas']:
    {'type': 'integer'}

On instance['services']['projectname']['deploy']['replicas']:
    '1'

Describe the solution you'd like
When requiring an integer, I would like a string to attempt a string to int conversion before discarding the value for not being the correct type.

Describe alternatives you've considered
YAML does not support conditionals, so the values will have to be set outside of the YAML file. When bringing them in they are always treated as strings, for as far as I know. I do not know of a way to mitigate this.

@pindab0ter pindab0ter added the enhancement New feature or request label Feb 27, 2025
@JohnPreston
Copy link
Member

Hello @pindab0ter
The error comes from the fact that the JSON schema for this only accepts integer whereas you have set ${ECS_REPLICAS} and unfortunately, env vars == string, that's why it's upset about it.

@JohnPreston
Copy link
Member

Seeing https://github.com/compose-spec/compose-spec/blob/main/schema/compose-spec.json#L521 the definition was updated since so I will update that to accept a string as argument :)

@pindab0ter
Copy link
Author

I have found !!int. Unfortunately, the type coercion happens before the environment variable substitution happens, which means that this is not a solution.

I look forward to the update with support for both strings and ints in those fields.

We're also using x-scaling with ScheduledActions:

    x-scaling:
      Range: "${ECS_OFFICE_HOURS_MIN_CAPACITY}-${ECS_OFFICE_HOURS_MAX_CAPACITY}"
      TargetScaling:
        CpuTarget: 50
      ScheduledActions:
        - Timezone: Europe/Amsterdam
          Schedule: cron(30 7 ? * MON-FRI)
          ScheduledActionName: Scale.Up
          ScalableTargetAction:
            MinCapacity: "${ECS_OFFICE_HOURS_MIN_CAPACITY}"
            MaxCapacity: "${ECS_OFFICE_HOURS_MAX_CAPACITY}"
          MacroParameters:
            AddServiceName: true
        - Timezone: Europe/Amsterdam
          Schedule: cron(30 17 ? * MON-FRI)
          ScheduledActionName: Scale.Down
          ScalableTargetAction:
            MinCapacity: "${ECS_AFTER_HOURS_MIN_CAPACITY}"
            MaxCapacity: "${ECS_AFTER_HOURS_MAX_CAPACITY}"

I could not find these in the schema you linked. Do you reckon this will work, too?

@JohnPreston
Copy link
Member

JohnPreston commented Feb 27, 2025

Yeah okay, so lots of these values need to be casted to int() from str in case you use env vars, I see. I don't use env vars all that much in my worflows and I guess defo not for numbers 🤣

Should be easy to do though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants