kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.53k stars 81 forks source link

output string '08' and '09' without quote using yaml output format #175

Closed conao3 closed 9 months ago

conao3 commented 9 months ago

Hi, I noticed the below behavior. Is it intended?

input file

$ cat tmp.yml 
Hours:
  - '01'
  - '02'
  - '03'
  - '04'
  - '05'
  - '06'
  - '07'
  - '08'
  - '09'
  - '10'

Actual behavior

Output as json. This result is not strange.

$ yq . tmp.yml           
{
  "Hours": [
    "01",
    "02",
    "03",
    "04",
    "05",
    "06",
    "07",
    "08",
    "09",
    "10"
  ]
}

Output as yaml, I don't know why, the only '08' and '09' are outputted as 08, 09 respectively

$ yq . tmp.yml -y  
Hours:
  - '01'
  - '02'
  - '03'
  - '04'
  - '05'
  - '06'
  - '07'
  - 08
  - 09
  - '10'

As a result, this output could not parse 'yq'.

$ yq . tmp.yml -y | yq .
yq: Error running jq: ValueError: invalid literal for int() with base 8: '08'.

Expected behaiver

Output '08' and '09' are outputted as '08', '09' respectively same as other examples.

$ yq . tmp.yml -y  
Hours:
  - '01'
  - '02'
  - '03'
  - '04'
  - '05'
  - '06'
  - '07'
  - '08'
  - '09'
  - '10'
conao3 commented 9 months ago

Numbers that fall into 0[0-7]+ must be quoted because they are interpreted as octal integers if written as they are, but 08 and 09 are interpreted as strings without quoting, so quoting is omitted.

-- This perspective is provided by @monaqa. Thanks.

The real problem is that 08 is recognized as an octal value and parsing is attempted as an octal value.

$ echo 'hour: 08' | yq .
yq: Error running jq: ValueError: invalid literal for int() with base 8: '08'.

pyyaml parses 08 as '08'.

$ echo 'hour: 08' | python -c 'import yaml; print(yaml.safe_load(input()))'
{'hour': '08'}

So, this issue merges into #152.