Skip to content

fix: tolerate env-var placeholders and quoted scalars in deploy.replicas decoding#129

Open
mvanhorn wants to merge 1 commit into
Mcrich23:mainfrom
mvanhorn:fix/95-container-compose-replicas-env-var-decode
Open

fix: tolerate env-var placeholders and quoted scalars in deploy.replicas decoding#129
mvanhorn wants to merge 1 commit into
Mcrich23:mainfrom
mvanhorn:fix/95-container-compose-replicas-env-var-decode

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

Give Deploy a custom init(from:) following the lenient-decoding precedent already established in Service.init(from:) (Service.swift, which uses try-Int/try-String fallbacks for mem_limit, command, entrypoint, etc.). Add an explicit CodingKeys enum for mode, replicas, resources, restart_policy. Decode replicas by first attempting Int; on failure, decode the String scalar, trim whitespace and surrounding quotes, and parse it with Int(_:) so quoted numerics like "2" still yield an integer.

Why this matters

Running container-compose up --env-file .env -f compose.yml fails with DecodingError.typeMismatch: expected value of type Int ... Expected to decode Int but found Scalar instead when a compose file uses an environment variable for deploy.replicas (e.g. replicas: ${CORES} with CORES=1 in .env). The root cause is decode ordering: ComposeUp decodes the YAML into DockerCompose (ComposeUp.swift line 164) before the env file is even loaded (line 167), and env-var interpolation happens per-field via resolveVariable after decoding.

See #95.

Testing

  • Happy path: a compose file with deploy: {mode: replicated, replicas: 3} still decodes with replicas == 3 (no regression on plain Int scalars). - Quoted numeric: replicas: "2" decodes with replicas == 2 instead of throwing typeMismatch. - Exact issue repro: the snippet from issue Got error "Expected to decode Int but found Scalar instead." if i use an env var in an assignment to "replicas" #95 (mode: replicated, replicas: ${CORES}) decodes successfully; replicas is nil and replicasExpression == "${CORES}", and sibling fields (mode, resources) are unaffected. - Error path: a structurally invalid replicas value that is neither Int nor String scalar (e.g.

Fixes #95

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.

Got error "Expected to decode Int but found Scalar instead." if i use an env var in an assignment to "replicas"

1 participant