Skip to content
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

Configuration file not parsing correctly. #24

Open
rouilj opened this issue Jul 4, 2023 · 5 comments
Open

Configuration file not parsing correctly. #24

rouilj opened this issue Jul 4, 2023 · 5 comments

Comments

@rouilj
Copy link

rouilj commented Jul 4, 2023

Using a configuration file formatted like:

{
    "due": "net 30",
    "from": "XYzzY Inc.",
    "to": "ABCD Services",
    "tax": 0.06
}

and a command line:

./invoice generate --import ABCD.json --id "20230703-33" --title "Invoice for June 2023" --item "Addition of Autoresponder" --quantity 10  --rate 75 --discount .10 --item "Phone review of proposal" --quantity 1 --rate -75

I get:

2023/07/04 00:09:12 json file not correctly formatted

with no info as to why it failed. If I run jq . ABCD.json, it passes without error:

{
  "due": "net 30",
  "from": "XYzzY Inc.",
  "to": "ABCD Services",
  "tax": 0.06
}

Using your example input:

{
    "logo": "/path/to/image.png",
    "from": "Dream, Inc.",
    "to": "Imagine, Inc.",
    "tax": 0.13,
    "items": ["Yellow Rubber Duck", "Special Edition Plaid Rubber Duck"],
    "quantities": [5, 1],
    "rates": [25, 25],
}

fails jq because of the trailing ','. If I remove that so jq is happy, I still get the error: 'file is not formatted correctly'.

Ideas?

Thanks.

@dvrd
Copy link
Contributor

dvrd commented Jul 4, 2023

@rouilj I tested both cases and they work for me. I checked and the problem seems to be passing extra arguments with the import flag

@rouilj
Copy link
Author

rouilj commented Jul 4, 2023

@dvrd Hmm, for me even running:

./invoice generate --import ABCD.json

results in the same error. I would claim that --import should be able to work with command
line arguments in any case. I could see an ambiguity there though. If tax is set in ABCD.json and I use:

./invoice generate --import ABCD.json --tax 0.05

where "tax: 0.13" is present in ABCD.json, what is the tax? I can see two ways to handle it:

  1. command line overrides value in import so tax is 0.05.
  2. order of import and setting matter (last wins):
    • --import ABCD.json --tax 0.05 tax is 0.05
    • --tax 0.05 --import ABCD.json tax is 0.13
  3. I am not sure how to handle array values (e.g. quantity, item). Does use of --quantity and --item wipe out the setting in ---import file or do they get added to the array? I could see value for both cases.
    • the import file has a monthly retainer charge. Then items/quantity should be added to the import values.
    • the import file setting should be ignored and only command line item/quantity used

For testing, I am using docker to compile/run the example. On the host:

$ git clone https://github.com/maaslalani/invoice.git
$ cd invoice
$ docker run -u 1000:1000 -it -v $PWD:/mnt golang@sha256:344193a70dc3588452ea39b4a1e465a8d3c91f788ae053f7ee168cebf18e0a50

Note my uid/gid on the host is 1000, 1000.

Inside the running container:

$ go version
go version go1.20.5 linux/amd64
$ cd /mnt
$ mkdir /tmp/home
$ HOME=/tmp/home
$ go build -v
[bunch of stuff]
$ ./invoice generate --import ABCD.json --id "20230703-33" --title "Invoice for June 2023" --item "Addition of Autoresponder" --quantity 10  --rate 75 --discount .10 --item "Phone review of proposal" --quantity 1 --rate -75
2023/07/04 19:40:19 json file not correctly formatted
$ sum ABCD.json
25530     1 ABCD.json
$ cat ABCD.json
{
    "logo": "/path/to/image.png",
    "from": "Dream, Inc.",
    "to": "Imagine, Inc.",
    "tax": 0.13,
    "items": ["Yellow Rubber Duck", "Special Edition Plaid Rubber Duck"],
    "quantities": [5, 1],
    "rates": [25, 25],
}

Ideas on how to debug this?

Thanks.

@dvrd
Copy link
Contributor

dvrd commented Jul 4, 2023

Well for that output example you have an extra comma in the json. Json cannot have linggering commas

@rouilj
Copy link
Author

rouilj commented Jul 4, 2023

Well for that output example you have an extra comma in the json. Json cannot have linggering commas

Yup, that's how it's documented as I noted in my original report. jq noted that it was incorrect.
Removing the trailing ',' makes the bare ./invoice --import ABCD.json work correctly. Sorry for the bad test case. If the output had reported a line number where the error was or some info
about why it was incorrect it would help.

I agree that mixing --import and other command line parameters are the cause of the issue.

@LewWadoo
Copy link

LewWadoo commented Aug 4, 2023

The example JSON contains a lingering comma.

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

No branches or pull requests

3 participants