webdriverio / webdriverio/cddl

Generators emit wrong numeric type for floating-point ranges

Open
#78 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4
Forks
4
Avg merge
1d 13h
Merged PRs (30d)
7

Description

Generators emit wrong numeric type for floating-point ranges

Summary

The CDDL→language generators (cddl2java, cddl2py, cddl2swift, cddl2kotlin)
render floating-point ranges such as (0.0..1.0) using an integer type, making
them indistinguishable from integer ranges like (0..1) in the generated code.

This was previously impossible to fix because the parser collapsed whole-valued
float literals to plain integers. That root cause is now fixed: the cddl
parser preserves float-ness on numeric literal nodes via an IsFloat: true
marker (see the parser PR). The generators can now consume that field.

Current behavior per package

  • cddl2javasrc/index.ts range branch picks the type with
    Number.isInteger(minVal) ? 'Integer' : 'Float'. For a whole-valued float
    range (0.0..1.0) the min value is 0, so it wrongly emits Integer.
    Non-whole ranges like (0.5..1.5) were already correct. This is the only
    generator that attempts the distinction at all.
  • cddl2py — range types are hardcoded to int.
  • cddl2swift — range types are hardcoded to Int.
  • cddl2kotlin — range types are hardcoded to Long.
  • cddl2ts — correctly renders ranges as number; TypeScript has no
    separate integer/float type, so no change is needed or possible.

Suggested fix

Each generator's range handling should consult the new IsFloat marker on the
range's Min/Max literal nodes and emit the language's floating-point type
(Float/Double) when set. For example, cddl2java's range branch becomes
roughly:

const minNode = specType[0].Value.Min
const isFloat = minNode.IsFloat === true || !Number.isInteger(minNode.Value)
return { type: isFloat ? 'Float' : 'Integer', isLiteral: false }

The Python/Swift/Kotlin generators need a slightly larger change since they do
not currently branch on the range element type at all.

Related

Parser fix that makes this possible: https://github.com/webdriverio/cddl/pull/77

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the range branch in cddl2java's src/index.ts, then inspect the range handling in cddl2py, cddl2swift, and cddl2kotlin. Use the parser's IsFloat marker on the Min or Max literal nodes to distinguish whole-valued floating-point ranges. Done means those generators emit their floating-point types for float ranges, preserve integer types for integer ranges, and leave cddl2ts unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, python, swift, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.